折腾了几天,终于弄清了mysql主从复制,原来如此简单html
1.主从保持版本一致,mysql
2.开启主服务器的bin-logsql
[root@cetos2 ~]# whereis my.cn
my: /etc/my.cnf
[root@cetos2 ~]# vim /etc/my.cnfshell
server-id=129 //服务器id 这个要与从服务器区分
log_bin=mysql-bin
log_bin_index=mysql-bin.index
expire_logs_days=10
max_binlog_size=100M数据库
重启服务vim
[root@cetos2 ~]# systemctl restart mysql;
[root@cetos2 ~]# 服务器
--查看主服务器日志是否已开启 ON表明已开启ide
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)测试
3.修改从服务器slaveui
#vi /etc/my.cnf [mysqld] log-bin=mysql-bin //[不是必须]启用二进制日志 server-id=226 //[必须]服务器惟一ID,默认是1,通常取IP最后一段
4.在主服务器上创建账户并受权slave:
mysql>GRANT REPLICATION SLAVE ON *.* 'repl'@'%' identified by 'm123456';
//一般复制用单独的帐号 repl
5. 登陆主服务器的mysql,查询master的状态
mysql> show master status\g;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
ERROR:
No query specified
注:执行完此步骤后不要再操做主服务器MYSQL,防止主服务器状态值变化
6.配置从服务器Slave:
mysql> change master to master_host='192.168.146.128',master_user='repl',
-> master_password='m123456',
-> master_log_file='mysql-bin.000004',master_log_pos=120;
Mysql>start slave; //启动从服务器复制功能
若是上面有错误能够从新配置
在从库上reset slave all ,从新配置,用repl用户
mysql> reset slave all
mysql>stop slave //中止
mysql>start slave //启动
7.检查从服务器复制功能状态:
mysql> show slave status\G
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.146.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 517
Relay_Log_File: mysqld-relay-bin.000010
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000006
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: 517
Relay_Log_Space: 457
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: 128
Master_UUID: 96ed73d8-942e-11ea-ae75-000c29f06575
Master_Info_File: /var/lib/mysql/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)
ERROR:
No query specified
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.222 //主服务器地址 Master_User: mysync //受权账户名,尽可能避免使用root Master_Port: 3306 //数据库端口,部分版本没有此行 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 600 //#同步读取二进制日志的位置,大于等于Exec_Master_Log_Pos Relay_Log_File: ddte-relay-bin.000003 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes //此状态必须YES Slave_SQL_Running: Yes //此状态必须YES ...... 注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,不然都是错误的状态(如:其中一个NO均属错误)。 以上操做过程,主从服务器配置完成。
8.主从服务器测试
主服务器Mysql,创建数据库,并在这个库中建表插入一条数据:
能够看到从服务器已能同步