MySQL主从复制配置方法(非常简单)
1.概述
测试版本为MySQL v5.5版本的。主服务器操作系统为Ubuntu 16.04,从服务器操作系统为window server 2008 sp2。
安装过程不说了,直接上配置方法。但必须保证如下条件:
1)数据库版本一致
2)启动mysql后,手动同步两边数据,保持一致。
2.Master主服务器配置
1)找到my.cnf配置文件,增加配置参数
# vim /etc/mysql/my.cnf
[mysqld]
server-id=1 //[必须]主服务器唯一ID,默认是1,一般取IP最后一段
log-bin=mysql-bin //[必须]启用二进制日志
log-bin-index=master-bin.index //[非必须]索引文件名称
2)创建用户repl,允许其他Slave服务器可以通过远程访问Master,通过该用户读取二进制日志,实现数据同步。
# mysql -h 172.17.140.39 -u root -p
mysql> CREATE USER ‘repl’@’%’ IDENTIFIED BY ‘123456’;
mysql> GRANT REPLICATION SLAVE ON *.* TO ‘repl’@’192.168.0.%’;
注:repl用户必须具有REPLICATION SLAVE权限,除此之外没有必要添加不必要的权限。说明一下,192.168.0.%,这个配置是指明repl用户所在服务器,这里%是通配符,表示192.168.0.1-192.168.0.254的Server都可以以repl用户登陆主服务器。当然你也可以指定固定Ip。提高安全!
3.Slave从服务器配置
找到MySQL安装文件夹修改my.ini文件,在[mysqld]下面增加下面几行代码
[mysqld]
server-id=2 //[必须]从服务器唯一ID,默认是1,一般取IP最后一段
relay-log-index=slave-relay-bin.index //[非必须]自定义索引文件名称
relay-log=slave-relay-bin //[非必须]自定义的日志文件名
注意:若从服务器为linux服务器,配置参数同上,只是配置文件为my.cnf。
4.重启两台服务器的MySQL服务
主:/etc/init.d/mysql restart
从:在“服务”界面里面手动重启mysql服务器。
5.登录主服务器的mysql,查询master的状态
mysql> show master status;
+——————-+————–+——————–+———————–+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————-+————–+——————–+———————–+
| master-bin.000001 | 70 | | |
+——————-+————–+——————–+———————–+
1 row in set (0.00 sec)
注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化,因为,这个里面的数据需要在后面用到。
7、配置从服务器Slave:
mysql> change master to master_host=’172.17.140.39′,master_user=’repl’,master_password=’123456′,
master_log_file=’mysql-bin.000001′,master_log_pos=70
mysql> start slave; //启动从服务器复制功能
8、检查从服务器复制功能状态:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.140.39 //主服务器IP地址
Master_User: repl //登录用户名
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 308887 //#同步读取二进制日志的位置,大于等于Exec_Master_Log_Pos
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 309033
Relay_Master_Log_File: mysql-bin.000001
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: 308887
Relay_Log_Space: 309189
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
1 row in set (0.00 sec)
注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。
以上操作过程,主从服务器配置完成。