Mysql 主從復制,mysql主從復制
mysql 主從復制步驟
1)下載MySQL(Ubuntu)
sudo apt-get install mysql-server mysql-client
2)更改mysql的配置文件
vi /etc/mysql/my.cnf
修改內容如下
#綁定本機ip地址,作為master
bind-address = 192.168.0.75
#作為master
server-id = 1
#去掉注釋
log_bin = /var/log/mysql/mysql-bin.log
#需要復制的數據庫
binlog_do_db = zm_gaiay_net_cn
保存更改
2)重啟mysql
sudo service mysql restart
3)通過mysql client 進入命令行
mysql -u root -p
提示輸入root密碼
4)把復制權限給slave_user,並指定密碼
grant replication slave on *.* to 'slave_user'@'%' identified by '123456';
5)連接binlog_do_db 對應的數據庫 (zm_gaiay_net_cn)
use zm_gaiay_net_cn;
6)鎖住表以免發生新的變化
flush tables with read lock;
7)查看master status
show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 107 | zm_gaiy_net_cn | |
+------------------+----------+--------------+------------------+
從庫中會使用到 file 和 position
從庫中安裝
1)安裝mysql
2)更改mysql配置文件my.cnf
server-id = 2
#添加,注意這裡是中劃線
replay-log = /var/log/mysql/mysql-replay-bin.log
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = zm_gaiay_net_cn
3)重啟服務
sudo service mysql restart
4)通過mysql-client 進入命令行界面
mysq -u root -p
配置主庫
CHANGE MASTER TO MASTER_HOST='192.168.0.75',MASTER_USER='slave_user', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS= 107;
解釋
master_host : 主庫的ip
master_user :組庫中用來復制的用戶
master_password:主庫中用來復制的用戶密碼
master_log_file:主庫(zm_gaiay_net_cn)對應的bin文件
master_log_pos:從哪裡開始復制
5)開啟復制
start slave
6)查看狀態
SHOW SLAVE STATUS