mysql配置多实例
mysql配置多实例在实际的开发过程中,可能会需要在一台服务器上部署多个MYSQL实例,我们可以通过官方自带的mysqld_multi使用单独的配置文件来实现多实例。
mysqld_multi是管理多个mysqld的服务进程,这些服务进程监听于不同的端口,可以启动、停止和监控当前的服务状态。
一、添加用户和用户组
#添加mysql用户组
groupadd mysql
#创建mysql用户并把它放到mysql组下
useradd -g mysql mysql
#修改mysql文件属性
chown -R root:mysql /usr/local/mysql
二、初始化数据目录
#把用到的工具添加到/usr/bin目录
ln -s /usr/local/mysql/bin/mysqld_multi /usr/bin/mysqld_multi
ln -s /usr/local/mysql/bin/mysql_install_db /usr/bin/mysql_install_db
#初始化四个数据目录
mysql_install_db --datadir=/usr/local/var/mysql1 --user=mysql
mysql_install_db --datadir=/usr/local/var/mysql2 --user=mysql
mysql_install_db --datadir=/usr/local/var/mysql3 --user=mysql
mysql_install_db --datadir=/usr/local/var/mysql4 --user=mysql
#修改属性
chown -R mysql /usr/local/var/mysql1
chown -R mysql /usr/local/var/mysql2
chown -R mysql /usr/local/var/mysql3
chown -R mysql /usr/local/var/mysql4
三、修改多实例安装路径
cp /usr/local/src/mysql-5.1.60/support-files/mysqld_multi.server /etc/init.d/mysqld_multi.server
#修改basedir和bindir为安装路径
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin
四、配置多实例数据库配置文件
用mysqld_multi工具查看该配置文件的模板方法,命令为:mysqld_multi --example
在/etc/目录下创建创建文件/etc/mysqld_multi.cnf,把mysqld_multi --example产生的文件粘进去,修改相应属性,如:mysqld,mysqladmin,socket,port,pid- file,datadir,user等。
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
#user = mysql
#password = my_password
[mysqld1]
socket = /usr/local/var/mysql1/mysql1.sock
port = 3306
pid-file = /usr/local/var/mysql1/mysql1.pid
datadir = /usr/local/var/mysql1
#language = /usr/local/mysql/share/mysql/english
user = mysql
[mysqld2]
socket = /usr/local/var/mysql2/mysql2.sock
port = 3307
pid-file = /usr/local/var/mysql2/mysql2.pid
datadir = /usr/local/var/mysql2
#language = /usr/local/mysql/share/mysql/english
user = mysql
[mysqld3]
socket = /usr/local/var/mysql3/mysql3.sock
port = 3308
pid-file = /usr/local/var/mysql3/mysql3.pid
datadir = /usr/local/var/mysql3
#language = /usr/local/mysql/share/mysql/english
user = mysql
[mysqld4]
socket = /usr/local/var/mysql4/mysql4.sock
port = 3309
pid-file = /usr/local/var/mysql4/mysql4.pid
datadir = /usr/local/var/mysql4
#language = /usr/local/mysql/share/mysql/english
user = mysql
五、启动多实例数据库
mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf start
启动具体某一个实例可在start、stop后面加上具体数据1,2,3等
六、检查是否启动成功
#查看相应端口是否已经被监听
netstat -tunlp
#查看是否有活动进程
ps -aux|grep mysql
如果没有发现你要的端口号mysql实例,可以检查下/var/lib/mysql_3307/3307.log文件,排除问题
七、登录相应数据库
#进入端口为3306的数据库
mysql -uroot -p -h127.0.0.1 -P3306
#通过sock文件登录
mysql -uroot -p -S /usr/local/var/mysql1/mysql1.sock
#查看socket文件
mysql> SHOW VARIABLES LIKE 'socket';
#查看pid文件
mysql> SHOW VARIABLES LIKE '%pid%';