Mysql主从复制
1.前提条件
拥有两台或以上的mysql服务器,一台为主服务器一台为从服务器
服务器参数:
主服务器(master):192.168.1.2 端口:3306
从服务器(slave) : 192.168.1.3 端口:3306
2.修改配置
2.1 主服务器配置
#vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[必须]启用二进制日志
server-id=2 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
2.2 从服务器配置
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[不是必须]启用二进制日志
server-id=3 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
2.3 重启两台服务器
/etc/init.d/mysql restart
2.4 在主服务器上建立帐户并授权slave:
#登录mysql
mysql -uroot -p123456
#创建新用户
create user 'test'@'%' identified by '123456';
#主从复制的权限:
grant replication slave on *.* to 'test'@'%' identified by '123456';
#(@后面的ip地址为允许连接的客户端的ip地址,如果改为 '%',就表示客户端没有ip地址的限制)
2.5 查看主服务器状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 1234 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化
2.6 配置从服务器Slave
#配置同步信息
mysql> change master to master_host='192.168.1.2',master_user='test',master_password='123456', master_log_file='mysql-bin.000002',master_log_pos=1234;
#//启动从服务器复制功能
mysql> start slave;
2.7 检查从服务器复制功能状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 47.242.12.66
Master_User: test
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 967
Relay_Log_File: DESKTOP-82QLLRC-relay-bin.000002
Relay_Log_Pos: 1133
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes #必须为YES否则有错误
Slave_SQL_Running: Yes #必须为YES否则有错误
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
1 row in set (0.00 sec)
3 主从服务器测试
#主服务器
mysql> use demo;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_demo |
+----------------+
| t_user |
+----------------+
1 row in set (0.00 sec)
mysql> create table test(id int(3),name char(10));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test values(001,'小明');
Query OK, 1 row affected (0.00 sec)
#从服务器
mysql> use demo;
Database changed
mysql> show tables;
+----------------+
| Tables_in_demo |
+----------------+
| t_user |
| test |
+----------------+
2 rows in set (0.00 sec)
mysql> select * from test;
+------+--------+
| id | name |
+------+--------+
| 1 | 小明 |
+------+--------+
1 row in set (0.00 sec)
4.同步指定的库或者表
4.1 修改主库
#vim /etc/mysql/mysql.conf.d/mysqld.cnf
binlog-do-db=xxxx 二进制记录指定的数据库日志
binlog-ignore-db=xxxx 二进制日志中忽略指定数据库
以上任意使用其中一行参数就行,如果需要指定或忽略多个库,则添加多行
4.2 修改从库
#vim /etc/mysql/mysql.conf.d/mysqld.cnf
replicate-do-db=xxx 设定需要复制的数据库
replicate-ignore-db=xxx 设定需要忽略的复制数据库
replicate-do-table=xxx 设定需要复制的表
replicate-ignore-table=xxx 设定需要忽略的复制表
replicate-wild-do-table=xxx 同replication-do-table功能一样,但是可以通配符
replicate-wild-ignore-table=xxx 同replication-ignore-table功能一样,但是可以加通配符
4.3 注意
- 修改前请关闭同步线程。
- 以上方法使用一种即可实现,视情况选择合适的方法。
- 修改后要重启服务。service msyql restart