Skip to content

Commit fcde6d2

Browse files
committed
fix
1 parent 7268a99 commit fcde6d2

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

_posts/2016-12-21-mysql-master-slave-replication.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ tags:
1414
# 场景描述
1515
在官方的[replication]描述中,replication的优点有如下几个方面:
1616

17-
{% highlight %}
17+
{% highlight text %}
1818
Advantages of replication in MySQL include:
1919

2020
- Scale-out solutions -
@@ -55,7 +55,7 @@ mysql5.7之后,除了传统的给予二进制的日志的replication之外,
5555
第一步是设置主数据库,具体需要配置的信息比较简单,就是在`my.cnf`文件中,
5656
修改以下配置(一般就是打开注释即可),
5757

58-
{% highlight %}
58+
{% highlight text %}
5959

6060
server-id = 1
6161
log_bin = /var/log/mysql/mysql-bin.log
@@ -65,7 +65,7 @@ log_bin = /var/log/mysql/mysql-bin.log
6565
由于从数据库需要用户名和密码去链接主数据库,而且用于复制的用户名和密码是[明文存储]的,
6666
所以需要在主数据库中新建专用的用户,新建的用户需要`REPLICATION SLAVE`权限:
6767

68-
{% highlight mysql %}
68+
{% highlight sql %}
6969

7070
mysql> CREATE USER 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';
7171
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.mydomain.com';
@@ -74,7 +74,7 @@ mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.mydomain.com';
7474

7575
为了让从数据库知道从哪里开始执行二进制的日志,需要先获取主数据中目前的二进制文件的[位置]
7676

77-
{% highlight mysql %}
77+
{% highlight sql %}
7878

7979
mysql> FLUSH TABLES WITH READ LOCK;
8080

@@ -83,7 +83,7 @@ mysql> FLUSH TABLES WITH READ LOCK;
8383
注意,这里需要保持这个窗口打开(session open),否则`READ LOCK`就会失效,
8484
然后新开一个窗口(也许你需要[tmux]),
8585

86-
{% highlight mysql %}
86+
{% highlight sql %}
8787

8888
mysql > SHOW MASTER STATUS;
8989
+------------------+----------+--------------+------------------+
@@ -99,7 +99,7 @@ mysql > SHOW MASTER STATUS;
9999
如果你只需要复制某一个或一些database,而不是整个数据库,
100100
或者在复制中需要忽略某一个或一些database,那么在`my.cnf`中有以下配置项:
101101

102-
{% highlight %}
102+
{% highlight text %}
103103

104104
binlog_do_db = database_name
105105
binlog_do_db = include_database_name2
@@ -128,7 +128,7 @@ shell> mysqldump -uroot -p database_name table_name > db_tb.sql
128128
首先与主数据库一样的配置,就是需要在`my.conf`中进行如下配置,
129129
其中的`server-id`必须是全局唯一的
130130

131-
{% highlight %}
131+
{% highlight text %}
132132

133133
[mysqld]
134134
server-id=2
@@ -137,15 +137,15 @@ server-id=2
137137

138138
注意这里需要先创建好slave的数据库,
139139

140-
{% highlight mysql %}
140+
{% highlight sql %}
141141

142142
mysql> CREATE DATABASE if not exists database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
143143

144144
{% endhighlight %}
145145

146146
然后在mysql中执行以下sql语句配置主数据库:
147147

148-
{% highlight mysql %}
148+
{% highlight sql %}
149149

150150
mysql> CHANGE MASTER TO
151151
-> MASTER_HOST='master_host_name',
@@ -159,15 +159,15 @@ mysql> CHANGE MASTER TO
159159
然后根据是否有已存在的数据来觉得是否需要先导入刚才导出的sql文件,
160160
最后执行
161161

162-
{% highlight mysql %}
162+
{% highlight sql %}
163163

164164
mysql> START SLAVE;
165165

166166
{% endhighlight %}
167167

168168
就搞定了。此时可以执行
169169

170-
{% highlight mysql %}
170+
{% highlight sql %}
171171

172172
mysql> SHOW SLAVE STATUS\G
173173

@@ -181,15 +181,15 @@ mysql> SHOW SLAVE STATUS\G
181181

182182
官方推荐的做法比较简单,就是选中一个已经存在的slave,关闭掉其服务,
183183

184-
{% highlight mysql %}
184+
{% highlight sql %}
185185

186186
shell> mysqladmin shutdown
187187

188188
{% endhighlight %}
189189

190190
然后拷贝整个slave的数据文件夹,然后修改新的slave的`my.cnf`
191191

192-
{% highlight %}
192+
{% highlight text %}
193193

194194
[mysqld]
195195
server-id=3
@@ -202,7 +202,7 @@ server-id=3
202202
# Read Only
203203

204204
mysql支持[read_only],在`my.cnf`中设置
205-
{% highlight %}
205+
{% highlight text %}
206206

207207
read_only=1
208208

0 commit comments

Comments
 (0)