一、环境:
主机IP | 操作系统版本 | MySQL版本 | 安装方法 | 角色 |
10.0.0.13(master) | centos7.3 | 5.6.17 | 源码编译安装 | 主库 |
10.0.0.14(slave) | centos7.3 | 5.6.17 | 源码编译安装 | 从库 |
二、数据库安装步骤:
查看以下链接:https://mp.csdn.net/postedit/82810888
三、配置主从同步:
1.配置主服务器master(10.0.0.14):
一、vim /etc/my.cnf 加入以下两行,后重启mysqld服务
server_id = 1 log_bin=mysql-bin
二、创建一个用于复制的账户
mysql> create user admin@'%' identified by 'password'; mysql> grant all privileges on *.* to admin@'%'; mysql> flush privileges;
三、主服务器备份数据,并传输到从
mysql> flush tables with read lock; mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 120 | | | | +------------------+----------+--------------+------------------+-------------------+ row in set (0.00 sec)
[root@localhost ~]# mysqldump -uroot -p --all-databases | gzip > bak.sql.gz [root@localhost ~]# scp bak.sql.gz [email protected]:/root/
mysql> unlock tables;
2.配置从服务器slave(10.0.0.15):
先恢复数据,恢复后和主库一致
[root@localhost bin]# gunzip bak.sql.gz [root@localhost bin]# mysql -uroot -p mysql> source /root/bak.sql;
[root@localhost bin]# vi /etc/my.cnf 添加如下内容 server-id = 2 重启服务器 [root@localhost ~]# service mysql restar
mysql> CHANGE MASTER TO -> MASTER_HOST='10.0.0.14', -> MASTER_USER='admin', -> MASTER_PASSWORD='123456', -> MASTER_LOG_FILE='mysql-bin.000001', -> MASTER_PORT=3306, -> MASTER_LOG_POS=120;
MASTER_HOST指的是主服务器的IP地址, MASTER_USER指的是复制的账户 MASTER_PASSWORD指的是账户的密码 MASTER_PORT指的是主服务器端口 MASTER_LOG_FILE指的是bin-log的文件 MASTER_LOG_POS指的是日志文件位
3.从服务器启动slave线程
mysql> start slave; mysql> show processlist; +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+-----------+---------------+ | Id | User | Host | db | Command | Time | State | Info | Rows_sent | Rows_examined | +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+-----------+---------------+ | 1 | system user | | NULL | Connect | 714 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | 0 | 0 | | 2 | system user | | NULL | Connect | 714 | Waiting for master to send event | NULL | 0 | 0 | | 4 | root | localhost | NULL | Query | 0 | init | show processlist | 0 | 0 | +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+-----------+---------------+ rows in set (0.08 sec)
4.测试
主库或从库插入数据,验证是否同步。
5.管理主从
查看从服务器状态
mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.47.5 Master_User: repl Master_Port: 3307 Connect_Retry: 60 Master_Log_File: mysql-bin.000036 Read_Master_Log_Pos: 3484 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000036 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: 3484 Relay_Log_Space: 460 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: 86d34969-fa5d-11e6-b372-000c29c88c3f Master_Info_File: /usr/local/mysql/data/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 row in set (0.00 sec)
主从同步异常处理方法:
重启master库:service mysqld restart
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 98 | | |
+------------------+----------+--------------+------------------+
mysql> slave stop;
mysql> change master to Master_Log_File='mysql-bin.000001',Master_Log_Pos=98;
mysql> slave start;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.3.21
Master_User: slave
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 98
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
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: 98
Relay_Log_Space: 235
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
1 row in set (0.00 sec)
关心的两个信息是Slave_IO_Running和Slave_SQL_Running是否是YES。Slave_IO_Running是从主服务器读取BINLOG日志,并写入到从服务器的中继日志中;Slave_SQL_Running负责读取和执行中继日志信息。
注意:如果需要从服务器禁止写入操作,则需要更改参数read-only,使从服务器非root账户只能读数据。