master(主服务器),slave(从服务器)
主服务器配置
[mysqld]
port=3306
basedir="D:/master/MySQL/"
datadir="D:/master/MySQL/data/"
character-set-server=utf8
server_id=1
log-bin=mysql-bin#要生成的二进制日记文件名称
binlog-do-db=test#同步的数据库
binlog-ignore-db=mysql#不参与同步的数据库 ,例如mysql
从服务器配置
[mysqld]
port=3307
basedir=D:\slave\mysql2
datadir=D:\slave\mysql2\data
server-id=2?
log-bin=mysql-bin#开启了二进制文件
binlog_do_db=follow#主从复制的数据库
binlog_ignore_db=mysql#不参与主从复制的数据库,例如mysql
接下来在主服务器登录mysql,创建用于主从复制的用户并授权
create user ‘xiao‘@‘127.0.0.1‘ identified by ‘password‘;
grant replication slave on . to xiao@‘127.0.0.1‘;
在从服务器上用创建的用户登录主服务器,看能不能成功,成功继续下面的步骤
在主服务器上执行命令:
show master status;(此命令查看主服务器的bin-log日志文件名称和position点)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 107 | test | mysql |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
在从库设置它的master:
mysql> change master to
master_host=‘127.0.0.1‘,master_port=3306,master_user=‘xiao‘,master_password=‘password‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=107;
Query OK, 0 rows affected (0.19 sec)
这里的master_log_file和master_log_pos对应刚才show master status记下的参数。
mysql>start slave;
Query OK, 0 rows affected (0.00 sec)
当Slave_IO_Running和Slave_SQL_Running线程都为yes是主从复制配置成功!
-- 主库
mysql> create table tianyc_02(b int);
Query OK, 0 rows affected (0.16 sec)
mysql> insert into tianyc_02 values(2013);
Query OK, 1 row affected (0.13 sec)
-- 从库
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tianyc_01 |
| tianyc_02 |
+----------------+
2 rows in set (0.00 sec)
mysql> select * from tianyc_02;
+------+
| b |
+------+
| 2013 |
+------+
1 row in set (0.00 sec)
同理,可以搭建第二、第三个从节点。
备注:两个服务的serve_id必须不同,否则在开启复制功能时会提示错误
mysql> slave start;
ERROR 1200 (HY000): The server is not configured as slave; fix in config file or with CHANGE MASTER TO
我这里设置的主节点id为1,从节点id为2。也可以按照ip或端口进行设置,好用来区分。
原文:https://www.cnblogs.com/xq-0226/p/11483590.html