mysql主從配置詳解(圖文)

最近工做不是很忙,把之前整理的mysql數據庫的主從配置過程記錄一下,有不足之處,請各位多多糾正指教 mysql

#環境配置#
master IP:192.168.46.137 slave IP:192.168.46.138 database:v1

1.在兩臺機器,分別安裝mysql數據庫,分別添加遠程鏈接權限sql

2.配置主數據庫 數據庫

#編輯mysql的配置文件,增長如下內容#
vim etc/my.cnf
server-id=1
binlog-do-db=v1
relay-log=/var/lib/mysql/mysql-relay-bin
relay-log-index=/var/lib/mysql/mysql-relay-bin.index
log-error=/var/lib/mysql/mysql.err
master-info-file=/var/lib/mysql/mysql-master.info
relay-log-info-file=/var/lib/mysql/mysql-relay-log.info
log-bin=/var/lib/mysql/mysql-bin
編輯完成後,重啓mysql,systemctl restart mysqld.service(centos7 直接使用systemctl命令)
建立一個複製用戶,具備replication slave 權限
grant replication slave on *.* to 'user3'@'192.168.46.138' identified by 'user3';
用戶名:user3 密碼:user3
flush privileges;
#查看主庫狀態#
show master status;
  
記住FileSet和Position參數值,後面步驟會用到

3.配置從數據庫 vim

#編輯從數據庫配置文件,添加如下內容#
vim etc/my.cnf
server-id=2
replicate-do-db=v1
relay-log=/var/lib/mysql/mysql-relay-bin
relay-log-index=/var/lib/mysql/mysql-relay-bin.index
log-error=/var/lib/mysql/mysql.err
master-info-file=/var/lib/mysql/mysql-master.info
relay-log-info-file=/var/lib/mysql/mysql-relay-log.info
log-bin=/var/lib/mysql/mysql-bin
注意:兩個數據庫配置文件裏的server-id不能相同
重啓mysql
mysql -u root -pxxx
change master to master_host='192.168.46.137',master_user='user3',master_password='user3',master_log_file='mysql-bin.000003',master_log_pos=120;
#啓動slave線程#
start slave;
# 查看slave狀態,注意G後面沒有分號#
show slave status\G
  
  紅色箭頭所指兩個參數,若是都爲Yes,說明配置成功,若是Slave_IO_Running爲connecting,請檢查防火牆,端口是否開放,FIle,Position參數是否一致,網絡是否暢通等
到此,mysql主從配置完成,能夠新增數據查看是否同步了
相關文章
相關標籤/搜索