Skip to content

Commit 6182066

Browse files
committed
draft about mysql replication
1 parent 7d24ce4 commit 6182066

1 file changed

Lines changed: 222 additions & 0 deletions

File tree

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
---
2+
author: shellbye
3+
comments: true
4+
date: 2016-12-21 00:00:08+00:00
5+
layout: post
6+
slug: mysql_master_slave_replication
7+
title: mysql master slave replication 主从备份
8+
categories:
9+
- tech_world
10+
tags:
11+
- mysql
12+
---
13+
14+
# 场景描述
15+
在官方的[replication]描述中,replication的优点有如下几个方面:
16+
17+
{% highlight %}
18+
Advantages of replication in MySQL include:
19+
20+
- Scale-out solutions -
21+
spreading the load among multiple slaves to improve performance.
22+
In this environment, all writes and updates must take place on the master server.
23+
Reads, however, may take place on one or more slaves.
24+
This model can improve the performance of writes
25+
(since the master is dedicated to updates),
26+
while dramatically increasing read speed across an increasing number of slaves.
27+
28+
- Data security - because data is replicated to the slave,
29+
and the slave can pause the replication process,
30+
it is possible to run backup services on the slave without corrupting
31+
the corresponding master data.
32+
33+
- Analytics - live data can be created on the master,
34+
while the analysis of the information can take place on
35+
the slave without affecting the performance of the master.
36+
37+
- Long-distance data distribution -
38+
you can use replication to create a local copy of data for a remote site to use,
39+
without permanent access to the master.
40+
41+
{% endhighlight %}
42+
43+
概括起来就是:可扩展性,数据安全,便捷分析,便于分发。
44+
45+
在我们团队目前的场景中,暂时需要的其实只有“便于分发”,
46+
但是其顺带的其他特性也是“the more the better”。
47+
48+
mysql5.7之后,除了传统的给予二进制的日志的replication之外,
49+
还支持基于global transaction identifiers ([GTIDs])的复制。
50+
51+
# 操作步骤
52+
具体操作步骤在文章最后的参考文章有一些,下面我的笔记来自[官方文档]
53+
54+
1. [Master Config]
55+
第一步是设置主数据库,具体需要配置的信息比较简单,就是在`my.cnf`文件中,
56+
修改以下配置(一般就是打开注释即可),
57+
58+
{% highlight %}
59+
60+
server-id = 1
61+
log_bin = /var/log/mysql/mysql-bin.log
62+
63+
{% endhighlight %}
64+
65+
由于从数据库需要用户名和密码去链接主数据库,而且用于复制的用户名和密码是[明文存储]的,
66+
所以需要在主数据库中新建专用的用户,新建的用户需要`REPLICATION SLAVE`权限:
67+
68+
{% highlight mysql %}
69+
70+
mysql> CREATE USER 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';
71+
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.mydomain.com';
72+
73+
{% endhighlight %}
74+
75+
为了让从数据库知道从哪里开始执行二进制的日志,需要先获取主数据中目前的二进制文件的[位置]
76+
77+
{% highlight mysql %}
78+
79+
mysql> FLUSH TABLES WITH READ LOCK;
80+
81+
{% endhighlight %}
82+
83+
注意,这里需要保持这个窗口打开(session open),否则`READ LOCK`就会失效,
84+
然后新开一个窗口(也许你需要[tmux]),
85+
86+
{% highlight mysql %}
87+
88+
mysql > SHOW MASTER STATUS;
89+
+------------------+----------+--------------+------------------+
90+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
91+
+------------------+----------+--------------+------------------+
92+
| mysql-bin.000003 | 73 | test | manual,mysql |
93+
+------------------+----------+--------------+------------------+
94+
95+
{% endhighlight %}
96+
97+
这里需要记录`File``Position`,稍后在slave的配置中会用到。
98+
99+
如果你只需要复制某一个或一些database,而不是整个数据库,
100+
或者在复制中需要忽略某一个或一些database,那么在`my.cnf`中有以下配置项:
101+
102+
{% highlight %}
103+
104+
binlog_do_db = include_database_name
105+
binlog_do_db = include_database_name2
106+
binlog_ignore_db = include_database_name
107+
binlog_ignore_db = include_database_name2
108+
# 通过不断添加即可实现多个database的配置
109+
110+
{% endhighlight %}
111+
112+
如果你的主数据库中没有数据,那么主数据库的配置工作到此为止,
113+
可以进入从数据库的配置工作了。但是如果你的主数据库中此时已经有了大量的数据,
114+
那么接下来就需要为你的数据库进行一次快照了。
115+
116+
mysql的快照一般都是通过`[mysqldump]`来实现的,具体的例子有以下几种:
117+
118+
{% highlight bash %}
119+
120+
shell> mysqldump -uroot -p database_name > db.sql
121+
shell> mysqldump -uroot -p database_name table_name > db_tb.sql
122+
123+
{% endhighlight %}
124+
125+
至此,主数据库的配置就完成了,接下来就是从数据库的配置了。
126+
127+
2. [Slave Config]
128+
首先与主数据库一样的配置,就是需要在`my.conf`中进行如下配置,
129+
其中的`server-id`必须是全局唯一的
130+
131+
{% highlight %}
132+
133+
[mysqld]
134+
server-id=2
135+
136+
{% endhighlight %}
137+
138+
然后在mysql中执行以下sql语句配置主数据库:
139+
140+
{% highlight mysql %}
141+
142+
mysql> CHANGE MASTER TO
143+
-> MASTER_HOST='master_host_name',
144+
-> MASTER_USER='replication_user_name',
145+
-> MASTER_PASSWORD='replication_password',
146+
-> MASTER_LOG_FILE='recorded_log_file_name',
147+
-> MASTER_LOG_POS=recorded_log_position;
148+
149+
{% endhighlight %}
150+
151+
然后根据是否有已存在的数据来觉得是否需要先导入刚才导出的sql文件,
152+
最后执行
153+
154+
{% highlight mysql %}
155+
156+
mysql> START SLAVE;
157+
158+
{% endhighlight %}
159+
160+
就搞定了。此时可以执行
161+
162+
{% highlight mysql %}
163+
164+
mysql> SHOW SLAVE STATUS\G
165+
166+
{% endhighlight %}
167+
168+
来查看从数据库的状态信息。
169+
170+
3. [Add slave]
171+
通过以上的操作,至此就可以完成主从数据库的从无到有,如果是需要继续添加slave,
172+
则需要继续下去。
173+
174+
官方推荐的做法比较简单,就是选中一个已经存在的slave,关闭掉其服务,
175+
176+
{% highlight mysql %}
177+
178+
shell> mysqladmin shutdown
179+
180+
{% endhighlight %}
181+
182+
然后拷贝整个slave的数据文件夹,然后修改新的slave的`my.cnf`
183+
184+
{% highlight %}
185+
186+
[mysqld]
187+
server-id=3
188+
189+
{% endhighlight %}
190+
191+
至此,全部的相关配置工作就全部结束了。但是,因为我们的从数据库一般都是只读的,
192+
所以如果你愿意的话,还有最后一步:
193+
194+
# Read Only
195+
196+
mysql支持[read_only],在`my.cnf`中设置
197+
{% highlight %}
198+
199+
read_only=1
200+
201+
{% endhighlight %}
202+
203+
就可以防止普通用户的写操作,但是这样并不能防止具有`SUPER privilege`的用户的写操作,
204+
MySQL5.7添加了一个新的配置项[super_read_only],顾名思义,
205+
就是这样设置了之后,连具有`SUPER privilege`的用户也只能read了!
206+
207+
208+
[replication]:http://dev.mysql.com/doc/refman/5.7/en/replication.html
209+
[GTIDs]:http://dev.mysql.com/doc/refman/5.7/en/replication-gtids.html
210+
[官方文档]:http://dev.mysql.com/doc/refman/5.7/en/replication-howto.html
211+
[Master Config]:http://dev.mysql.com/doc/refman/5.7/en/replication-howto-masterbaseconfig.html
212+
[明文存储]:http://dev.mysql.com/doc/refman/5.7/en/slave-logs-status.html
213+
[位置]:http://dev.mysql.com/doc/refman/5.7/en/replication-howto-masterstatus.html
214+
[tmux]:https://tmux.github.io/
215+
[mysqldump]:http://dev.mysql.com/doc/refman/5.7/en/mysqldump.html
216+
[Slave Config]:http://dev.mysql.com/doc/refman/5.7/en/replication-setup-slaves.html
217+
[Add slave]:http://dev.mysql.com/doc/refman/5.7/en/replication-howto-additionalslaves.html
218+
[read_only]:https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_read_only
219+
[super_read_only]:https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_super_read_only
220+
# 参考文章
221+
[https://www.digitalocean.com/community/tutorials/how-to-set-up-master-slave-replication-in-mysql](https://www.digitalocean.com/community/tutorials/how-to-set-up-master-slave-replication-in-mysql)
222+
[https://www.toptal.com/mysql/mysql-master-slave-replication-tutorial](https://www.toptal.com/mysql/mysql-master-slave-replication-tutorial)

0 commit comments

Comments
 (0)