说明:两台机器都以安装好mysql环境,下面演示一下配主从配置过程!同步的库名为zengzhunzhun
环境:192.168.15.96(主库)
192.168.15.102(从库)
1、主库的my.cnf文件里面增加如下内容:
server-id=1
log-bin=binlog
binlog-do-db=zengzhunzhun
2、启动主库
/usr/local/mysql/bin/mysqld_safe --set-variable=max_allowed_packet=2M &
3、登录主库并授权,允许指定用户在主库上读取日志
mysql -u root -p
mysql> grant replication slave on *.* to
'root'@'192.168.15.102' identified by '123456';
4、主库验证授权成功?
mysql> select user,host,password from mysql.user;
有如下输出则说明授权成功
+-------+----------------+-------------------------------------------+
| user | host | password |
+-------+----------------+-------------------------------------------+
| user | host | password |
+-------+----------------+-------------------------------------------+
| root | 192.168.15.102 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-------+----------------+-------------------------------------------+
5、锁主库表
+-------+----------------+-------------------------------------------+
5、锁主库表
mysql> flush tables with read lock;
6、显示主库信息。记录file和position信息,从库同步的时候用的到
6、显示主库信息。记录file和position信息,从库同步的时候用的到
mysql> show master status;
+---------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| binlog.000001 | 571 | zengzhunzhun | |
+---------------+----------+--------------+------------------+
1 row in set (0.00 sec)
7、主库用mysqldump备份zengzhunzhun整个库
+---------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| binlog.000001 | 571 | zengzhunzhun | |
+---------------+----------+--------------+------------------+
1 row in set (0.00 sec)
7、主库用mysqldump备份zengzhunzhun整个库
mysqldump -u root -p123456 zengzhunzhun > 20121123.sql
8、从库从主库拷贝备份文件,scp和rsync都可以!
rsync -acvz 192.168.15.96:/root/20121123.sql /root
9、登录从库,建立zengzhunzhun数据库,
mysql> create database zengzhunzhun;
10、将20121123.sql文件导入到zengzhunzhun库
mysql -u root -p123456 zengzhunzhun
11、解锁主库表
mysql> unlock tables;
12、修改从库的my.cnf文件,增加如下内容:
server-id=2
13、重新启动从库
pkill -9 mysql
/usr/local/mysql/bin/mysqld_safe --set-variable=max_allowed_packet=2M &
14、登录从库并且设置同步
mysql> slave stop;
mysql> change master to master_host='192.168.15.96',master_user='root',master_password='123456',master_log_file='binlog.000001',master_log_pos=571;
15、查看从库状态,下面红色字体必须是Yes状态。
15、查看从库状态,下面红色字体必须是Yes状态。
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.15.96
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000001
Read_Master_Log_Pos: 571
Relay_Log_File: localhost-relay-bin.000003
Relay_Log_Pos: 606
Relay_Master_Log_File: binlog.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: 292
Relay_Log_Space: 327
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
1 row in set (0.00 sec)
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.15.96
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000001
Read_Master_Log_Pos: 571
Relay_Log_File: localhost-relay-bin.000003
Relay_Log_Pos: 606
Relay_Master_Log_File: binlog.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: 292
Relay_Log_Space: 327
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
1 row in set (0.00 sec)
ERROR:
No query specified
16、测试工作
No query specified
16、测试工作
在主库zengzhunzhun上新建一个表并插些记录,确认从库是否同步这张表过去?数据是否全?
总结:有时候在生产环境中遇到从库的两个状态不是Yes状态,也许是配置不正确,也许是别的问题,有时候我们可以尝试跳过一个错误,再看看状态,命令如下:
mysql> salve stop;
mysql> set global sql_slave_skip_counter=1; (1是指跳过一个错误)
mysql> slave start;