当然首先 docker pull mysql

mkdir /usr/local/mysqlData/master/cnf
mkdir /usr/local/mysqlData/master/data
vi /usr/loal/mysqlData/master/cnf/mysql.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql symbolic-links=0 character-set-server = utf8
#skip-networking
innodb_print_all_deadlocks = 1
max_connections = 2000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 28M
key_buffer_size = 4M thread_cache_size = 8 query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M ft_min_word_len = 4 log-bin = mysql-bin
server-id = 1
binlog_format = mixed performance_schema = 0
explicit_defaults_for_timestamp #lower_case_table_names = 1 interactive_timeout = 28800
wait_timeout = 28800 # Recommended in standard MySQL setup sql_mode=NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER,STRICT_TRANS_TABLES [mysqldump]
quick
max_allowed_packet = 16M [myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

docker run -itd -p : --name master -v /usr/local/mysqlData/master/cnf:/etc/mysql/conf.d -v /usr/local/mysqlData/master/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=masterpwd 5709795eeffa

,并添加复制master数据的用户,等下会用上

[root@ ~]# docker exec -it 942c2253509c /bin/bash
root@942c2253509c:/# mysql -uroot -pmasterpwd mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'masterpwd' WITH GRANT OPTION;
Query OK, rows affected, warning (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* to 'reader'@'%' identified by 'readerpwd';

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mkdir /usr/local/mysqlData/slave/cnf
mkdir /usr/local/mysqlData/slave/data
vi /usr/loal/mysqlData/slave/cnf/mysql.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql symbolic-links=0 character-set-server = utf8
#skip-networking
innodb_print_all_deadlocks = 1
max_connections = 2000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 28M
key_buffer_size = 4M thread_cache_size = 8 query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M ft_min_word_len = 4 log-bin = mysql-bin
server-id = 2
binlog_format = mixed performance_schema = 0
explicit_defaults_for_timestamp #lower_case_table_names = 1 interactive_timeout = 28800
wait_timeout = 28800 # Recommended in standard MySQL setup sql_mode=NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER,STRICT_TRANS_TABLES [mysqldump]
quick
max_allowed_packet = 16M [myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

 创建远程连接用户,并赋予查询数据库,以及查询的权限,可以用于读写分离

docker run -itd -p : --name master -v /usr/local/mysqlData/slave/cnf:/etc/mysql/conf.d -v /usr/local/mysqlData/slave/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=slavepwd 5709795eeffa

#进入容器开启root 远程访问
[root@ ~]# docker exec -it 942c2253509c /bin/bash
root@942c2253509c:/# mysql -uroot -pslavepwd
mysql> grant SHOW DATABASES,SELECT on *.* to 'slave'@'%' identified by 'slavepwd';
mysql> FLUSH PRIVILEGES;
Query OK, rows affected (0.00 sec)

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

mysql> change master to master_host='192.168.1.9',master_user='reader',master_password='readerpwd',master_log_file='mysql-bin.000007',master_log_pos=;
Query OK, rows affected, warnings (0.03 sec) mysql> start slave;
Query OK, rows affected (0.03 sec) mysql> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.9
Master_User: reader
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: 0b763a8d1ddd-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

mysql> create database slavetest;
Query OK, row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| slavetest |
| sys |
+--------------------+
rows in set (0.00 sec)

docker mysql 主从复制的更多相关文章

  1. 如何利用docker快速构建MySQL主从复制环境

    在学习MySQL的过程中,常常会测试各种参数的作用.这时候,就需要快速构建出MySQL实例,甚至主从. 考虑如下场景: 譬如我想测试mysqldump在指定--single-transaction参数 ...

  2. Docker搭建 MySQL 主从复制

    为什么选 Docker 搭建主从复制需要两个以上的MySQL, 使用 Docker 非常方便.如果以前没用过,找个简单的文档看看,熟悉一下命令. 搭建过程 1.下载镜像 docker pull mys ...

  3. docker之MySQL主从复制

    MySQL主从复制 主服务器 配置文件目录 mkdir /var/lib/mysql/master/conf.d 数据存储目录 mkdir var/lib/mysql/master/data 配置my ...

  4. 1.Mysql集群------Docker下的Mysql主从复制

    前言: 实话说,我想做的是Mysql集群架构. 我的计划是: 1.实现Docker下的Mysql主从复制 2.实现MyCat基于Mysql的读写分离 3.实现MyCat的分库分表 4.实现pxc集群 ...

  5. 基于Docker Compose搭建mysql主从复制(1主2从)

    系统环境 * 3 Ubuntu 16.04 mysql 8.0.12 docker 18.06.1-ce docker-compose 1.23.0-rc3 *3 ==> PS  ###我用的是 ...

  6. 基于Docker的Mysql主从复制

    基于Docker的Mysql主从复制搭建 为什么基于Docker搭建? 资源有限 虚拟机搭建对机器配置有要求,并且安装mysql步骤繁琐 一台机器上可以运行多个Docker容器 Docker容器之间相 ...

  7. docker+mysql 构建数据库的主从复制

    docker+mysql 构建数据库的主从复制 在最近的项目中,决定将项目改造成数据库读写分离的架构,后续会有博文详细讲述我的开发改造,本文主要记录我是如何一步步的构建数据库的主从复制. 为什么使用d ...

  8. Docker实战之MySQL主从复制

    前言 曾几何时,看着高大上的架构和各位前辈高超的炫技,有没有怦然心动,也想一窥究竟?每当面试的时候,拿着单应用的架构,吹着分库分表的牛X,有没有心里慌的一批? 其实很多时候,我们所缺少的只是对高大上的 ...

  9. Docker搭建MySQL主从复制

    Docker搭建MySQL主从复制 主从服务器上分别安装Docker 1.1 Docker 要求 CentOS 系统的内核版本高于 3.10 [root@localhost ~]# uname -r ...

随机推荐

  1. BSA Network Shell系列-nexec命令

    # nexec ## 1 说明:nexec是远程命令接口引擎 ,用于在远程主机执行命令 nexec [-?] [-t term] [-o] [-i] [-l] [-nohup hostname &qu ...

  2. SCOPE_IDENTITY()

    @@IDENTYITY,SCOPE_IDENTITY的主要区别:在有触发器中而且触发器的内容里面含有插入标识符的操作的时候,@@IDENTITY则返回的是触发器里面新插入标识符的值而SCOPE_IDE ...

  3. redhat linux/CentOS 6/7 如何关闭防火墙?

    redhat linux/CentOS 6/7 如何关闭防火墙?关闭防火墙iptables的具体命令如下: 临时性的完全关闭防火墙,可以不重启机器(但是重启服务器后iptables防火墙服务会自动随系 ...

  4. android 自定义控件用的定时CountDownTimer

    定时执行在一段时候后停止的倒计时,在倒计时执行过程中会在固定间隔时间得到通知(译者:触发onTick方法), 下面的例子显示在一个文本框中显示一个30s倒计时: new CountdownTimer( ...

  5. lodash源码分析之baseFindIndex中的运算符优先级

    我悟出权力本来就是不讲理的--蟑螂就是海米:也悟出要造反,内心必须强大到足以承受任何后果才行. --北岛<城门开> 本文为读 lodash 源码的第十篇,后续文章会更新到这个仓库中,欢迎 ...

  6. GitHub For Beginners: Commit, Push And Go

    In Part 1 of this two-part GitHub tutorial, we examined the main uses for GitHub and bega5n the proc ...

  7. [codility] Lession1 - Iterations - BinaryGap

    Task1: A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is ...

  8. 这可能是我觉得最折腾的C++环境配置(编码+调试)--mac+eclipse

    本着造福大众的心态,万一可能有人喜欢这种环境开发呢对吧~~折腾了一天,又是谷歌又是百度,讲真遇到报错问题搜出的资料挺少的,看来用这类环境开发C++的人不多,毕竟谁没事干放着xcode,vscode不用 ...

  9. template()方法

    template(id, data)方法: id:必传,渲染模板的id. data:可选,一个Object对象. return:传data—>渲染完成html代码:不传data—>一个渲染 ...

  10. C++/C高质量编程指南-笔记

    复习: C/C++高质量编程指南: [规则1-2-1]为了防止头文件被重复引用,应当用ifndef/define/endif结构产生预处理块. [规则1-2-2]用 #include <file ...