主库配置文件my.ini
server-id=1
log-bin=mysql-bin.log
server-id=2
#启用从库日志,这样可以进行链式复制
log-slave-updates
#从库是否只读,0表示可读写,1表示只读
read-only=1
#只复制某个表
replicate-do-table=tablename
#只复制某些表(可用匹配符)
replicate-wild-do-table=tablename%
#只复制某个库
replicate-do-db=dbname
#只复制某些库
replicte-wild-do-db=dbname%
#不复制某个表
replicate-ignore-table=tablename
#不复制某些表
replicate-wild-ignore-table=tablename%
#不复制某个库
replicate-ignore-db=dbname
#复制完的sql语句是否立即从中继日志中清除,1表示立即清除
relay-log-purge=1
#从服务器主机,用于show slave hosts生成从库清单
report-host=hostname
master:
mysql> grant replication slave on *.* to 'rep'@'%' identified by 'rep';
Query OK, 0 rows affected (0.00 sec)
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000015 | 433 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.01 sec)
salve:
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.1.201',
-> MASTER_USER='rep',
-> MASTER_PASSWORD='rep',
-> MASTER_LOG_FILE='mysql-bin.000015',
-> MASTER_LOG_POS=433;
Query OK, 0 rows affected (0.07 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.201
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000015
Read_Master_Log_Pos: 615
Relay_Log_File: node2-relay-bin.000002
Relay_Log_Pos: 433
Relay_Master_Log_File: mysql-bin.000015
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: 615
Relay_Log_Space: 588
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)
master:
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
slave:
mysql> show processlist;
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
| 1 | root | localhost | test | Query | 0 | NULL | show processlist |
| 2 | system user | | NULL | Connect | 647 | Waiting for master to send event | NULL |
| 3 | system user | | NULL | Connect | 240 | Has read all relay log; waiting for the slave I/O thread to update it | NULL |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
3 rows in set (0.00 sec)
master:
mysql> show processlist;
+----+------+-------------+------+-------------+------+----------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-------------+------+-------------+------+----------------------------------------------------------------+------------------+
| 1 | rep | node2:56986 | NULL | Binlog Dump | 1837 | Has sent all binlog to slave; waiting for binlog to be updated | NULL |
| 2 | root | localhost | test | Query | 0 | NULL | show processlist |
+----+------+-------------+------+-------------+------+----------------------------------------------------------------+------------------+
2 rows in set (0.02 sec)
在从库的数据目录下,有几个和复制相关的文件需要说明一下:
*-reloay-bin.* 从主库同步过来的Bin log文件,也叫中继日志
master.info 主库帐号信息和同步信息,这里记录了复制用户名和密码,需要保护好权限。
relay-log.info 跟踪执行同步过来的Bin log的执行情况
参考 http://www.ningoo.net/html/2007/mysql_replication_configuration.html
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/758322/viewspace-680438/ ,如需转载,请注明出处,否则将追究法律责任。