Master:
#MySQLdump -uroot -hlocalhost -p123456 --all-databases --lock-all-tables --flush-logs --master-data=2 > /backup/alldatabase.sql
mysql>flush tables with read lock;
mysql>show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 360 | | |
+------------------+----------+--------------+------------------+
mysql>unlock tables;
#scp /backup/alldatabase.sql 172.16.4.12:/tmp
Slave:
#mysql -uroot -p123456 < /tmp/alldatabase.sql
mysql>change master to \
master_host='172.16.4.11',
master_user='repl',
master_password='135246',
master_log_file='mysql-bin.000004',
master_log_pos=360;
mysql>show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 172.16.4.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 360
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: No
Slave_SQL_Running: No
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: 360
Relay_Log_Space: 107
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
mysql>start slave;
mysql>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Queueing master event to the relay log
Master_Host: 172.16.4.11
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 360
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000004
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: 360
Relay_Log_Space: 300
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 11
说明MySQL的主从复制架构成功
注1:MySQL的复制可以基于某个数据库或库中的默写表进行复制,要想实现该功能,只需在其配置文件中添加以下配置:
Master:
binlog-do-db=db_name 只复制db_name数据库
binlog-ignore-db=db_name 不复制db_name数据库
注2:在Master上定义过滤规则,意味着,任何不涉及到该数据库相关的写操作都不会被记录到二进制日志中,因此不建议在Master上定义过滤规则,并且不建议binlog-do-db与binlog-ignore-db同时定义。
Slave:
replicate_do_db=db_name 只复制db_name数据库
replicate_ignore_db=db_name 不复制db_name数据库
replicate_do_table=tb_name 只复制tb_name表
replicate_ignore_table=tb_name 只复制tb_name表
replicate_wild_do_table=test% 只复制以test为开头并且后面跟上任意字符的名字的表
replicate_wild_ignore_table=test_ 只复制以test为开头并且后面跟上任意单个字符的名字的表
注3:如果需要指定多个db或table时,则只需将命令多次写入
