使用工具
MySQL数据版本:5.6.36-log、
两台云服务器(Linux系统)
首先,需要在Linux系统下安装MySQL
,具体步骤可以参考这里,并且确保两台主机可以相互访问,可以直接ping一下。
配置Master
- 在
Linux
环境下,MySQL
的配置文件在/ect/my.cnf
,直接打开并编辑该文件:vim /etc/my.cnf
- 在
[mysqld]
下输入配置12345log-bin=mysql-binserver-id=2binlog-ignore-db=information_schemabinlog-ignore-db=mysqlbinlog-do-db=rwtest
这里的server-id
用于标识唯一的数据库,这里设置为2在设从库需要设置为其他值。
binlog-ignore-db
:表示同步的时候ignore的数据库binlog-do-db
:指定需要同步的数据库
- 重启
MySQL
:service mysqld restart
登录
MySQL
并对访问用户进行授权:Slave机器需要File
权限和REPLICATION SLAVE
权限,当然也可以授予全部权限。123456789grant file on *.* to 'root'@'%' identified by 'your password';grant replication slave on *.* to 'root'@'%' identified by 'your password';flush privileges;-------------------------------------------grant all privileges on *.* to 'root'@'%' identified by 'your password';flush privileges;完成后,可以查看下
Master
的状态:show master status
1234567mysql> show master status;+------------------+----------+--------------+----------------------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+----------------------------------+-------------------+| mysql-bin.000001 | 2272 | rwtest | information_schema,mysql | |+------------------+----------+--------------+----------------------------------+-------------------+1 row in set (0.03 sec)
这里的File
和Position
在Slave
中都要用到,Binlog_Do_DB
指需要同步的数据库,Binlog_Ignore_DB
指不需要同步的数据库,就是刚才配置的值。
配置Slave
同样在[mysqld]下对配置文件进行编写
123456789log-bin=mysql-binserver-id=3binlog-ignore-db=information_schemabinlog-ignore-db=mysqlreplicate-do-db=rwtestreplicate-ignore-db=mysqllog-slave-updatesslave-skip-errors=allslave-net-timeout=60配置完毕后同样重启一下
MySQL
,并进入设置其对应的Master
1234mysql> stop slave; #关闭Slavemysql> change master to master_host='Master主机IP',master_user='刚才授权的用户',master_password='your password',master_log_file='mysql-bin.000001', master_log_pos=2272;mysql> start slave; #开启Slave
master_log_file
和master_log_pos
都是Master的状态值。必须对应。
- 登录slave的数据库,查看slave的状态:
show slave status \G
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455Slave_IO_State: Waiting for master to send eventMaster_Host: 你的Master主机IPMaster_User: rootMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 2272Relay_Log_File: izwz9fcvpu481xh55cegx0z-relay-bin.000002Relay_Log_Pos: 1339Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: rwtestReplicate_Ignore_DB: mysqlReplicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 2272Relay_Log_Space: 1530Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 2Master_UUID: de20814f-2fb8-11e7-8f61-5254002b91a1Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update itMaster_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position: 01 row in set (0.03 sec)
如果状态中含有没有报Error,则表示配置成功了。
接下来你通过可视化工具连接两个数据库,在主库中进行增删改操作,从库中也会有相应的操作,而在从库中进行此类操作对主库无效。