MySQL主从数据库配置攻略以及错误处理
MySQL主从复制配置
1. 要求
1.1 系统平台一致
1.2 数据库版本一致
2. 修改my.cnf文件,主服务器和备服务器要求server-id不能一样
3. 启动两侧的数据库
4. 在主服务器上建立账号,并且授权slave,从服务器可访问
GRANT REPLICATION SLAVE ON *.* to 'mysql_sync'@'10.10.88.101' identified by '123456';
5. 查看主服务器状态
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| dbtest.000005 | 414 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.15 sec)
注意:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化
6. 配置从服务器Slave
mysql> change master to master_host='10.10.88.100',master_user='mysql_sync',master_password='123456',master_log_file='dbtest.000005',master_log_pos=414;
mysql> start slave; //启动从服务器复制功能
mysql> change master to master_host='10.10.88.100',master_user='mysql_sync',master_password='123456',master_log_file='dbtest.000007',master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.12 sec)
mysql> start slave;
Query OK, 0 rows affected (0.24 sec)
7. 检查从服务器复制功能状态(在从服务器上面执行检查):
mysql> show slave status\G
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.10.88.100 --主服务器IP
Master_User: mysql_sync --主服务器数据库账号名称
Master_Port: 3306 --数据库端口
Connect_Retry: 60
Master_Log_File: dbtest.000005
Read_Master_Log_Pos: 414
Relay_Log_File: DBTEST2-relay-bin.000002
Relay_Log_Pos: 280
Relay_Master_Log_File: dbtest.000005
Slave_IO_Running: Yes --状态,必须为Yes
Slave_SQL_Running: Yes --状态,必须为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: 414
Relay_Log_Space: 455
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: 1
Master_UUID: 73b077c6-626d-11e5-8f5b-000c29e840c1
Master_Info_File: /app/mysqldata/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
配置过程中,因为后来从主库cp过来所有的数据文件,作为从库的初始环境,结果就报错如下:
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
经过分析,是因为从主库cp了所有文件,导致server_uuid一致,所以从库起不来,解决办法如下:
1. 进入到数据目录
cd /app/mysqldata
2. 备份auto.cnf 让数据库自动生成一个新的uuid来用
cp auto.cnf auto.cnf.bak
rm -rf auto.cnf
3. 重新启动主库以及从库
查看主库状况,重置从库状态,启动slave即可。