MySQL主從復(fù)制配置詳解
點(diǎn)擊關(guān)注上方“SQL數(shù)據(jù)庫(kù)開(kāi)發(fā)”,
設(shè)為“置頂或星標(biāo)”,第一時(shí)間送達(dá)干貨 SQL專欄 SQL基礎(chǔ)知識(shí)第二版
SQL高級(jí)知識(shí)第二版
之前很多小伙伴想知道MySQL主從復(fù)制的配置步驟,今天它來(lái)了。帶著你可能碰到的各種異常來(lái)了。
配置環(huán)境
操作系統(tǒng):兩臺(tái)CentOS 7.6的Linux系統(tǒng)
數(shù)據(jù)庫(kù)版本:MySQL 5.6.39
主服務(wù)器IP:192.168.0.1
從服務(wù)器IP:192.168.0.2
安裝數(shù)據(jù)庫(kù)
之前已經(jīng)給小伙伴們?cè)敿?xì)的講解了CentOS安裝MySQL的操作步驟了,還沒(méi)看過(guò)的小伙伴可以戳這里:
配置前提
1、需要保證3306端口開(kāi)啟或關(guān)閉防火墻,在MySQL的安裝里有介紹。
--在192.168.0.2上輸入ping命令
ping 192.168.0.1
--在192.168.0.1上輸入ping命令
ping 192.168.0.23、安裝成功一臺(tái)MySQL后,使用虛擬機(jī)克隆一臺(tái)作為從服務(wù)器
配置主(Master)數(shù)據(jù)庫(kù)
1、修改數(shù)據(jù)庫(kù)配置文件
[root@localhost ~]# vi /etc/my.cnf將里面的內(nèi)容修改為
[mysqld]
#開(kāi)啟二進(jìn)制日志
log-bin=mysql-bin
#標(biāo)識(shí)唯一id(必須),一般使用ip最后位
server-id=1
#不同步的數(shù)據(jù)庫(kù),可設(shè)置多個(gè)
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
#指定需要同步的數(shù)據(jù)庫(kù)(和slave是相互匹配的),可以設(shè)置多個(gè)
binlog-do-db=test添加日志存儲(chǔ)方式和規(guī)則(選填)
#設(shè)置存儲(chǔ)模式不設(shè)置默認(rèn)
binlog_format=MIXED
#日志清理時(shí)間
expire_logs_days=7
#日志大小
max_binlog_size=100m
#緩存大小
binlog_cache_size=4m
#最大緩存大小
max_binlog_cache_size=521m注:日志的存儲(chǔ)容量我設(shè)置的都比較小,當(dāng)然你可以根據(jù)實(shí)際情況修改得大一點(diǎn)。
service mysqld restartThe server quit without updating PID file......
ps -ef|grep mysqld[root@localhost ~]# mysql -u root -p#給從庫(kù)放權(quán)限
mysql>GRANT FILE ON *.* TO 'root'@'192.168.0.2' IDENTIFIED BY 'root password'; #創(chuàng)建用戶
mysql>GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.0.2' IDENTIFIED BY 'root password'; #修改用戶權(quán)限
mysql>select host ,user ,password from mysql.user; #查看是否修改成功
mysql>FLUSH PRIVILEGES; #刷新權(quán)限4、重啟MySQL服務(wù),登錄MySQL,查看主庫(kù)信息
[root@localhost ~]# service mysqld restart #重啟mysql服務(wù)
[root@localhost ~]# mysql -u root -p #登陸mysql
mysql> show master status; #查看master狀態(tài)顯示大概如下內(nèi)容
+------------------+----------+--------------+----------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+----------------------------------+-------------------+
| mysql-bin.000006 | 120 | ufind_db | information_schema,performance_schema,mysql | |
+------------------+----------+--------------+----------------------------------+-------------------+
1 row in set (0.00 sec)Empty set(0.00 sec),那說(shuō)明前面的my.cnf沒(méi)配置對(duì),請(qǐng)回去重新檢查配置步驟。配置從(Slave)數(shù)據(jù)庫(kù)
[root@localhost ~]# vi /etc/my.cnf將里面的內(nèi)容修改為
#開(kāi)啟二進(jìn)制日志
log-bin=mysql-bin
server-id=2
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
#與主庫(kù)配置保持一致
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=602、重啟MySQL服務(wù),登錄MySQL
[root@localhost ~]# service mysqld restart
[root@localhost ~]# mysql -u root -p并作如下修改:
#關(guān)閉Slave
mysql> stop slave; #設(shè)置連接主庫(kù)信息
mysql> change master to master_host='192.168.0.1',master_user='root',master_password='root password',master_log_file='mysql-bin.000006', master_log_pos=120;
#開(kāi)啟Slave
mysql> start slave;注:上面的master_log_file是在配置Master的時(shí)候的File字段, master_log_pos是在配置Master的Position 字段。一定要一一對(duì)應(yīng)
mysql> show slave status \G;成功的話會(huì)顯示如下信息:
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.1
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 120
Relay_Log_File: localhost-relay-bin.000006
Relay_Log_Pos: 520
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes //顯示yes為成功
Slave_SQL_Running: Yes //顯示yes為成功,如果為no,一般為沒(méi)有啟動(dòng)master
Replicate_Do_DB: test
Replicate_Ignore_DB: mysql//上面的都是配置文件中的信息
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: 357
Relay_Log_Space: 697
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: //如果為no,此處會(huì)顯示錯(cuò)誤信息
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2
Master_UUID: be0a41c0-2b40-11e8-b791-000c29267b6a
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
ERROR:
No query specified注:如果Slave_IO_Running: No并且出現(xiàn)下面的錯(cuò)誤
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
mysql>select UUID();[root@localhost ~]# vi /usr/local/mysql/data/auto.cnfGot fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
mysql>stop slave; //停止
mysql>reset slave; //復(fù)位
mysql>start slave; //開(kāi)啟至此整個(gè)過(guò)程就配置好了。
可能有小伙伴會(huì)問(wèn),這些配置文件我都配好了,信息也和你的一樣,我還是不確定是否配置成功。
那么你可以在主服務(wù)器上創(chuàng)建一個(gè)表,然后在從服務(wù)器上查詢剛創(chuàng)建的這個(gè)表,看是否存在就可以啦。
Tips
#select 語(yǔ)句,暫時(shí)沒(méi)有發(fā)現(xiàn)問(wèn)題
#insert 語(yǔ)句,暫時(shí)沒(méi)有發(fā)現(xiàn)問(wèn)題
#update 語(yǔ)句,暫時(shí)沒(méi)有發(fā)現(xiàn)問(wèn)題
#delete 語(yǔ)句,主庫(kù)刪除多條數(shù)據(jù),發(fā)現(xiàn)數(shù)據(jù)不一致#查看binlog全部文件
mysql>show binary logs;
#查看binlog是否開(kāi)啟NO為開(kāi)啟
mysql> show variables like 'log_bin%';
#詳細(xì)信息
mysql> show variables like 'binlog%';
#查看binlog日志
mysql> show binlog events in'mysql-bin.000019';
#或者使用mysqlbinlog,如果報(bào)錯(cuò)使用--no-defaults(使用全路徑)
[root@localhost ~]# /usr/local/mysql/bin/mysqlbinlog --no-defaults /usr/local/mysql/data/mysql-bin.000019#手動(dòng)刷新日志
mysql> show master status;
#刪除全部
mysql> reset slave;或 rest master;
#刪除MySQL-bin.004
mysql> PURGE MASTER LOGS TO 'MySQL-bin.004';此外,如果你在修改最大連接數(shù)時(shí),可能會(huì)存在已經(jīng)將mysql配置文件的連接數(shù)改成1000或更大,但是查詢數(shù)據(jù)庫(kù)的最大連接數(shù)始終都是214,可以嘗試如下方法:
https://www.cnblogs.com/brucetang/p/9733998.html
https://javawind.net/p141 https://www.cnblogs.com/brucetang/p/9733998.html
最后給大家分享我寫的SQL兩件套:《SQL基礎(chǔ)知識(shí)第二版》和《SQL高級(jí)知識(shí)第二版》的PDF電子版。里面有各個(gè)語(yǔ)法的解釋、大量的實(shí)例講解和批注等等,非常通俗易懂,方便大家跟著一起來(lái)實(shí)操。 有需要的可以下載學(xué)習(xí),只需要在下面的公眾號(hào)「數(shù)據(jù)前線」(非本號(hào)),后臺(tái)回復(fù)關(guān)鍵字:SQL,就行 數(shù)據(jù)前線
后臺(tái)回復(fù)關(guān)鍵字:1024,獲取一份精心整理的技術(shù)干貨 后臺(tái)回復(fù)關(guān)鍵字:進(jìn)群,帶你進(jìn)入高手如云的交流群。 推薦閱讀

