1. 程式人生 > >mysql主從伺服器設定

mysql主從伺服器設定

● 準備多個mysql(5.7.23)
● master設定

[[email protected] ~]# vi /etc/my.cnf
#在節點加入如下配置(然後重啟mysql)
[mysqld]
#id
server-id=1
#二進位制日誌檔案(注意是/  不是\)
log-bin="D:/MySQL/MySQL Server 5.5/data/mysql-bin"
#錯誤記錄檔案
log-error="D:/MySQL/MySQL Server 5.5/data/mysql-error"
#主從同步時 忽略的資料庫
binlog-ignore-db=mysql
#(可選)指定主從同步時,同步哪些資料庫
binlog-do-db=cok_db1

master授權哪些是從資料庫:

mysql>GRANT REPLICATION slave,reload,super ON *.* TO 'root'@'192.168.0.%' IDENTIFIED BY 'root';
mysql>flush privileges ; 

檢視master資料庫的狀態(每次在主從同步前,需要觀察 主機狀態的最新值)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 38848152 | cok_db1      |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

● slave設定(my.cnf加入如下)

[mysqld]
server-id=2
#mysql-bin此路徑要注意是否有足夠的許可權
log-bin=mysql-bin
#replicate-do-db與master資料庫binlog-do-db保持一致
replicate-do-db=cok_db1

從資料庫進入mysql設定master

#master_log_pos : 此值就是master的Position值
#master_log_file : 對應master的File值
CHANGE MASTER TO 
MASTER_HOST = '192.168.0.118', 
MASTER_USER = 'root', 
MASTER_PASSWORD = 'root', 
MASTER_PORT = 3306,
master_log_file='mysql-bin.000005',
master_log_pos=38848152;

若執行上述語句報錯

如果報錯:This operation cannot be performed with a running slave; run STOP SLAVE first
解決:STOP SLAVE ;再次執行上條授權語句

● 開啟主從同步

slave節點操作:
mysql> start slave;
#檢驗
mysql> show slave status \G
#然後會噼裡啪啦列出一大堆東西,我們只需要關注如下:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#若兩個都是YES,說明沒問題
#否則:檢視同列
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error: