前言

自己在百度、Google一番踩坑搭建成功后,记录一下,也希望后来人不再被这些坑到。

这里为了方便使用 docker,不会的同学请移步相关 Docker 教程。

正文

1. 启动 mysql

  1. #启动 master
  2. docker run --name master -e MYSQL_ROOT_PASSWORD=123456 -d mysql
  3. #启动 slave
  4. docker run --name slave -e MYSQL_ROOT_PASSWORD=123456 -d mysql

备注:--name 是指定容器名称;-e MYSQL_ROOT_PASSWORD 是指定mysql密码

2. 修改 mysql 配置

docker 的正确用法应该是基于mysql镜像,创建两个新的镜像,这里为了简单,直接进入容器修改

修改 master 的 mysql 配置

  1. docker exec -it [master容器id] bash
  1. echo '[mysqld]
  2. pid-file = /var/run/mysqld/mysqld.pid
  3. socket = /var/run/mysqld/mysqld.sock
  4. datadir = /var/lib/mysql
  5. secure-file-priv= NULL
  6. symbolic-links=0
  7. #启用二进制日志
  8. log-bin=mysql-bin
  9. #设置服务器唯一Id,这里省事直接写1
  10. server-id=1
  11. !includedir /etc/mysql/conf.d/' > /etc/mysql/my.cnf

备注:第二段请一口气复制,它们是一句..

这里因为容器内没有装vim所以使用这种方式,其实就是加上log-bin=mysql-binserver-id=1这两个配置

修改 slave 的 mysql 配置

其实一样,只是改个server-id

  1. docker exec -it [slave容器id] bash
  1. echo '[mysqld]
  2. pid-file = /var/run/mysqld/mysqld.pid
  3. socket = /var/run/mysqld/mysqld.sock
  4. datadir = /var/lib/mysql
  5. secure-file-priv= NULL
  6. symbolic-links=0
  7. #启用二进制日志
  8. log-bin=mysql-bin
  9. #设置服务器唯一Id
  10. server-id=2
  11. !includedir /etc/mysql/conf.d/' > /etc/mysql/my.cnf

重启容器

  1. docker restart [master容器id] [slave容器id]

3. 配置同步

登录 master 获取信息

  1. docker exec -it [master容器id] mysql -uroot -p123456
  2. mysql> show master status;
  3. +------------------+----------+--------------+------------------+-------------------+
  4. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  5. +------------------+----------+--------------+------------------+-------------------+
  6. | mysql-bin.000001 | 155 | | | |
  7. +------------------+----------+--------------+------------------+-------------------+
  8. 1 row in set (0.00 sec)

主要记住 mysql-bin.000001 和 155 这两个值,一个是文件,一个是偏移量

登录 slave 设置同步

  1. docker exec -it [slave容器id] mysql -uroot -p123456
  2. mysql> change master to master_host='172.17.0.2',master_user='root',master_log_file='mysql-bin.000001',master_log_pos=155,master_port=3306,master_password='123456';
  3. msql> start slave;

备注:这里的 ip 是 master 的 ip,可以通过 docker inspect [容器id]|grep IPA 查看 IP

4. 检验成功

通过 show slave status\G; 看到

失败了..

Last_IO_Error: error connecting to master 'root@172.17.0.2:3306' - retry-time: 60 retries: 1 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.

不安全的连接?

最后在 Mysql 官网发现端倪,需要先在 slave 上连接 master 获取 public-key,如下

mysql --ssl-mode=DISABLED -h [masterIP] -uroot -p123456 --get-server-public-key

然后重启下 slave 线程

stop slave;

start slave;

查看状态

show slave status\G;

可以看到下面两个yes就说明成功了

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

后记

关于最后那个连接不安全的错误,应该是 mysql8 才有的,不过具体原因还没空去研究

