MySQL主从配置
一、准备
1、主从数据库的版本号最好一直(因为官方推荐这样,最主要还是怕版本不一致带来的不兼容问题;)
2、主从数据库内数据保持一致,若不一致可将从数据库的数据备份备份出来。在将从数据库的数据全都删除,并将主库的数据全部导入进去
主数据库:192.168.122.135
从数据库:192.168.122.136
二、操作
主数据库操作
1、开启binlog
【mysqld】
log-bin=mysql-bin server-id=1#主从的server-id不能一样
2、启动主库
systemctl start mysql #用来创建同步的用户
3、登陆数据库
mysql -uroot -p
4、创建一个测试库、表
>create database game; >use game; >show tables; >create table LOL( id int AUTO_INCREMENT PRIMARY KEY, name char(20), age int); >insert into LOL(name,age) values('JS',12),('XYR',23);
5、在主库上创建供slave远程连接的用户并授权(用户LM。slave库ip。密码)
>create user 'LM'@'192.168.122.136' identified by 'Www.1.com';
6、授予复制权限
>grant replication slave on *.* to 'LM'@'192.168.122.136';
7、刷新
>flush privileges;
8、锁表,禁止写入,当前窗口不能退出,这时候开启另一个终端继续操作
>flush table with read lock;
9、新窗口操作,查看master状态,记录二进制文件名和位置
>show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 1042 | | | +------------------+----------+--------------+------------------+
10、把当前数据导出
# mysqldump -u root -p --all-databases > alldb.sql
11、解锁查看binlog日志位置,如果没变化证明锁定成功,从库将从这个binlog日志开始恢复
>unlock tables;
>show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 1042 | | | +------------------+----------+--------------+------------------+
从库操作
1、导入数据库
# mysql -u root -p < alldb.sql
2、修改配置文件
server-id=2 #设置server-id,必须唯一
3、重启mysql,配置同步(需要主服务器主机名,登陆凭证,二进制文件的名称和位置)
change master to master_host='192.168.122.135', -> master_user='LM', -> master_password='Www.1.com', -> master_log_file='mysql-bin.000003', -> master_log_pos=1024;
4、启动slave
>start slave;
5、查看slave状态
>show slave status\G; 如果 Slava_IO_Running:YES Slave_SQL_Runing:YES 即可
三、错误解决
Slave_IO_Running: No
由于主服务器异外重启, 导致从报错, 错误如下: show slave status错误: mysql> show slave status\G Master_Log_File: mysql-bin.000288 Read_Master_Log_Pos: 627806304 Relay_Log_File: mysql-relay-bin.000990 Relay_Log_Pos: 627806457 Relay_Master_Log_File: mysql-bin.000288 Slave_IO_Running: No Slave_SQL_Running: Yes Exec_Master_Log_Pos: 627806304 Relay_Log_Space: 627806663
查看日志
tail /data/mysql/mysql-error.log 111010 17:35:49 [ERROR] Error reading packet from server: Client requested master to start replication from impossible position ( server_errno=1236) 111010 17:35:49 [ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: 'Client requested master to start replication from impossible position', Error_code: 1236 111010 17:35:49 [Note] Slave I/O thread exiting, read up to log 'mysql-bin.000288', position 627806304
解决方法
错误依旧, 接下来登陆到主服务器查看binlog日志. 先按照错误点的标记去主服务器日志中查找: [root@db1 ~]# mysqlbinlog --start-position=627655136 /data/mysql/binlog/mysql-bin.000288 /*!40019 SET @@session.max_insert_delayed_threads=0*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #111010 13:31:19 server id 4 end_log_pos 106 Start: binlog v 4, server v 5.1.45-log created 111010 13:31:19 # Warning: this binlog is either in use or was not closed properly. BINLOG ' F1aTTg8EAAAAZgAAAGoAAAABAAQANS4xLjQ1LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC '/*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; 没有看到这个位置. [root@db1 ~]# mysqlbinlog /data/mysql/binlog/mysql-bin.000288 > test.txt less text.txt 看最后一部分 # at 627625495 #111010 16:35:46 server id 1 end_log_pos 627625631 Query thread_id=45613333 exec_time=32758 error_code=0 SET TIMESTAMP=1318289746/*!*/; delete from freeshipping_bef_update where part='AR-4006WLM' and code='' /*!*/; # at 627625631 #111010 16:35:46 server id 1 end_log_pos 627625751 Query thread_id=45613333 exec_time=32758 error_code=0 SET TIMESTAMP=1318289746/*!*/; delete from shippingFee_special where part='AR-4006WLM' /*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; 找到最接近错误标记627655136的一个position是627625631. 再回到slave机器上change master, 将postion指向这个位置. mysql> stop slave; Query OK, 0 rows affected (0.00 sec) mysql> change master to master_log_file='mysql-bin.000288',master_log_pos=627625631; Query OK, 0 rows affected (0.06 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec) 再次查看 mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Queueing master event to the relay log Master_Host: 192.168.21.105 Master_User: rep Master_Port: 3306 Connect_Retry: 10 Master_Log_File: mysql-bin.000289 Read_Master_Log_Pos: 25433767 Relay_Log_File: mysql-relay-bin.000003 Relay_Log_Pos: 630 Relay_Master_Log_File: mysql-bin.000289 Slave_IO_Running: Yes Slave_SQL_Running: Yes 主从同步正常了, 同样的方法修复其它slave机器.