MySQL5.6主從復(fù)制(mysql數(shù)據(jù)同步配置)
規(guī)劃
master 192.168.56.2
slave 192.168.56.5
1、在主庫(kù)上,設(shè)置一個(gè)復(fù)制使用的賬戶rep1,并授予REPLICATION SLAVE權(quán)限。
mysql> grant replication slave on *.* to 'rep1'@'192.168.56.2' identified by '123456'; Query OK, 0 rows affected (0.01 sec)
2、修改主數(shù)據(jù)庫(kù)服務(wù)器的配置文件my.cnf,開啟BINLOG,并設(shè)置server-id的值。這兩個(gè)參數(shù)的修改需要重新啟動(dòng)數(shù)據(jù)庫(kù)服務(wù)才可以生效。
vi /etc/my.cnf
[mysqld]
log-bin=/home/mysql/log/mysql-bin.log
server-id=1
[root@rhel6 ~]# service mysql restart Shutting down MySQL.. [ OK ] Starting MySQL. [ OK ]
3、在主庫(kù)上,設(shè)置讀鎖定有效,這個(gè)操作是為了確保沒(méi)有數(shù)據(jù)庫(kù)操作,以便獲得一個(gè)一致性的快照。
mysql> flush tables with read lock; Query OK, 0 rows affected (0.02 sec)
4、得到主庫(kù)上當(dāng)前的二進(jìn)制日志名和偏移量值。這個(gè)操作的目的是為了從數(shù)據(jù)庫(kù)啟動(dòng)以后,從這個(gè)點(diǎn)開始進(jìn)行數(shù)據(jù)的恢復(fù)。
mysql> show master status; +-----------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-----------------+----------+--------------+------------------+-------------------+ | mysqlbin.000021 | 120 | | | | +-----------------+----------+--------------+------------------+-------------------+ 1 row in set (0.03 sec)
5、現(xiàn)在主數(shù)據(jù)庫(kù)服務(wù)器已經(jīng)停止了更新操作,需要生成主數(shù)據(jù)庫(kù)的備份,備份的方式有很多種,可以直接在操作系統(tǒng)下復(fù)制全部的數(shù)據(jù)文件到從數(shù)據(jù)庫(kù)服務(wù)器上,也可以通過(guò)mysqldump導(dǎo)出數(shù)據(jù)或者使用ibbackup工具進(jìn)行數(shù)據(jù)庫(kù)的備份。如果主數(shù)據(jù)庫(kù)的服務(wù)可以停止,那么直接復(fù)制數(shù)據(jù)文件應(yīng)該是最快的生成快照的方法:
[root@rhel6 lib]# tar -zcvf mysql.tar.gz mysql mysql/ tar: mysql/mysql.sock: socket ignored mysql/mysqlbin.000019 mysql/test1/ mysql/test1/db.opt mysql/test1/pack.MYD mysql/test1/myisam2.frm mysql/test1/emp.ibd mysql/test1/mer_myisam.MRG mysql/test1/emp.frm mysql/test1/pri_t.TRG .... 傳到從數(shù)據(jù)庫(kù) [root@rhel6 lib]# scp mysql.tar.gz root@192.168.56.5:/root root@192.168.56.5's password: mysql.tar.gz 100% 1402KB 1.4MB/s 00:00
6、主數(shù)據(jù)庫(kù)備份完畢后可恢復(fù)寫操作,剩下的只需要在從庫(kù)上執(zhí)行
mysql> unlock tables; Query OK, 0 rows affected (0.02 sec)
7、將主數(shù)據(jù)庫(kù)的一致性備份恢復(fù)到從數(shù)據(jù)庫(kù)上。如果是使用.tar.gz打包的文件包,只需要解開到相應(yīng)的目錄即可。
tar -zxvf mysql.tar.gz
8、修改從數(shù)據(jù)庫(kù)的配置文件my.cnf,增加server-id參數(shù)。注意server-id的值必須是唯一的,不能和主數(shù)據(jù)庫(kù)的配置相同,如果有多個(gè)從數(shù)據(jù)庫(kù)服務(wù)器,每個(gè)從數(shù)據(jù)庫(kù)服務(wù)器必須有自己唯一的server-id值。
vi my.cnf
[mysqld]
server-id=2
9、在從庫(kù)上,使用--skip-slave-start選項(xiàng)啟動(dòng)數(shù)據(jù)庫(kù),這樣不會(huì)立即啟動(dòng)從數(shù)據(jù)庫(kù)服務(wù)上的復(fù)制進(jìn)程,方便我們對(duì)從數(shù)據(jù)庫(kù)的服務(wù)進(jìn)行進(jìn)一步的配置(可選)
mysqld_safe --skip-slave-start &
或者修改my.cnf,添加skip-slave-start參數(shù),service mysql start
10、對(duì)從數(shù)據(jù)庫(kù)服務(wù)器做相應(yīng)設(shè)置,指定復(fù)制使用的用戶,主數(shù)據(jù)庫(kù)服務(wù)器的IP、端口以及開始執(zhí)行復(fù)制的日志文件和位置等
mysql> change master to -> master_host='192.168.56.2', -> master_user='rep1', -> master_port=3306, -> master_password='123456', -> master_log_file='mysqlbin.000021', -> master_log_pos=120; Query OK, 0 rows affected, 2 warnings (0.08 sec)
11、在從庫(kù)上,啟動(dòng)slave線程
mysql> start slave; Query OK, 0 rows affected (0.17 sec)
12、從庫(kù)上查看進(jìn)程
mysql> show processlist \G; *************************** 1. row *************************** Id: 1 User: root Host: localhost db: zx Command: Query Time: 0 State: init Info: show processlist *************************** 2. row *************************** Id: 4 User: system user Host: db: NULL Command: Connect Time: 1484 State: Waiting for master to send event Info: NULL *************************** 3. row *************************** Id: 5 User: system user Host: db: NULL Command: Connect Time: 739 State: Slave has read all relay log; waiting for the slave I/O thread to update it Info: NULL 3 rows in set (0.00 sec) mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.56.2 Master_User: rep1 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysqlbin.000021 Read_Master_Log_Pos: 652 Relay_Log_File: rhel6-relay-bin.000002 Relay_Log_Pos: 814 Relay_Master_Log_File: mysqlbin.000021 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: 652 Relay_Log_Space: 987 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: 1 Master_UUID: 3743271b-aa6d-11e6-aa2e-080027e5f5dd Master_Info_File: /mysqldata/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)
Slave_IO_Running和Slave_SQL_Running全部為YES才算搭建成功。如果出錯(cuò)查看報(bào)錯(cuò)原因。我搭建時(shí)遇到的兩個(gè)錯(cuò)誤:
(1)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.
原因:搭建時(shí)把主庫(kù)整個(gè)目錄復(fù)制過(guò)來(lái)的,主庫(kù)和從庫(kù)的UUID一致了,修改從庫(kù)的UUID即可。
方法:修改$datadir/auto.cnf,按照16進(jìn)制格式隨便改一下,重啟mysql即可。
(2)Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
原因:配置change master to時(shí)把master_log_file配置錯(cuò)誤
方法:change master to master_log_file='mysqlbin.000021';start slave;即可。
13、測(cè)試復(fù)制的正確性
主庫(kù) mysql> create database zx; Query OK, 1 row affected (0.05 sec) mysql> use zx; Database changed mysql> create table t(id int); Query OK, 0 rows affected (0.09 sec) mysql> insert into t values(1),(2),(3); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 從庫(kù) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | data | | mysql | | performance_schema | | test | | test1 | | zx | +--------------------+ 7 rows in set (0.00 sec) mysql> use zx Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from t; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ 3 rows in set (0.00 sec)
相關(guān)文章
關(guān)于數(shù)據(jù)庫(kù)中保留小數(shù)位的問(wèn)題
本篇文章是對(duì)關(guān)于在數(shù)據(jù)庫(kù)中保留小數(shù)位的問(wèn)題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07
MYSQL實(shí)現(xiàn)排名及查詢指定用戶排名功能(并列排名功能)實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于MYSQL實(shí)現(xiàn)排名及查詢指定用戶排名功能(并列排名功能)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-10-10
MySQL與存儲(chǔ)過(guò)程的相關(guān)資料
這篇文章主要介紹了MySQL與存儲(chǔ)過(guò)程的相關(guān)資料,需要的朋友可以參考下2007-03-03
MySQL數(shù)據(jù)庫(kù)數(shù)據(jù)視圖
這篇文章主要介紹了MySQL數(shù)據(jù)庫(kù)數(shù)據(jù)視圖,視圖是原始數(shù)據(jù)庫(kù)數(shù)據(jù)的一種變換,是查看表中數(shù)據(jù)的另外一種方式,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08
MySQL存儲(chǔ)過(guò)程輸入?yún)?shù)(in),輸出參數(shù)(out),輸入輸出參數(shù)(inout)
這篇文章主要介紹了MySQL存儲(chǔ)過(guò)程輸入?yún)?shù)(in),輸出參數(shù)(out),輸入輸出參數(shù)(inout),存儲(chǔ)過(guò)程就是一組SQL語(yǔ)句集,功能強(qiáng)大,可以實(shí)現(xiàn)一些比較復(fù)雜的邏輯功能,類似于JAVA語(yǔ)言中的方法;Python里面的函數(shù)2022-07-07
GDB調(diào)試Mysql實(shí)戰(zhàn)之源碼編譯安裝
今天小編就為大家分享一篇關(guān)于GDB調(diào)試Mysql實(shí)戰(zhàn)之源碼編譯安裝,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
MySql總彈出mySqlInstallerConsole窗口的解決方法
這篇文章主要介紹了MySql總彈出mySqlInstallerConsole窗口的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
在MySQL中用正則表達(dá)式替換數(shù)據(jù)庫(kù)中的內(nèi)容的方法
在MySQL中用正則表達(dá)式替換數(shù)據(jù)庫(kù)中的內(nèi)容的方法...2007-03-03

