---
title: "MySQL主从配置"
author: "weini174"
date: "2016-10-08T15:28:39Z"
tags: []
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2016-10-08T15:28:39Z"
id: 2326462
url: "https://weini174.iteye.com/blog/2326462"
---

1.环境

Ubuntu 15.10, MySQL 5.7.14

2.Master 配置

```
sudo vim /etc/my.cnf --修改my.cnf文件
```

 添加如下内容

```
log-bin=server-bin
server-id=1 --唯一标识，可以取ip最后一段
```

重启master数据库

```
service mysql restart;
```

 在master库创建一个用户，供slave库复制数据使用

```
GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO 'backuser'@'10.9.21.%' IDENTIFIED BY '123456'；
```

 查看master状态

```
mysql>show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      763 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
```

 记录一下File和position的值，下边配置slave的时候要用到。

 

3 Slave配置

```
sudo vim /etc/my.cnf --修改my.cnf文件
```

 添加如下内容

```
log-bin=server-bin
server-id=2 --唯一标识，可以取ip最后一段
```

重启master数据库

```
service mysql restart;
```

 

执行如下命令

```
mysql>change master to master_host='10.9.21.175',master_user='backuser',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.09 sec)

--master_host是master服务器的ip
--master_user,master_password是我们在master数据库建的用户
--master_log_file,master_log_pos是我们上边记录的master状态信息

mysql>start slave;
```