安装mysql的方法指令见 阿里云CentOS配置Java,Tomcat,MySQL
首先是在主库中创建用于同步的账户
grant replication slave,file on *.* to 'replication'@'192.168.1.108' identified by '123456';
flush privileges;
然后是修改主库的配置文件 /etc/my.cnf
log-bin=mysql-bin #打开mysql二进制日志
server-id=1 #设置mysql_id,主从不能相同
binlog-do-db=testamoeba #设置二进制日志记录的库
binlog-ignore-db=mysql ##设置二进制日志不记录的库
sync_binlog=1
service mysql restart
然后重启MySQL服务
同时配置从库 /etc/my.cnf
log-bin=mysql-bin
server-id=2
replicate-do-db=testamoeba #设置同步的库
replicate-ignore-db=mysql #设置不同步的库
log-slave-updates #同步后记录二进制日志
slave-skip-errors=all
sync_binlog=1
slave-net-timeout=60
相同的也重启MySQL服务
service mysql restart
然后登陆主库的MySQL
执行:
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000003333
Position: 423332
Binlog_Do_DB: testamoeba
Binlog_Ignore_DB: mysql
Executed_Gtid_Set:
1 row in set (0.00 sec)
然后再登陆从库的MySQL
执行
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000003333
Position: 12032321
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
首先登陆MySQL先关闭从库的自动同步
mysql> stop slave;
然后配置自动同步的主库
change master to
-> master_host='123.456.789.111',master_user='replication',master_password='123456',master_log_file='mysql-bin.0000033333',master_log_pos=423;
然后再开启从库的自动同步
mysql> start slave;
此时查看就会出现
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 123.456.789.111
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 423
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: testamoeba
Replicate_Ignore_DB: mysql
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: 423
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: 1
Master_UUID: 603b8571-6255-11e8-9ebd-525400428d31
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)
此时再主库中插入数据便会自动同步到从库
关于读写分离,将使用阿里的中间件 amoeba 实现