mysql主從複製相信已經用得不少了,可是因爲工做緣由一直沒怎麼用過。趁着這段時間相對空閒,也就本身實現一遍。儘管互聯網上已有大把相似的文章,可是自身實現的仍然值得記錄。mysql
環境:sql
主服務器:centos 6.0 mysql 5.1.67-log IP:192.168.0.107
從服務器:centos 6.0 mysql 5.1.67-log IP:192.168.0.109
主服務器test數據庫數據庫
CREATE TABLE `menber` ( `name` varchar(255) DEFAULT NULL default '', `id` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1; insert into `menber` (`name`, `id`) values('zhangsan','1'); insert into `menber` (`name`, `id`) values('lisi','2'); insert into `menber` (`name`, `id`) values('王五','3');
mysql默認配置文件,如不特殊指定默認爲/etc/my.cnfcentos
mysql配置文件查找順序:/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf服務器
配置:ide
1、主服務器測試
1.一、建立一個複製用戶,具備replication slave 權限。spa
mysql>grant replication slave on *.* to 'repl'@'192.168.0.109' identified by 'repl'; mysql>flush privileges;
1.二、編輯my.cnf文件線程
vi /etc/my.cnf
添加rest
server-id=107
並開啓log-bin二進制日誌文件(Mysql須要有/var/lib/mysql/目錄的讀寫權限【可經過chown -R mysql:mysql /var/lib/mysql命令進行更改】)
log-bin=/var/lib/mysql/mysql-bin
#指定絕對路徑,否者會出現mysql運行show master status;時沒法查看日誌狀況 mysql> show master status; Empty set (0.00 sec) mysql> show binary logs; ERROR 1381 (HY000): You are not using binary logging
其餘擴展配置項:
binlog-do-db=mysql1 #須要備份的數據庫名,若是備份多個數據庫,重複設置這個選項 便可
binlog-ignore-db=mysql2 #不須要備份的數據庫名,若是備份多個數據庫,重複設置這 個選項便可
log-slave-updates=1 #這個參數必定要加上,不然不會給更新的記錄些到二進制文件 裏
slave-skip-errors=1 #是跳過錯誤,繼續執行復制操做(可選)
1.三、重啓mysql數據庫
service mysqld restart
1.四、設置讀鎖
mysql>flush tables with read lock;
1.五、獲得binlog日誌文件名和偏移量(此處記住File名稱和Position值,後面slave服務器配置時須要用到)
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 713 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
1.六、備份要同步的數據庫
mysqldump test > test.sql
1.七、解鎖
mysql>unlock tables;
2、從服務器(192.168.0.109)
將master(192.168.0.107)備份的數據庫數據恢復到slave從服務器(192.168.0.109)
2.一、編輯my.cnf文件
vi /etc/my.cnf
添加
server-id=109
2.二、重啓從數據庫
service mysqld restart
2.三、對從數據庫進行相應設置
此處要注意logfile的名稱和position的值,其他host、user和password爲主數據庫設置的帳號和密碼
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to
-> master_host='192.168.0.107',
-> master_user='repl',
-> master_password='repl',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=713;
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.107
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1079
Relay_Log_File: mysqld-relay-bin.000004
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000001
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: 1079
Relay_Log_Space: 407
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:
1 row in set (0.00 sec)
ERROR:
No query specified
在這裏主要是看:
Slave_IO_Running=Yes
Slave_SQL_Running=Yes
若是出現Slave_IO_Running: No或Slave_SQL_Running: NO,須要重作2.三、對從數據庫進行相應設置
3、測試:
上述項配置完之後可查看master和slave上線程的狀態。在master上,你能夠看到slave的I/O線程建立的鏈接:在master上輸入show processlist\G;
mysql> show processlist\G; *************************** 1. row *************************** Id: 4 User: root Host: localhost db: NULL Command: Query Time: 0 State: NULL Info: show processlist *************************** 2. row *************************** Id: 19 User: repl Host: 192.168.0.109:42337 db: NULL Command: Binlog Dump Time: 183 State: Has sent all binlog to slave; waiting for binlog to be updated Info: NULL 2 rows in set (0.00 sec) ERROR: No query specified
3.一、在主數據庫:192.168.0.107上添加新數據
insert into `menber` (`name`) values('李八');insert into `menber` (`name`) values('蒼井空');
3.2從數據庫:192.168.0.109上查看數據庫
mysql> select * from menber; +-----------+----+ | name | id | +-----------+----+ | zhangsan | 1 | | lisi | 2 | | 王五 | 3 | | 李八 | 4 | | 蒼井空 | 5 | +-----------+----+ 5 rows in set (0.02 sec)
此時數據已經成功複製到slave從數據庫192.168.0.109上。