首页 > 代码库 > mysql安装与多实例(详解)

mysql安装与多实例(详解)

mysql安装及多实例,配置优化

 

yum install tree nmap sysstat lrzsz dos2unix -y

安装相关的包
tar -xf cmake-2.8.8.tar.gz
cd cmake-2.8.8
./configure
make && make install

yum install gcc gcc-c++ autoconf* automake* zlib* libxml* ncurses-devel* libgcrypt* libtool* openssl*

1.添加用户
groupadd mysql
useradd mysql -M -s /sbin/nologin -g mysql

2.
mkdir -p /database/mysql

chown mysql.mysql -R /database/mysql

tar -zxvf mysql-5.6.24.tar.gz
cd mysql-5.6.24
make clean
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ \
-DMYSQL_DATADIR=/database/mysql \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DTRACE=0 \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_EMBEDDED_SERVER=1 \

make && make install

/bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
/bin/cp /usr/local/mysql/support-files/my-small.cnf /etc/my.cnf

chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on

配置环境变量
echo ‘export PATH=/usr/local/mysql/bin:$PATH’ >>/etc/profile
source /etc/profile
echo $PATH

初始化数据库
cd /usr/local/mysql/scripts
./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/database/mysql

chown mysql.mysql -R /database/mysql
chmod -R 1777 /tmp/

/etc/init.d/mysqld start

 

常见问题:

登录mysql故障解决
ERROR 1045(28000):Access denied for user ‘root‘@‘localhost‘(using password: NO)
问题解决:
杀死进程 pkill mysqld
删除数据文件 rm -rf /database/mysql/data/*
重新初始化 cd /usr/local/mysql/scripts
./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/database/mysql

系统报错字符集问题解决
改成UTF-8

vim /etc/sysconfig/i18n

LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"

 

基本优化

简单清理用户表:
查看用户表 select user,host from mysql.user;
删除密码为空用户 delete from mysql.user where Password=‘‘;
删除用户为空 delete from mysql.db where User=‘‘;
删除IPV6 delete from mysql.user where host=‘::1‘;

添加管理员账号:
grant all privileges on *.* to system@‘localhost‘ identified by ‘123456‘ with grant option;
flush privileges;

增加root密码:

mysqladmin -u root password ‘linuxboy‘

 

mysql多实例

就是在一台机器上面开启多个不同的端口,运行多个MySQL服务进程。这些MySQL多实例公用一套安装程序,使用不同的(也可以是相同的)配置文件,
启动程序,数据文件。在提供服务时候,多实例MySQL在逻辑上看来是各自独立的,多个实例的自身是根据配置文件对应的设定值,来取得相关硬件资源的多少。

作用与缺点
有效利用服务器的资源,单个服务器资源有剩余时,可以充分利用剩余的资源提供更多的服务。
高并发下某一个实例占用很多资源互相争抢问题


单一配置文件部署方案:
mysqld_multi --config-file=/database/mysql/my_multi.cnf start 1,2,3,4,5,6,7启动
耦合性太高

mysql多实例安装


yum install gcc gcc-c++ autoconf* automake* zlib* libxml* ncurses-devel* libgcrypt* libtool* openssl* libaio-devel

1.添加用户
groupadd mysql
useradd mysql -M -s /sbin/nologin -g mysql

创建mysql多实例的数据文件目录

mkdir -p /data/{3306,3307}/data

主要修改配置文件:
主要是修改配置文件路径
[client]
port = 3306
socket = /data/3306/mysql.sock

[mysqld]

port = 3306
socket = /data/3306/mysql.sock
basedir = /usr/local/mysql
datadir = /data/3306/data

[mysqld_safe]

log-error=/data/3306/ilanni.err
pid-file=/data/3306/ilanni.pid

******************************************************

[client]
port = 3307
socket = /data/3307/mysql.sock

[mysqld]

port = 3307
socket = /data/3307/mysql.sock
basedir = /usr/local/mysql
datadir = /data/3307/data

[mysqld_safe]

log-error=/data/3307/ilanni.err
pid-file=/data/3307/ilanni.pid

多实例启动脚本

#!/bin/bash

port=3306
mysql_user="root"
mysql_passwd="123456"
CmdPath="/usr/local/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"

function_start_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "Starting Mysql...\n"
/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 >/dev/null &
else
printf "Mysql is running...\n"
exit
fi
}
function_stop_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "Mysql is stopped...\n"
exit
else
printf "Stoping Mysql...\n"
${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_passwd} -S /data/${port}/mysql.sock shutdown
fi
}

function_restart_mysql()
{
printf "Restang Mysql...\n"
function_stop_mysql
sleep 2
function_start_mysql
}

case $1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
esac


授权mysql用户

chown -R mysql.mysql /data

初始化mysql多实例的数据库文件
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql –datadir=/data/3306/data –user=mysql

/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql –datadir=/data/3307/data –user=mysql
启动mysql

/data/3306/mysql start

单实例增加密码

mysqladmin -u root -S /data/3306/mysql.sock password ‘linuxboy‘

 

mysql 应用管理

找回丢失的Mysql root 用户密码

使用--skip-grant-tables启动Mysql,忽略授权登录验证

mysqld_safe --skip-grant-tables --user=mysql &

多实例Mysql启动修改丢失root 密码方法

启动时加--skip-grant-tables参数
mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-table &
mysql -u root -p -s /data/3306/mysql.sock

 

mysql安装与多实例(详解)