本文不写原理相关内容,只记录操作步骤。
主服务器IP:192.168.0.101
从服务IP:192.168.0.102
Master服务器配置
1.更改master服务器mysql的my.cnf配置文件,配置文件在etc目录下,开启Master节点上的二进制日志功能
# 以下为添加的内容
......
# 开启日志
log_bin
# 以下这些参数会在后文进行说明
sync_binlog=1
binlog_format=mixed
binlog-do-db=test #该参数用于设置MySQL Master节点上需要进行Replicaion操作的数据库名称。
binlog_checksum=CRC32
binlog_cache_size=2M
max_binlog_cache_size=1G
max_binlog_size=100M
# 必须为这个MySQL服务节点设置一个集群中唯一的 server id信息
server_id=1
修改完成之后重启mysql服务
2.在Master节点中设置可供连接的Salve节点信息,包括进行Replicaion同步的用户和密码信息,首先需要用root账户登录mysql
# 这里我们直接使用root账号进行同步,但是生产环境下不建议这样使用,注意后面的IP地址为从服务器的IP地址
grant replication slave on *.* to root@192.168.0.102 identified by '123456'
#刷新权限
flush privileges;
# 通过以下命令,可以查看设置完成后的Master节点工作状态
show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000012 | 450 | test | | |
+------------------+----------+--------------+------------------+-------------------+
记录File的值和Position 的值,在配置从服务器时会用到。至此,主服务器就配置好了
Salve服务器配置
1.首先还是需要修改Salve节点的my.cnf文件
# my.cnf文件中没有涉及Replicaion机制的配置信息,就不在这里列出了
......
# 开启日志
log_bin
sync_relay_log=1
# 必须为这个MySQL服务节点设置一个集群中唯一的server id信息
server_id=2
保存后重启mysql服务
2.登录mysql后配置
# 请注意这里设置的用户名和密码信息要和Master上的设置一致
# 另外master log file所指定的文件名也必须和Master上使用的日志文件名一致,即上面Master服务器中记录的File文件
#master_log_pos的值为Master服务器中记录的Position的值
#若slave上配置过从服务,需要先停止:stop slave;
change master to master_host='192.168.0.101',master_user='root',master_password='123456', master_log_file='mysql-bin.000012',master_log_pos=450;
# 启动Savle同步
start slave;
# 然后我们就可以使用以下命令查看salve节点的同步状态
show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.101
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000012
Read_Master_Log_Pos: 1373
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 1243
Relay_Master_Log_File: mysql-bin.000012
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: 1373
Relay_Log_Space: 1450
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: b30ec0b5-b6df-11e6-9c87-00163e0defc2
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 more updates
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
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
至此,主从配置就完成了。
注意:只有当Slave_IO_Running和Slave_SQL_Running的值都为yes是才表示配置成功,有一个不为yes表示配置都不成功