新聞中心
環(huán)境:

成都創(chuàng)新互聯(lián)公司2013年開(kāi)創(chuàng)至今,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元蕭山做網(wǎng)站,已為上家服務(wù),為蕭山各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):028-86922220
Mater: centos7.1 5.5.52-MariaDB 192.168.108.133
Slave: CentOS7.1 5.5.52-MariaDB 192.168.108.140
1.導(dǎo)出主服務(wù)數(shù)據(jù),將主備初始數(shù)據(jù)同步
master:
//從master上導(dǎo)出需要同步的數(shù)據(jù)庫(kù)信息 mysqldump -u*** -p*** --database test > test.sql //將master上的備份信息傳輸?shù)絪lave上 scp /root/test.sql [email protected]:/opt/
slave:
//進(jìn)入slave的數(shù)據(jù)庫(kù) mysql -u*** -p*** //清空test數(shù)據(jù)庫(kù) drop database test //導(dǎo)入master的test數(shù)據(jù)庫(kù)信息 source /opt/test.sql
2.配置master和slave上的mysql數(shù)據(jù)庫(kù)
master:
//修改master的my.cnf文件 vim /etc/my.cnf //master配置如下,在[mysqld]下添加如下配置 #log-bin server-id = 1 log_bin = master-bin expire_logs_days = 10 max_binlog_size = 100M binlog-do_db = test binlog_ignore_db = mysql //重啟mysql數(shù)據(jù)庫(kù) service mysqld restart //如果安裝的是mariadb可以重啟mariadb systemctl restart mariadb.service
slave:
//修改slave的my.cnf文件 vim /etc/my.cnf //slave配置如下,在[mysqld]下添加如下配置 server-id = 2 //重啟mysql數(shù)據(jù)庫(kù) service mysqld restart //如果安裝的是mariadb可以重啟mariadb systemctl restart mariadb.service
簡(jiǎn)單說(shuō)明一下參數(shù)配置,保證主備server-id唯一。在master上需要開(kāi)啟mysql的binlog,log_bin=master_bin,指定binlog文件的名稱(chēng)。
3.創(chuàng)建一個(gè)復(fù)制用戶(hù),具有replication slave 權(quán)限,能保證slave能把master的數(shù)據(jù)同步過(guò)去
master:
grant replication slave on *.* to 'replication'@'192.168.108.140' identified by 'replication';
4.獲取master的binlog位置
master:
//進(jìn)入mysql數(shù)據(jù)庫(kù) mysql -u*** -p*** //設(shè)置讀鎖 flush tables with read lock; //獲取mysql的binlog文件信息和偏移量 show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000010 | 3713 | test | mysql | +-------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) //解鎖 unlock tables;
5.設(shè)置備端數(shù)據(jù)庫(kù)
//進(jìn)入mysql數(shù)據(jù)庫(kù)
mysql -u*** -p***
//停止slave
stop slave;
//設(shè)置對(duì)應(yīng)master的binlog信息
MariaDB [(none)]> change master to
-> master_host='192.168.108.133',
-> master_user='replication',
-> master_password='replication',
-> master_log_file='master-bin.000010',
-> master_log_pos=3713;
//啟動(dòng)slave
start slave;
6.查看備端狀態(tài)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.108.133
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000010
Read_Master_Log_Pos: 3881
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 698
Relay_Master_Log_File: master-bin.000010
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: 3881
Relay_Log_Space: 994
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
1 row in set (0.00 sec)
ERROR: No query specified
如果:Slave_IO_Running: Yes,Slave_SQL_Running: Yes則為配置成功,配置錯(cuò)誤重復(fù)上面操作即可。如果解決不了可通過(guò)查看mysql日志分析處理。
vim /var/log/mariadb/mariadb.log
7.測(cè)試。其實(shí)測(cè)試沒(méi)啥好寫(xiě)的,配置成功之后直接連到主從數(shù)據(jù)庫(kù),在master上改變表、字段、數(shù)據(jù),slave會(huì)同步變化。
寫(xiě)在最后:當(dāng)時(shí)想的試一試能不能用mysql自帶的功能做數(shù)據(jù)庫(kù)災(zāi)備,后來(lái)發(fā)現(xiàn)mysql數(shù)據(jù)庫(kù)主從同步會(huì)有一些問(wèn)題。第一個(gè)不好腳本化的東西是在同步之前需保證兩邊的數(shù)據(jù)庫(kù)初始信息一樣,因?yàn)閭涠伺渲玫膍ysql-binlog位置只是當(dāng)前主數(shù)據(jù)庫(kù)信息的位置,在該位置之前的數(shù)據(jù)只能通過(guò)人工導(dǎo)入。第二個(gè)就是mysql主從同步時(shí),只能進(jìn)行數(shù)據(jù)庫(kù)的增量同步,不能進(jìn)行全量同步;還有就是如果備端出現(xiàn)臟數(shù)據(jù),多了一條數(shù)據(jù),當(dāng)主那邊新增一條主鍵相同的數(shù)據(jù),則同步失敗。之后我會(huì)嘗試的能不能把這些操作腳本化,發(fā)現(xiàn)mysql自帶的同步功能限制性很大,并且手工干預(yù)的東西太多了。
分享名稱(chēng):CentOS7.1下MySQL數(shù)據(jù)庫(kù)主從同步
分享路徑:http://m.fisionsoft.com.cn/article/dpjogop.html


咨詢(xún)
建站咨詢(xún)
