簡單Mysql主從同步做法
1、主庫配置
1)vim /etc/my.cnf
[mysqld]
server-id=1 #伺服器唯一ID,每個伺服器必須不一樣,不然會報錯
log-bin=master-bin #開啟binlog日誌功能,可以指定絕對路徑,不指定預設在datadir指定目錄下
binlog-ignore-db = mysql
binlog-ignore-db = information_schema
binlog-do-db = mysqldb
binlog_format = Row/Statement/Mixed
systemctl restart mysqld #重啟mysql
2)建立用於主從的使用者以及許可權,資料庫中執行
grant replication slave on *.* to 'sms'@'%' identified by "密碼";
grant replication slave on *.* to 'sms'@'%';
show master status; #檢視主庫資訊(binlong名稱和位置,用於節點同步開始位置)
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
| master-bin.000004 | 891 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2、從庫配置
[mysqld]
server-id=2
relay-log=mysql-relay#開啟中繼日誌,可以指定絕對路徑,不指定預設在datadir指定目錄下
master_info_repository = TABLE
relay_log_info_repository = TABLE
relay_log_recovery = ON
systemctl restart mysqld #重啟資料庫
2)配置從庫的主機資訊,資料庫中執行
change master to
master_host='192.168.118.133',
master_user='root',
master_password='[email protected]',
master_log_file='master-bin.000004',
master_log_pos= 891;
start slave; #啟動
show slave status\G; 檢視主從狀態 #Slave_IO_Running: Yes Slave_SQL_Running: Yes 都為Yes則主從成功