为我的博客网站数据库使用Docker实现MySQL主从同步

前言

个人博客使用了阿里云的服务器,此文是笔者实现WordPress+Docker+MySQL主从同步架构的记录。

环境

Docker version 1.13.1

LNMP:

  • 阿里云centos 7.3
  • nginx version: nginx/1.12.2
  • mysql5.6(Docker镜像)
  • PHP 5.4.16

WordPress:

  • wordpress-4.9.4-zh_CN.zip

步骤

LNMP结构已经搭建好这里不再累赘,Docker搭建MySQL主从两个容器映射到了主机的3307端口(MySQL主节点)和3307端口(MySQL从节点)。

1、下载mysql5.6镜像

[root@tender]# docker pull mysql:5.6

2、为容器创建映射的配置文件目录、数据目录、和日志目录

[root@tender]# tree .
├── mysql_master
│   ├── conf
│   ├── data
│   └── logs
├── mysql_slave
    ├── conf
    ├── data
    └── logs

3、在mysql_master、mysql_slave的conf文件夹下创建MySQL主从结构配置文件,映射给容器时会使用这个配置文件启动MySQL。

mysql_master

[root@tender]# vim mysql_master/conf/my.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

mysql_slave

[root@tender]# vim mysql_slave/conf/my.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

4、 运行容器
mysql_master:

docker run -p 3307:3306 --name mastermysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6

mysql_slave:

docker run -p 3307:3306 --name mastermysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6

命令说明:

  • -p 3306:3306:将容器的 3306 端口映射到主机的 3306 端口。
  • -v $PWD/conf:/etc/mysql/conf.d:将主机当前目录下的conf/my.cnf 挂载到容器的 /etc/mysql/my.cnf。
  • -v $PWD/logs:/logs:将主机当前目录下的 logs 目录挂载到容器的 /logs。
  • -v $PWD/data:/var/lib/mysql :将主机当前目录下的data目录挂载到容器的 /var/lib/mysql 。
  • -e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码。

5、进入容器设置主从同步
mysql_master:

[root@tender]# docker exec -it mastermysql /bin/bash
root@cbf16fcfbc85:/# mysql -uroot -p
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 49
Server version: 5.6.40-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant all privileges on *.* to repluser@'%' identified by '123456';
 1 rows in set (0.01 sec)
mysql> flush privileges;
 1 rows in set (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |   1164 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> exit
Bye
root@cbf16fcfbc85:/# exit
exit
[root@tender]#

mysql_slave:

[root@tender]# docker exec -it slavemysql /bin/bash
root@acdf13bf370a:/# mysql -uroot -p
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.6.40-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> change master to
    -> master_host='172.17.0.2',
    -> master_port=3306,
    -> master_user='relpuser',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000004',
    -> master_log_pos=1163;
mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
			……
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
			……

在MySQL从节点设置主节点时,master_host选项需要得到mastermysql那台容器的ip,我们可以连接上mastermysql,键入命令cat /etc/hosts 可以看到容器ip:

root@cbf16fcfbc85:/# cat /etc/hosts
127.0.0.1	localhost
……
172.17.0.2	cbf16fcfbc85

至此,Docker+MySQL主从结构搭建完成。使用mysql命令指定端口即可连入主库。

6、wordpress搭建
软件压缩包:wordpress-4.9.4-zh_CN.zip

[root@tender]# unzip wordpress-4.9.4-zh_CN.zip
[root@tender]# cd wordpress
[root@tender]# cp -Rr ./* /usr/local/nginx/html/

# 进入mastermysql,创建wordpress数据库和用户
[root@tender]# docker exec -it mastermysql /bin/bash
root@cbf16fcfbc85:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 51
Server version: 5.6.40-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database wordpress;
mysql> grant all on wordpress.* to wordpress@"%" identified by '123456';

最后,在浏览器访问博客IP地址,进入WordPress初始化设置界面,在数据库名填上wordpress,数据库地址填上服务器ip:3307,也就是映射给mastermysql的端口,初始化设置完毕后,wordpress数据库会自动生成博客系统所需要的表。
为我的博客网站数据库使用Docker实现MySQL主从同步_第1张图片
为我的博客网站数据库使用Docker实现MySQL主从同步_第2张图片

再在浏览器访问博客IP地址,进入博客,wordpress+Docker+MySQL主从结构搭建完成。
为我的博客网站数据库使用Docker实现MySQL主从同步_第3张图片

你可能感兴趣的:(Linux,教程记录,Docker,LNMP,MySQL,WordPress)