mysql主從復制相信已經用得很多了,但是由於工作原因一直沒怎么用過。趁着這段時間相對空閑,也就自己實現一遍。盡管互聯網上已有大把類似的文章,但是自身實現的仍然值得記錄。
環境:
主服務器: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.cnf
mysql配置文件查找順序:/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
配置:
一、主服務器
1.1、創建一個復制用戶,具有replication slave 權限。
mysql>grant replication slave on *.* to 'repl'@'192.168.0.109' identified by 'repl'; mysql>flush privileges;
1.2、編輯my.cnf文件
vi /etc/my.cnf
添加
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.3、重啟mysql數據庫
service mysqld restart
1.4、設置讀鎖
mysql>flush tables with read lock;
1.5、得到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.6、備份要同步的數據庫
mysqldump test > test.sql
1.7、解鎖
mysql>unlock tables;
二、從服務器(192.168.0.109)
將master(192.168.0.107)備份的數據庫數據恢復到slave從服務器(192.168.0.109)
2.1、編輯my.cnf文件
vi /etc/my.cnf
添加
server-id=109
2.2、重啟從數據庫
service mysqld restart
2.3、對從數據庫進行相應設置
此處要注意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.1、在主數據庫: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上。
