? mysql主從呢栏妖,聽(tīng)就聽(tīng)得多了應(yīng)該不難,但動(dòng)手做還是第一次,做起來(lái)發(fā)現(xiàn)還是出了點(diǎn)小問(wèn)題耽誤了一下攒驰,這里做一下記錄。
先記錄問(wèn)題:
slave_io_running: no
解決方法:
1故爵、查mysql的error.log日志
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 5.6的復(fù)制引入了uuid的概念玻粪,各個(gè)復(fù)制結(jié)構(gòu)中的server_uuid得保證不一樣隅津,但是查看到直接copy? data文件夾后server_uuid是相同的,show variables like '%server_uuid%';
解決方法:
找到data文件夾下的auto.cnf文件劲室,修改里面的uuid值伦仍,保證各個(gè)db的uuid不一樣,重啟db即可
2很洋、配置文件my.cnf里面server.id一樣
原因分析:
和server_uuid類似充蓝,servier_id也得保證不一樣
解決方法:
找到my.cnf配置文件中的server_id,修改從庫(kù)的server_id保證和復(fù)制結(jié)構(gòu)中的其他db不一樣喉磁,重啟db即可
3谓苟、在配置slave同步時(shí)因?yàn)閟lave訪問(wèn)master沒(méi)有權(quán)限導(dǎo)致;
注意用戶權(quán)限
4协怒、master上的mysql-bin.xxxxxx文件全被誤刪除了涝焙;
解決方法:
重建主從
master:
重啟master庫(kù):service mysqld restart
mysql> show master status;
slave:
mysql> slave stop;
mysql> change master to Master_Log_File='mysql-bin.000001',Master_Log_Pos=98;
mysql> slave start;
一、基礎(chǔ)環(huán)境
Centos6.5版本
master:192.168.2.52
slave: 192.168.2.65
注意下面幾點(diǎn):
1)防火墻開(kāi)放3306端口孕暇。
2)關(guān)閉selinux仑撞。
3)同步前,雙方數(shù)據(jù)庫(kù)中需要同步的數(shù)據(jù)要保持一致妖滔。這樣隧哮,同步環(huán)境實(shí)現(xiàn)后,再次更新的數(shù)據(jù)就會(huì)如期同步了座舍。
二近迁、搭建過(guò)程
在master上面新建庫(kù)測(cè)試,這里只同步一個(gè)庫(kù)
mysql> CREATE DATABASE center CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
mysql> use center;
Database changed
mysql> create table if not exists users (id int(10) PRIMARY KEY AUTO_INCREMENT,name varchar(50) NOT NULL);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into users values(1,"zhangsan"),(2,"lisi");
Query OK, 2 rows affected (0.00 sec)
Records: 2? Duplicates: 0? Warnings: 0
mysql> select * from users;
+----+-----------+
| id | name? ? ? |
+----+-----------+
|? 1 |? zhangsan |
|? 2 |? ? lisi? |
+----+-----------+
2 rows in set (0.00 sec)
下面是master數(shù)據(jù)庫(kù)上的操作:
1)設(shè)置master數(shù)據(jù)庫(kù)的my.cnf文件(在[mysqld]配置區(qū)域添加下面內(nèi)容)
[root@master ~]# vim /usr/local/mysql/my.cnf
server-id=1? ? ? ? #數(shù)據(jù)庫(kù)唯一ID簸州,主從的標(biāo)識(shí)號(hào)絕對(duì)不能重復(fù)鉴竭。
log-bin=mysql-bin? ? #開(kāi)啟bin-log,并指定文件目錄和文件名前綴
binlog-do-db=center? #需要同步的數(shù)據(jù)庫(kù)岸浑。如果是多個(gè)同步庫(kù)搏存,就以此格式另寫幾行即可。如果不指明對(duì)某個(gè)具體庫(kù)同步矢洲,就去掉此行璧眠,表示同步所有庫(kù)(除了ignore忽略的庫(kù))。
binlog-ignore-db=mysql? #不同步mysql系統(tǒng)數(shù)據(jù)庫(kù)读虏。如果是多個(gè)不同步庫(kù)责静,就以此格式另寫幾行;也可以在一行盖桥,中間逗號(hào)隔開(kāi)灾螃。
sync_binlog = 1? ? ? #確保binlog日志寫入后與硬盤同步
binlog_checksum = none? #跳過(guò)現(xiàn)有的采用checksum的事件,mysql5.6.5以后的版本中binlog_checksum=crc32,而低版本都是binlog_checksum=none
binlog_format = mixed? #bin-log日志文件格式揩徊,設(shè)置為MIXED可以防止主鍵重復(fù)腰鬼。
2)導(dǎo)出master數(shù)據(jù)庫(kù)嵌赠,導(dǎo)入到slave數(shù)據(jù)庫(kù)中。保證雙方在同步環(huán)境實(shí)現(xiàn)前的數(shù)據(jù)一致熄赡。
導(dǎo)出數(shù)據(jù)庫(kù)之前先鎖定數(shù)據(jù)庫(kù)
mysql> flush tables with read lock;? ? #數(shù)據(jù)庫(kù)只讀鎖定命令姜挺,防止導(dǎo)出數(shù)據(jù)庫(kù)的時(shí)候有數(shù)據(jù)寫入。unlock tables命令解除鎖定
mysqldump -uroot -p密碼 center > /opt/center.sql
scp center.sql root@192.168.2.65:/opt/ #將導(dǎo)出的sql文件上傳到slave機(jī)器上
3)設(shè)置數(shù)據(jù)同步權(quán)限
mysql> grant replication slave,replication client on *.* to slave@'192.168.2.65' identified by "123456";
Query OK, 0 rows affected (0.02 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
4)查看主服務(wù)器master狀態(tài)(注意File與Position項(xiàng)彼硫,從服務(wù)器需要這兩項(xiàng)參數(shù))
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File? ? ? ? ? ? | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000009 |? ? ? 721 |? center? ? ? |? ? ? ? ? ? ? ? ? |? ? ? ? ? ? ? ? ? |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
下面是slave數(shù)據(jù)庫(kù)上的操作:
1)設(shè)置slave數(shù)據(jù)庫(kù)的my.cnf配置文件
[root@master ~]# vim /usr/local/mysql/my.cnf
.......
server-id=2? #設(shè)置從服務(wù)器id炊豪,必須于主服務(wù)器不同
log-bin=mysql-bin? #啟動(dòng)MySQ二進(jìn)制日志系統(tǒng)
replicate-do-db=center? #需要同步的數(shù)據(jù)庫(kù)名。如果不指明同步哪些庫(kù)拧篮,就去掉這行词渤,表示所有庫(kù)的同步(除了ignore忽略的庫(kù))。
replicate-ignore-db=mysql? #不同步mysql系統(tǒng)數(shù)據(jù)庫(kù)
slave-skip-errors = all? #跳過(guò)所有的錯(cuò)誤錯(cuò)誤他托,繼續(xù)執(zhí)行復(fù)制操作
2)在slave數(shù)據(jù)庫(kù)中導(dǎo)入從master傳過(guò)來(lái)的數(shù)據(jù)掖肋。
mysql -uroot -p密碼 -e "create database if not exists center;"
mysql -uroot -p密碼 center < /opt/center.sql
3)配置主從同步指令
mysql> stop slave;? #執(zhí)行同步前,要先關(guān)閉slave
mysql> change? master to master_host='192.168.2.52',master_user='slave',master_password='123456',master_log_file='mysql-bin.000009',master_log_pos=721;
mysql> start slave;
mysql> show slave status \G;
.......
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.52
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 721
Relay_Log_File: mysql-relay-bin.000009
Relay_Log_Pos: 721
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: center
Replicate_Ignore_DB: mysql
.............
Seconds_Behind_Master: 0
如上赏参,當(dāng)IO和SQL線程的狀態(tài)均為Yes志笼,則表示主從已實(shí)現(xiàn)同步了!
下面測(cè)試下Mysql主從同步的效果
現(xiàn)在master數(shù)據(jù)庫(kù)上寫入新數(shù)據(jù)
mysql> unlock tables;? ? #解鎖把篓,否則新數(shù)據(jù)無(wú)法寫入
mysql> insert into center.users values(3,"test");
Query OK, 1 row affected (0.00 sec)
然后在slave數(shù)據(jù)庫(kù)上查看纫溃,發(fā)現(xiàn)master上新寫入的數(shù)據(jù)已經(jīng)同步過(guò)來(lái)了
mysql> select * from center.users;
+-----+-----------+
| id? | name? ? ? |
+-----+-----------+
|? 1 |? zhangsan |
|? 2 |? lisi? ? |
|? 3 |? test? ? |
+-----+-----------+
3 rows in set (0.00 sec)
至此,主從同步環(huán)境已經(jīng)實(shí)現(xiàn)韧掩!
參考網(wǎng)址:http://www.cnblogs.com/kevingrace/p/6256603.html