附上操作日志

  1. ################【启动mysql】################
  2. deepin@DeepinBook:~$ docker run --name master -e MYSQL_ROOT_PASSWORD=123456 -d mysql
  3. 6f0d597fda22325f35eef4d00356238e4058d3dc2122177fe1bdc2a9cca554b2
  4. deepin@DeepinBook:~$ docker run --name slave -e MYSQL_ROOT_PASSWORD=123456 -d mysql
  5. 5dca640e2ceb4744a1e85402c50a0134ad981a9cc658f91058bd739c80fb22b4
  6. ################【修改mysql配置】#############
  7. deepin@DeepinBook:~$ docker exec -it 6f bash
  8. root@6f0d597fda22:/# cat /etc/mysql/my.cnf
  9. # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; version 2 of the License.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program; if not, write to the Free Software
  22. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. #
  24. # The MySQL Server configuration file.
  25. #
  26. # For explanations see
  27. # http://dev.mysql.com/doc/mysql/en/server-system-variables.html
  28. [mysqld]
  29. pid-file = /var/run/mysqld/mysqld.pid
  30. socket = /var/run/mysqld/mysqld.sock
  31. datadir = /var/lib/mysql
  32. secure-file-priv= NULL
  33. # Disabling symbolic-links is recommended to prevent assorted security risks
  34. symbolic-links=0
  35. # Custom config should go here
  36. !includedir /etc/mysql/conf.d/
  37. root@6f0d597fda22:/# echo '[mysqld]
  38. > pid-file = /var/run/mysqld/mysqld.pid
  39. > socket = /var/run/mysqld/mysqld.sock
  40. > datadir = /var/lib/mysql
  41. > secure-file-priv= NULL
  42. > symbolic-links=0
  43. > log-bin=mysql-bin
  44. > server-id=1
  45. > !includedir /etc/mysql/conf.d/' > /etc/mysql/my.cnf
  46. root@6f0d597fda22:/# cat /etc/mysql/my.cnf
  47. [mysqld]
  48. pid-file = /var/run/mysqld/mysqld.pid
  49. socket = /var/run/mysqld/mysqld.sock
  50. datadir = /var/lib/mysql
  51. secure-file-priv= NULL
  52. symbolic-links=0
  53. log-bin=mysql-bin
  54. server-id=1
  55. !includedir /etc/mysql/conf.d/
  56. root@6f0d597fda22:/# exit
  57. deepin@DeepinBook:~$ docker exec -it 5d bash
  58. root@5dca640e2ceb:/# echo '[mysqld]
  59. > pid-file = /var/run/mysqld/mysqld.pid
  60. > socket = /var/run/mysqld/mysqld.sock
  61. > datadir = /var/lib/mysql
  62. > secure-file-priv= NULL
  63. > symbolic-links=0
  64. > #启用二进制日志
  65. > log-bin=mysql-bin
  66. > #设置服务器唯一Id
  67. > server-id=2
  68. > !includedir /etc/mysql/conf.d/' > /etc/mysql/my.cnf
  69. root@5dca640e2ceb:/#
  70. root@5dca640e2ceb:/#
  71. root@5dca640e2ceb:/# exit
  72. exit
  73. deepin@DeepinBook:~$ docker restart 6f 5d
  74. 6f
  75. 5d
  76. ################【配置主从】#############
  77. deepin@DeepinBook:~$ docker exec -it 6f mysql -uroot -p123456
  78. mysql: [Warning] Using a password on the command line interface can be insecure.
  79. Welcome to the MySQL monitor. Commands end with ; or \g.
  80. Your MySQL connection id is 8
  81. Server version: 8.0.17 MySQL Community Server - GPL
  82. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  83. Oracle is a registered trademark of Oracle Corporation and/or its
  84. affiliates. Other names may be trademarks of their respective
  85. owners.
  86. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  87. mysql> show master status;
  88. +------------------+----------+--------------+------------------+-------------------+
  89. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  90. +------------------+----------+--------------+------------------+-------------------+
  91. | mysql-bin.000001 | 155 | | | |
  92. +------------------+----------+--------------+------------------+-------------------+
  93. 1 row in set (0.00 sec)
  94. mysql> exit
  95. Bye
  96. deepin@DeepinBook:~$ docker exec -it 5d mysql -uroot -p123456
  97. mysql: [Warning] Using a password on the command line interface can be insecure.
  98. Welcome to the MySQL monitor. Commands end with ; or \g.
  99. Your MySQL connection id is 8
  100. Server version: 8.0.17 MySQL Community Server - GPL
  101. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  102. Oracle is a registered trademark of Oracle Corporation and/or its
  103. affiliates. Other names may be trademarks of their respective
  104. owners.
  105. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  106. mysql> change master to master_host='172.17.0.2',master_user='root',master_log_file='mysql-bin.000001',master_log_pos=155,master_port=3306,master_password='123456';
  107. Query OK, 0 rows affected, 2 warnings (0.04 sec)
  108. mysql> start slave;
  109. Query OK, 0 rows affected (0.01 sec)
  110. mysql> show slave status\G;
  111. *************************** 1. row ***************************
  112. Slave_IO_State: Connecting to master
  113. Master_Host: 172.17.0.2
  114. Master_User: root
  115. Master_Port: 3306
  116. Connect_Retry: 60
  117. Master_Log_File: mysql-bin.000001
  118. Read_Master_Log_Pos: 155
  119. Relay_Log_File: 5dca640e2ceb-relay-bin.000001
  120. Relay_Log_Pos: 4
  121. Relay_Master_Log_File: mysql-bin.000001
  122. Slave_IO_Running: Connecting
  123. Slave_SQL_Running: Yes
  124. Replicate_Do_DB:
  125. Replicate_Ignore_DB:
  126. Replicate_Do_Table:
  127. Replicate_Ignore_Table:
  128. Replicate_Wild_Do_Table:
  129. Replicate_Wild_Ignore_Table:
  130. Last_Errno: 0
  131. Last_Error:
  132. Skip_Counter: 0
  133. Exec_Master_Log_Pos: 155
  134. Relay_Log_Space: 155
  135. Until_Condition: None
  136. Until_Log_File:
  137. Until_Log_Pos: 0
  138. Master_SSL_Allowed: No
  139. Master_SSL_CA_File:
  140. Master_SSL_CA_Path:
  141. Master_SSL_Cert:
  142. Master_SSL_Cipher:
  143. Master_SSL_Key:
  144. Seconds_Behind_Master: NULL
  145. Master_SSL_Verify_Server_Cert: No
  146. Last_IO_Errno: 2061
  147. Last_IO_Error: error connecting to master 'root@172.17.0.2:3306' - retry-time: 60 retries: 8 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
  148. Last_SQL_Errno: 0
  149. Last_SQL_Error:
  150. Replicate_Ignore_Server_Ids:
  151. Master_Server_Id: 0
  152. Master_UUID:
  153. Master_Info_File: mysql.slave_master_info
  154. SQL_Delay: 0
  155. SQL_Remaining_Delay: NULL
  156. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  157. Master_Retry_Count: 86400
  158. Master_Bind:
  159. Last_IO_Error_Timestamp: 191008 10:43:52
  160. Last_SQL_Error_Timestamp:
  161. Master_SSL_Crl:
  162. Master_SSL_Crlpath:
  163. Retrieved_Gtid_Set:
  164. Executed_Gtid_Set:
  165. Auto_Position: 0
  166. Replicate_Rewrite_DB:
  167. Channel_Name:
  168. Master_TLS_Version:
  169. Master_public_key_path:
  170. Get_master_public_key: 0
  171. Network_Namespace:
  172. 1 row in set (0.00 sec)
  173. ERROR:
  174. No query specified
  175. mysql> exit
  176. Bye
  177. deepin@DeepinBook:~$ docker exec -it 5d bash
  178. root@5dca640e2ceb:/# mysql --ssl-mode=DISABLED -h172.17.0.2 -uroot -p123456 --get-server-public-key
  179. mysql: [Warning] Using a password on the command line interface can be insecure.
  180. Welcome to the MySQL monitor. Commands end with ; or \g.
  181. Your MySQL connection id is 20
  182. Server version: 8.0.17 MySQL Community Server - GPL
  183. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  184. Oracle is a registered trademark of Oracle Corporation and/or its
  185. affiliates. Other names may be trademarks of their respective
  186. owners.
  187. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  188. mysql> exit
  189. Bye
  190. root@5dca640e2ceb:/# mysql -uroot -p123456
  191. mysql: [Warning] Using a password on the command line interface can be insecure.
  192. Welcome to the MySQL monitor. Commands end with ; or \g.
  193. Your MySQL connection id is 11
  194. Server version: 8.0.17 MySQL Community Server - GPL
  195. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  196. Oracle is a registered trademark of Oracle Corporation and/or its
  197. affiliates. Other names may be trademarks of their respective
  198. owners.
  199. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  200. mysql> show slave status\G;
  201. *************************** 1. row ***************************
  202. Slave_IO_State: Connecting to master
  203. Master_Host: 172.17.0.2
  204. Master_User: root
  205. Master_Port: 3306
  206. Connect_Retry: 60
  207. Master_Log_File: mysql-bin.000001
  208. Read_Master_Log_Pos: 155
  209. Relay_Log_File: 5dca640e2ceb-relay-bin.000001
  210. Relay_Log_Pos: 4
  211. Relay_Master_Log_File: mysql-bin.000001
  212. Slave_IO_Running: Connecting
  213. Slave_SQL_Running: Yes
  214. Replicate_Do_DB:
  215. Replicate_Ignore_DB:
  216. Replicate_Do_Table:
  217. Replicate_Ignore_Table:
  218. Replicate_Wild_Do_Table:
  219. Replicate_Wild_Ignore_Table:
  220. Last_Errno: 0
  221. Last_Error:
  222. Skip_Counter: 0
  223. Exec_Master_Log_Pos: 155
  224. Relay_Log_Space: 155
  225. Until_Condition: None
  226. Until_Log_File:
  227. Until_Log_Pos: 0
  228. Master_SSL_Allowed: No
  229. Master_SSL_CA_File:
  230. Master_SSL_CA_Path:
  231. Master_SSL_Cert:
  232. Master_SSL_Cipher:
  233. Master_SSL_Key:
  234. Seconds_Behind_Master: NULL
  235. Master_SSL_Verify_Server_Cert: No
  236. Last_IO_Errno: 2061
  237. Last_IO_Error: error connecting to master 'root@172.17.0.2:3306' - retry-time: 60 retries: 11 message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
  238. Last_SQL_Errno: 0
  239. Last_SQL_Error:
  240. Replicate_Ignore_Server_Ids:
  241. Master_Server_Id: 0
  242. Master_UUID:
  243. Master_Info_File: mysql.slave_master_info
  244. SQL_Delay: 0
  245. SQL_Remaining_Delay: NULL
  246. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  247. Master_Retry_Count: 86400
  248. Master_Bind:
  249. Last_IO_Error_Timestamp: 191008 10:46:52
  250. Last_SQL_Error_Timestamp:
  251. Master_SSL_Crl:
  252. Master_SSL_Crlpath:
  253. Retrieved_Gtid_Set:
  254. Executed_Gtid_Set:
  255. Auto_Position: 0
  256. Replicate_Rewrite_DB:
  257. Channel_Name:
  258. Master_TLS_Version:
  259. Master_public_key_path:
  260. Get_master_public_key: 0
  261. Network_Namespace:
  262. 1 row in set (0.00 sec)
  263. ERROR:
  264. No query specified
  265. mysql> stop slave;
  266. Query OK, 0 rows affected (0.01 sec)
  267. mysql> start slave;
  268. Query OK, 0 rows affected (0.00 sec)
  269. mysql> show slave status\G;
  270. *************************** 1. row ***************************
  271. Slave_IO_State: Waiting for master to send event
  272. Master_Host: 172.17.0.2
  273. Master_User: root
  274. Master_Port: 3306
  275. Connect_Retry: 60
  276. Master_Log_File: mysql-bin.000001
  277. Read_Master_Log_Pos: 155
  278. Relay_Log_File: 5dca640e2ceb-relay-bin.000002
  279. Relay_Log_Pos: 322
  280. Relay_Master_Log_File: mysql-bin.000001
  281. Slave_IO_Running: Yes
  282. Slave_SQL_Running: Yes
  283. Replicate_Do_DB:
  284. Replicate_Ignore_DB:
  285. Replicate_Do_Table:
  286. Replicate_Ignore_Table:
  287. Replicate_Wild_Do_Table:
  288. Replicate_Wild_Ignore_Table:
  289. Last_Errno: 0
  290. Last_Error:
  291. Skip_Counter: 0
  292. Exec_Master_Log_Pos: 155
  293. Relay_Log_Space: 537
  294. Until_Condition: None
  295. Until_Log_File:
  296. Until_Log_Pos: 0
  297. Master_SSL_Allowed: No
  298. Master_SSL_CA_File:
  299. Master_SSL_CA_Path:
  300. Master_SSL_Cert:
  301. Master_SSL_Cipher:
  302. Master_SSL_Key:
  303. Seconds_Behind_Master: 0
  304. Master_SSL_Verify_Server_Cert: No
  305. Last_IO_Errno: 0
  306. Last_IO_Error:
  307. Last_SQL_Errno: 0
  308. Last_SQL_Error:
  309. Replicate_Ignore_Server_Ids:
  310. Master_Server_Id: 1
  311. Master_UUID: 48507a05-e9b2-11e9-ae20-0242ac110002
  312. Master_Info_File: mysql.slave_master_info
  313. SQL_Delay: 0
  314. SQL_Remaining_Delay: NULL
  315. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  316. Master_Retry_Count: 86400
  317. Master_Bind:
  318. Last_IO_Error_Timestamp:
  319. Last_SQL_Error_Timestamp:
  320. Master_SSL_Crl:
  321. Master_SSL_Crlpath:
  322. Retrieved_Gtid_Set:
  323. Executed_Gtid_Set:
  324. Auto_Position: 0
  325. Replicate_Rewrite_DB:
  326. Channel_Name:
  327. Master_TLS_Version:
  328. Master_public_key_path:
  329. Get_master_public_key: 0
  330. Network_Namespace:
  331. 1 row in set (0.00 sec)
  332. ERROR:
  333. No query specified

