mysql主从切换机制torch_MySQL Replication设置(Master/Slave)实现主从复制
一、Master配置
1、my.cnf配置
# vim /etc/mysql/mariadb.conf.d/50-server.cnf
[mysqld]
log-bin=mysql-bin //[必須]啟用二進制日志
server-id=140 //[必須]服務器唯一ID,默認是1,一般取IP最后一段
2、重啟mysql
sudo service mysql restart
3、在主服務器上建立帳戶并授權slave
# mysql -u root -p sD9854fals87s34GB98s
mysql> GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'sD9854fals87s34GB98s';
一般不用root帳號,%表示所有客戶端都可能連,只要帳號,密碼正確,此處可用具體客戶端IP代替,如192.168.245.139,加強安全。
4、登錄mysql,查詢master的狀態
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000014 | 318 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
注:執行完此步驟后不要再操作主服務器MYSQL,防止主服務器狀態值變化。
上面的Position需要記住用于下方使用
二、Slave配置
1、my.cnf配置
# vim /etc/mysql/mariadb.conf.d/50-server.cnf
[mysqld]
log-bin=mysql-bin //[必須]啟用二進制日志
server-id=139 //[必須]服務器唯一ID,默認是1,一般取IP最后一段
2、重啟mysql
sudo service mysql restart
3、配置從服務器Slave:
master_log_pos是上面Position的值
mysql> change master to master_host='192.168.245.140',master_user='mysync',master_password='sD9854fals87s34GB98s',master_log_file='mysql-bin.000014',master_log_pos=318;
# 注意不要斷開,"318"無單引號。
mysql>start slave;
# 啟動從服務器復制功能
4、檢查從服務器復制功能狀態:
復制代碼
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.245.140
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 5669
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 5482
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 5669
Relay_Log_Space: 5639
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 140
1 row in set (0.00 sec)
注:Slave_IO及Slave_SQL進程必須正常運行,即YES狀態,否則都是錯誤的狀態。
三、主從服務器測試
主服務器Mysql,建立數據庫,并在這個庫中建表插入一條數據,觀看從庫是否也增加了相應的數據庫、數據表、數據。
四、清除Slave的配置信息
有時候我們的有許多臺Slave庫,根據業務需要一臺甚至幾臺不在使用需要下線了.這時候就不在需要從Master同步數據了.那么我們如何清理Slave:
reset slave all;
四、常見錯誤
可以從Last_IO_Error和Last_SQL_Error配置。
1、
service mysql status
如果出現如下錯誤需要配置skip-name-resolve參數,作用是不再進行反解析(ip不反解成域名),這樣可以加快數據庫的反應時間。:
Nov 16 19:26:28 iZ2zedt5e7ronlirnw6vl3Z mysqld[22185]: 181116 19:26:28 [Warning] IP address '180.76.0.0' could not be resolved: Name or service not known
修改配置文件添加如下代碼并重啟:
[mysqld]
skip-name-resolve
2、在沒有停止slave進程的情況下change master
mysql> change master to master_host='IP', master_user='USER', master_password='PASSWD', master_log_file='mysql-bin.000001',master_log_pos=106;
ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first
首先需要執行STOP SLAVE停止
3、slave沒有啟動
執行start slave啟動從服務器復制功能:
# show slave status\G
----------
Slave_IO_Running: No
Slave_SQL_Running: No
4、Got fatal error 1236 from master when reading data from binary log
5、Got fatal error 1236 from master when reading data from binary log: 'binlog truncated in the middle of event; consider out of disk space on master; the first event 'mysql-bin.000017' at 1367, the last event read from 'mysql-bin.000017' at 1367, the last byte read from 'mysql-bin.000017' at 1368.'
解決方案:
stop slave;
下方的master_log_file ,master_log_pos是前方讓記住的文件位置,一定不能錯
MariaDB [test1]> CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000001',MASTER_LOG_POS=329;
Query OK, 0 rows affected (0.072 sec)
6、Got fatal error 1236 from master when reading data from binary log: 'binlog truncated in the middle of event; consider out of disk space on master; the first event 'mysql-bin.000017' at 1367, the last event read from 'mysql-bin.000017' at 1367, the last byte read from 'mysql-bin.000017' at 1368.'
使用如下代碼刷新master的配置,然后根據上方更新一下Slave
flush logs;
show master status;
總結
以上是生活随笔為你收集整理的mysql主从切换机制torch_MySQL Replication设置(Master/Slave)实现主从复制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 安装盒子_Windows20
- 下一篇: 甜蜜暖心的哄老婆的句子238个