今天有同事問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
http://www.sanote.cn/mysqld-multi.html