简要原理
1、Mysql 主从复制基本工作原理 :
主服务器将改变记录记录到她的二进制日志中,从服务器将主服务器的二进制日志复制到它的中级日志中去,然后通过中级日志改变它的数据,这样就实现了主从复制。
2、Slave的两个线程:
I/O线程:将主服务器的二进制日志写入到中继日志中去
sql线程:读取中继日志的内容,更新数据
一、实验环境
两台装有mysql的虚拟机
Server3 172.25.17.3 主服务器
Server4 172.25.17.4 从服务器
二、修改 /etc/my.cnf文件
1、主服务器
[mysqld]
log-bin=mysql-bin 启用二进制日志
server-id=3 服务器唯一ID,默认是1
2、从服务器
[mysqld]
log-bin=mysql-bin 启用二进制日志
server-id=4 服务器唯一ID,默认是1
3、重启两台虚拟机的mysql /etc/init.d/mysqld restart
三、主从数据库的授权和配置
1、主服务器
mysql> GRANT REPLICATION SLAVE ON *.* to 'zzw'@'172.25.17.%' identified by 'westos';
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+------------------------+
| mysql-bin.000007 | 257 | | |
+------------------+----------+--------------+------------------+-------------------------+
1 row in set (0.00 sec)
2、从服务器
mysql> change master to master_host='172.25.17.3',master_user='zzw',master_password='westos', master_log_file='mysql-bin.000007',master_log_pos=257;
Query OK, 0 rows affected (0.42 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.17.3
Master_User: zzw
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 257
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000007
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: 257
Relay_Log_Space: 407
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)
Slave_IO_Running: Yes Slave_SQL_Running: Yes
这两个状态都是yes 才说明主从复制没有问题
四、测试
1、主服务器数据库插入测试数据
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> use test;
Database changed
mysql> create table test(id int(3),name char(10));
Query OK, 0 rows affected (0.08 sec)
mysql> insert into test values(1,'zzw');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+------+------+----+
| id | name |
+------+------+----+
| 1 | zzw |
+------+------+----+
1 row in set (0.00 sec)
2、查看从服务器数据库有无同步
mysql> use test
Database changed
mysql> select * from test;
+------+------+----+
| id | name |
+------+------+----+
| 1 | zzw |
+------+------+----+
1 row in set (0.00 sec)
测试成功!
五、实验总结
1、修改 /etc/my.cnf 应注意 添加server-id和启用二进制日志 时必须添加在
[mysqld]下,而不是[mysqld_safe]下 否则从服务器的复制功能可能无法正常启动。
还有需要重启两台虚拟机的mysql.
(将server-id放在[mysqld_safe]下 会默认读取server-id=1)
2、配置主服务器数据库时,每次授权slave 都会产生不同的mysql-bin
和Position
3、重启mysql 失败 出现报错
Another MySQL daemon already running with the same unix socket.
两个方法解决:
第一个是立即关机 关机后在启动,进程就停止了。
第二个直接把mysql.sock文件改名即可。也可以删除,推荐改名。
4、重启从服务器的复制功能后 其状态 I/O 是关闭的 Slave_IO_Running: No
查看日志的报错Slave I/O: error connecting to master
最后通过排查是用户的问题
5、重新配置从服务器复制功能时需要先stop slave;