Mysql 主从数据库同步
环境:
配置:
添加
server-id=1
并开启log-bin二进制日志文件
log-bin=mysql-bin
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.0000010 | 106| | |
+------------------+----------+--------------+------------------+
添加
server-id=2
-> master_host='192.168.1.22'
-> master_user='repl'
-> master_password='repl'
-> master_log_file='mysql-bin.0000010'
-> master_log_pos=106;
*************************** 2. row ***************************
Id: 2
User: system user
Host:
db: NULL
Command: Connect
Time: 2579
State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.22
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.0000010
Read_Master_Log_Pos: 106
Relay_Log_File: centos-relay-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.0000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_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: 106
Relay_Log_Space: 830
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)
验证是否配置正确
Waiting for master to send event
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
测试
mysql>create table user(id int);
mysql> show tables like 'user';
+----------------------+
| Tables_in_test(user) |
+----------------------+
| user |
+----------------------+
1 row in set (0.00 sec)
问题?
#binlog-ignore-db=不需要备份的数据库名,可写多行
从服务器my.cnf
# replicate-ignore-db=mysql 忽略的数据库
# master-connect-retry=60 如果从服务器发现主服务器断掉,重新连接的时间差(秒)
master-host=192.168.1.22
master-port=3306
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.0000011 | 260| | |
+------------------+----------+--------------+------------------+
+--------------------------------------------------+
| master_pos_wait('mysql-bin.0000011','260') |
+--------------------------------------------------+
| 0 |
+--------------------------------------------------+
1 row in set (0.01 sec)
Query OK,0 affected (0.00 sec)
mysql>show processlist\G;
Id: 2
User: system user
Host:
db: NULL
Command: Connect
Time: 4757
State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
Query OK,0 affected (0.00 sec)
mysql>reset master;
Query OK,0 affected (0.00 sec)