Mysql 主从复制搭建-极简版的更多相关文章

  1. Underscore源码阅读极简版入门

    看了网上的一些资料,发现大家都写得太复杂,让新手难以入门.于是写了这个极简版的Underscore源码阅读. 源码: https://github.com/hanzichi/underscore-an ...

  2. js消除小游戏(极简版)

    js小游戏极简版 (1) 基础布局 <div class = "box"> <p></p> <div class="div&qu ...

  3. SimpleThreadPool极简版

    package com.dwz.concurrency.chapter13; import java.util.ArrayList; import java.util.LinkedList; impo ...

  4. 1分钟搭建极简mock server

    1.无聊的背景.起源: 如今的业务系统越来越复杂庞大,各个功能直接的调用也是多如牛毛,但如果在联调的时候,恰好被调的接口正在开发,怎么办?傻傻的等么,不存在的!这时会搭建一些server来进行mock ...

  5. CentOS下使用Postfix + Dovecot + Dnsmasq搭建极简局域网邮件系统

    背景 开发环境为局域网,工作内容需要经常查看邮件文件(*.eml),可恶的Foxmail必须验证账户才能进入主界面,才能打开eml文件查看. 无奈搭一个局域网内的邮件系统吧.极简搭建,仅用于通过Fox ...

  6. 基于layui和bootstrap搭建极简后台管理框架

    年前无聊,想自己搭建一个后台管理框架,对比了easyui.Extjs.H-ui.H+UI.layui几个框架,easyui和Extjs虽然功能强大但是界面实在是接受不了,H+UI和layuiAdmin ...

  7. (转)MySQL 主从复制搭建,基于日志(binlog

    原文:http://blog.jobbole.com/110934/ 什么是MySQL主从复制 简单来说,就是保证主SQL(Master)和从SQL(Slave)的数据是一致性的,向Master插入数 ...

  8. Mysql 主从复制搭建

    Mysql 主重复制搭建 Linux版本:Linux Centos 6.4 32位 Mysql版本:Mysql-5.6.38-linux-glibc2.12-i686 Mysql安装:Mysql安装教 ...

  9. MYSQL主从复制搭建及切换操作(GTID与传统)

    结构如下: MYSQL主从复制方式有默认的复制方式异步复制,5.5版本之后半同步复制,5.6版本之后新增GTID复制,包括5.7版本的多源复制. MYSQL版本:5.7.20 操作系统版本:linux ...

随机推荐

  1. Spring Boot应用启动的三种方式

    Spring Boot应用HelloWorld的三种启动方式: 项目的创建可以在http://start.spring.io/网站中进行项目的创建. 首先项目结构: 1.  通过main方法的形式启动 ...

  2. luogu P3572 [POI2014]PTA-Little Bird

    题目描述 从1开始,跳到比当前矮的不消耗体力,否则消耗一点体力,每次询问有一个步伐限制,求每次最少耗费多少体力 单调队列优化动态规划 #include<cstdio> #include&l ...

  3. 3D硬件加速提升动画性能 与 z-index属性

    目录 1. chrome Layer borders 2. 层创建标准 3. 例子 总结 1. chrome Layer borders <WebKit技术内幕>第二章介绍了网页的结构,其 ...

  4. spring security 权限安全认证框架-入门(一)

    spring security 概述: Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架.它是保护基于spring的应用程序的实际标准. Spring Security ...

  5. ARTS-S golang常用代码段

    通过http下载文件 func DownloadFile(filepath string, url string) error { out, err := os.Create(filepath) if ...

  6. Asp.net Core 异常日志与API返回值处理

    需求: 1.对异常进行捕获记录日志 并且修改返回值给前端 解释: ILogger4是自定义的一个日志,更改它就好 解决方案1: 使用中间件进行异常捕获并且修改其返回值 public class Err ...

  7. Net Core 基于AngleSharp的HTML转实体工具

    最近这几天在采集一些房产信息网站的二手房产数据.采用的是.net core 2.2+AngleSharp做的,放在自己服务器上跑着玩.写着写着,发现好麻烦.原因如下 部分代码如下图 1.每个节点都要手 ...

  8. python学习-if

    # 判断"""if 条件(True/False): 条件为真时,执行的代码(要干的事情)[elif 条件: 条件为真时,执行的代码(要干的事情)elif 条件: 条件为真 ...

  9. 【Java】在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

    题目描述: 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整 ...

  10. TOMCAT启动报错:org.apache.tomcat.jni.Error: 730055

    TOMCAT启动报错:org.apache.tomcat.jni.Error: 730055 具体原因:不清楚 解决方式:重启应用服务器后,再启动tomcat就可以了 欢迎关注公众号,学习kettle ...