今天有同事问MySQL可否多主一从进行实时备份,google下可以使用mysqld_multi来管理多个MySQL实例。
之前有想过在一台服务器上运行多个MySQL实例就可以实现一从多主的master-slave方式的实时备份,今天刚好看到mysqld-multi就动手测试下
配置过程如下,
1. 修改mysql的配置文件my.cnf配置mysqld_multi的选项
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user = multi_admin
password = multipass
log = /usr/local/mysql/etc/mysqld_multi.log
2. 创建mysqld_multi的config-file
cat /usr/local/mysql/etc/mysqld_multi.cnf
[mysqld1]
socket = /tmp/mysql.sock1
port = 3306
pid-file = /usr/local/mysql/var1/localhost.pid
datadir = /usr/local/mysql/var1
user = mysql
[mysqld2]
socket = /tmp/mysql.sock2
port = 3307
pid-file = /usr/local/mysql/var2/localhost.pid
datadir = /usr/local/mysql/var2
user = mysql
[mysqld3]
socket = /tmp/mysql.sock3
port = 3308
pid-file = /usr/local/mysql/var3/localhost.pid
datadir = /usr/local/mysql/var3
user = mysql
3. 拷贝mysqld_multi.server(在源代码的support-files中)脚本到/usr/sbin
cp /mysql_source/support-files/mysqld_multi.server /usr/sbin
4. 编辑/usr/sbin/mysqld_multi.server(添加–config-file=/usr/local/mysql/etc/mysqld_multi.cnf选项)
'start' )
"$mysqld_multi" start $2
;;
'stop' )
"$mysqld_multi" --config-file=/usr/local/mysql/etc/mysqld_multi.cnf stop $2
;;
'report' )
"$mysqld_multi" --config-file=/usr/local/mysql/etc/mysqld_multi.cnf report $2
5. 启动多个MySQL实例
mysqld_multi.server start
6. 之前在my.cnf设置的user和password是用来关闭数据库实例的,根据配置中的用户名和密码授权该用户shutdown权限
mysql -u root -S /tmp/mysql.sock1 -p
mysql> grant shutdown on *.* to multi_admin@'localhost' identified by 'multipass';
7. 关闭所有数据库实例
mysqld_multi.server stop
8. 对单台数据库的启动和关闭
语法:mysqld_multi [options] {start|stop|report} [GNR[,GNR] …]
如对mysqld1的启动和关闭,可以通过mysqld_multi来操作
启动: mysqd_multi start 1
关闭: mysqd_multi stop 1
也可以同时启动和关闭多个数据库实例
启动: mysqd_multi start 1-3
关闭: mysqd_multi stop 1-3