MySQL5.6 數(shù)據(jù)庫主從同步安裝與配置詳解(Master/Slave)
MySQL5.6 數(shù)據(jù)庫主從同步安裝與配置詳解(Master/Slave)
本篇文章主要介紹了MySQL5.6 數(shù)據(jù)庫主從同步安裝與配置詳解,具有一定的參考價(jià)值,有興趣的可以了解一下。
安裝環(huán)境
操作系統(tǒng) :CentOS 6.5
數(shù)據(jù)庫版本:MySQL 5.6.27
主機(jī)A:192.168.1.1 (Master)
主機(jī)B:192.168.1.2 (Slave)
這里強(qiáng)調(diào)的數(shù)據(jù)庫的版本,是因?yàn)镸ySQL在5.6之前和之后的安裝方式是不一樣的。
本人在進(jìn)行配置的時(shí)候,也遇到了這個(gè)坑,這里提前說明,希望大家不要采坑。
注:這里有一篇CentOS安裝MySQL的文章,在這里引出,過程親測,希望對大家有幫助:http://www.dbjr.com.cn/article/101778.htm
基本環(huán)境配置
首先,要保證防火墻對3306端口的開啟,如果只是為了學(xué)習(xí)數(shù)據(jù)庫的主從配置,可以使用service iptables stop 命令直接關(guān)閉防火墻。
然后可以在兩臺機(jī)子之間進(jìn)行 ping操作,確保兩臺機(jī)器之間能夠相同。
Master的配置
在Linux環(huán)境下MySQL的配置文件的位置是在 /etc/my.cnf ,在該文件下指定Master的配置如下:
log-bin=mysql-bin server-id=2 binlog-ignore-db=information_schema binlog-ignore-db=cluster binlog-ignore-db=mysql binlog-do-db=ufind_db
這里的server-id用于標(biāo)識唯一的數(shù)據(jù)庫,這里設(shè)置為2,在設(shè)置從庫的時(shí)候就需要設(shè)置為其他值。
binlog-ignore-db:表示同步的時(shí)候ignore的數(shù)據(jù)庫
binlog-do-db:指定需要同步的數(shù)據(jù)庫
完整配置截圖如下:
1、然后重啟mysql:
service mysqld restart
2、進(jìn)入mysql:[root@VM_221_4_centos ~]# mysql -u root -p 回車,輸入mysql密碼進(jìn)入。
3、 賦予從庫權(quán)限帳號,允許用戶在主庫上讀取日志,賦予192.168.1.2也就是Slave機(jī)器有File權(quán)限,只賦予Slave機(jī)器有File權(quán)限還不行,還要給它REPLICATION SLAVE的權(quán)限才可以。
在Master數(shù)據(jù)庫命令行中輸入:
>GRANT FILE ON *.* TO 'root'@'192.168.1.2' IDENTIFIED BY 'mysql password'; >GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.1.2' IDENTIFIED BY 'mysql password'; >FLUSH PRIVILEGES
這里使用的仍是 root 用戶作為同步的時(shí)候使用到的用戶,可以自己設(shè)定。
4、重啟mysql,登錄mysql,顯示主庫信息
mysql> show master status;
mysql> show master status; +------------------+----------+--------------+----------------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+----------------------------------+-------------------+ | mysql-bin.000004 | 28125 | ufind_db | information_schema,cluster,mysql | | +------------------+----------+--------------+----------------------------------+-------------------+ 1 row in set (0.00 sec) mysql>
這里的 File 、Position 是在配置Salve的時(shí)候要使用到的,Binlog_Do_DB表示要同步的數(shù)據(jù)庫,Binlog_Ignore_DB 表示Ignore的數(shù)據(jù)庫,這些都是在配置的時(shí)候進(jìn)行指定的。
另外:如果執(zhí)行這個(gè)步驟始終為Empty set(0.00 sec),那說明前面的my.cnf沒配置對。
Slave的配置
1、從庫的配置,首先也是修改配置文件:/etc/my.cnf 如下:
log-bin=mysql-bin server-id=3 binlog-ignore-db=information_schema binlog-ignore-db=cluster binlog-ignore-db=mysql replicate-do-db=ufind_db replicate-ignore-db=mysql log-slave-updates slave-skip-errors=all slave-net-timeout=60
2、這里可以看到,在MySQL5.6之后的版本中沒有指定:
master-host=192.168.1.1 #Master的主機(jī)IP master-user=root master-password=mysql password #Master的MySQL密碼
3、這也是在網(wǎng)上很多搜索的配置過程,他們也都指定了數(shù)據(jù)庫的版本,但是并沒有說出來新版本的配置這種方式是不適用的。
4、如果,你在MySQL5.6和之后的版本中配置從庫的時(shí)候,設(shè)置到了上邊的內(nèi)容,即指定了master-host、master-user等信息的話,重啟MySQL的時(shí)候就回報(bào)錯(cuò),錯(cuò)誤信息如下:
[root@VM_128_194_centos bin]# service mysqld restart Shutting down MySQL... SUCCESS! Starting MySQL... ERROR! The server quit without updating PID file (/data/mysqldb/VM_128_194_centos.pid). [root@VM_128_194_centos bin]#
此時(shí),查看數(shù)據(jù)庫的報(bào)錯(cuò)信息(數(shù)據(jù)庫的目錄, /data/mysqldb/VM_128_194_centos.err ),可以看到:
2016-05-06 13:12:04 13345 [Note] InnoDB: Waiting for purge to start 2016-05-06 13:12:04 13345 [Note] InnoDB: 5.6.27 started; log sequence number 2850211 2016-05-06 13:12:04 13345 [ERROR] /data/home/server/mysql-5.6.27/bin/mysqld: unknown variable 'master-host=192.168.1.1' 2016-05-06 13:12:04 13345 [ERROR] Aborting
可以看出master-host 被檢測數(shù)是一個(gè)未知的變量,因此會(huì)出現(xiàn)錯(cuò)誤。
5、在5.6以及后續(xù)版本的配置如下:
修改完/etc/my.cnf 文件之后,重啟一下MySQL(service mysqld restart)
進(jìn)入Slave mysql控制臺,執(zhí)行:
mysql> stop slave; #關(guān)閉Slave mysql> change master to master_host='192.168.1.1',master_user='root',master_password='123456',master_log_file='mysql-bin.000004', master_log_pos=28125; mysql> start slave; #開啟Slave
在這里指定Master的信息,master_log_file是在配置Master的時(shí)候的File選項(xiàng), master_log_pos是在配置Master的Position 選項(xiàng),這里要進(jìn)行對應(yīng)。
然后可以通過mysql> show slave status; 查看配置的信息:
mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.167.1.1 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 28125 Relay_Log_File: VM_128_194_centos-relay-bin.000004 Relay_Log_Pos: 26111 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: ufind_db 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: 28125 Relay_Log_Space: 26296 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: 2 Master_UUID: 8ac3066a-9680-11e5-a2ec-5254007529fd Master_Info_File: /data/mysqldb/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 mysql>
可以看到,已經(jīng)配置成功。
添加需要同步的從庫Slave
由于種種原因,測試的時(shí)候使用test庫,這里我按照上述的方式,修改Master的my.cnf的配置文件,新增同步的數(shù)據(jù)庫test,重啟MySQL,執(zhí)行Master的:show master status如下:
相應(yīng)的,要修改Slave從庫的信息在my.cnf 增加 replicate-do-db=test,重啟Mysql,根據(jù)上述的show master status,在Slave從庫中執(zhí)行下邊的內(nèi)容:
>stop slave >change master to master_host='192.168.1.1',master_user='root',master_password='123456',master_log_file='mysql-bin.000005', master_log_pos=120; >start slave
然后使用:show slave status;
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.1 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 1422 Relay_Log_File: VM_128_194_centos-relay-bin.000004 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000005 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: ufind_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: 1422 Relay_Log_Space: 468 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: 2 Master_UUID: 8ac3066a-9680-11e5-a2ec-5254007529fd Master_Info_File: /data/mysqldb/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 mysql>
已經(jīng)新增加了test。
真正的測試
在主庫中新增數(shù)據(jù)庫表,user,觀察從庫變化如下:
創(chuàng)建數(shù)據(jù)庫的時(shí)候:
新增數(shù)據(jù)的時(shí)候:
刪除Master數(shù)據(jù)庫表的時(shí)候:
配置過程,到此為止,希望能夠幫助大家,如有疑問 歡迎留言。也希望大家多多支持腳本之家。
- mysql同步問題之Slave延遲很大優(yōu)化方法
- 解決MySQL中的Slave延遲問題的基本教程
- MySQL中slave監(jiān)控的延遲情況分析
- mysql 主從數(shù)據(jù)不一致,提示: Slave_SQL_Running: No 的解決方法
- 記一次MySQL Slave庫恢復(fù)實(shí)戰(zhàn)記錄
- Mysql主從數(shù)據(jù)庫(Master/Slave)同步配置與常見錯(cuò)誤
- MySQL中slave_exec_mode參數(shù)詳解
- MySQL Slave 觸發(fā) oom-killer解決方法
- MySQL slave 延遲一列 外鍵檢查和自增加鎖
相關(guān)文章
SQL實(shí)現(xiàn)Excel的10個(gè)常用功能的示例詳解
SQL,數(shù)據(jù)分析崗的必備技能,你可以不懂Python,R,不懂可視化,不懂機(jī)器學(xué)習(xí)。但SQL,你必須懂。本文為大家總結(jié)了SQL實(shí)現(xiàn)Excel的10個(gè)常用功能的示例代碼,感興趣的可以了解一下2022-07-07淺談mysql中多表不關(guān)聯(lián)查詢的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄獪\談mysql中多表不關(guān)聯(lián)查詢的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10MySQL存儲引擎應(yīng)用場景MyISAM?vs?InnoDB優(yōu)勢選擇
這篇文章主要為大家介紹了MySQL存儲引擎應(yīng)用場景MyISAM?vs?InnoDB優(yōu)勢選擇,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10MySQL無法啟動(dòng)、無法停止解決方法(安全設(shè)置后容易出現(xiàn))
最近在Win2003上的MySQL出現(xiàn)過多次正常運(yùn)行時(shí)無法連接數(shù)據(jù)庫故障,根本原因就是因?yàn)榘踩O(shè)置以后容易出現(xiàn)的問題,其實(shí)很簡單的解決2012-03-03MYSQL實(shí)現(xiàn)添加購物車時(shí)防止重復(fù)添加示例代碼
在向mysql中插入數(shù)據(jù)的時(shí)候最需要注意的就是防止重復(fù)發(fā)添加數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于MYSQL如何實(shí)現(xiàn)添加購物車的時(shí)候防止重復(fù)添加的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09安裝配置mysql及Navicat prenium的詳細(xì)流程
這篇文章主要介紹了安裝配置mysql及Navicat Premium的詳細(xì)流程,配置方法也真的很簡單,本文給大家詳細(xì)介紹mysql Navicat Premium安裝配置相關(guān)知識感興趣的朋友,一起學(xué)習(xí)吧2021-06-06