1,环境:2台linux虚拟机(centos7)(ip:192.168.2.105 ip:192.168.2.107),mysql5.6 (自行安装)
2,步骤
2.1,选择192.168.2.105机器做master,192.168.2.107做slave,在两台机器上都开启log-bin
[root@liubity-fbs-db /]# vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
character-set-server=utf8
#开启二进制日志
log-bin=mysql-bin
server-id=3
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
建议master机器设置 server-id=1,slave机器设置server-id=3 (数字不要重复就可以)
2.2 重启mysql服务
[root@liubity-fbs-provider ~]# systemctl restart mysqld
2.3 在master机器上面添加一个slave能访问master 上的mysql 账号
mysql> grant all on *.* to 'yourname'@'%' identified by “yourpassword”;
mysql> flush privileges;
(方便起见,权限给了所有,host给了所有,请按需修改)
2.4 在master上查看 log-bin文件名已经position
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
记住这个文件名和position,下一步要用到
2.5,在slave机器上面 登录mysql ,绑定master
mysql> change master to master_host='192.168.2.105',master_user='yourname',master_password='yourpassword',master_log_file='mysql-bin.000002',master_log_pos=154;
2.6,在slave上开启 slave
mysql> start slave;
2.7,查看是否成功开启
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.107
Master_User: test
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 488
Relay_Log_File: liubity-fbs-provider-relay-bin.000006
Relay_Log_Pos: 685
Relay_Master_Log_File: mysql-bin.000004
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: 488
Relay_Log_Space: 1344
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: 7551fed6-8bfa-11e8-ae4c-000c29a51165
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:
1 row in set (0.00 sec)
mysql>
注意,当
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
都为YES才表示成功!如果有错误,会在上面有提示,请根据提示进行修改
3, 测试:
在master上的mysql 进行操作,slave上的mysql都会进行相应的跟新
4,小知识
1,master上要忽略某个数据库或多个数据库
在my.ini 中配置 binlog_ignore_db=test (多个请重复)
2,master指定要主从的数据库
在my.ini 中配置 binlog_do_db=test (多个请重复)