MySQL一主多從
阿新 • • 發佈:2018-04-07
MySQL主192.168.65.128上配置/etc/my.cnf
[root@localhost ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS!
備份數據
從1、192.168.65.129的/etc/my.cnf
[root@localhost ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!
從2、192.168.65.130的/etc/my.cnf
[root@localhost ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!
主上備份數據
[root@localhost ~]# mysqldump -uroot -p123456 --all-databases > mysqlall.sql
傳輸數據到從上
[root@localhost ~]# scp mysqlall.sql 192.168.65.129:/tmp/ [root@localhost ~]# scp mysqlall.sql 192.168.65.130:/tmp/
主上創建用作主從同步的虛擬用戶
mysql> grant replication slave on *.* to 'repl'@192.168.65.129 identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> grant replication slave on *.* to 'repl'@192.168.65.130 identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> show master status;
鎖表(做實驗沒有數據變化,可以不鎖表)
mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec)
在從上恢復數據
[root@localhost /]# mysql -uroot -p123456 < /tmp/mysqlall.sql
mysql> stop slave; Query OK, 0 rows affected (0.00 sec) mysql> change master to master_host = '192.168.65.128', master_user = 'repl', master_port=3306, master_password='123456', master_log_file = 'thermos.000001', master_log_pos=542; Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G
和從1一樣,配置從2
成功!
如有錯誤,敬請指出,謝謝!
MySQL一主多從