环境要求:docker ,centos7
准备工作:
创建docker 容器卷文件(便于文件的后期查看等)目录结构如下:
1:搭建
1.1、docker pull mysql:5.7 #mysql获取
1.2、配置文件编写
1.2.1、 在上诉文件/usr/local/mysqlCluster/mysqlMaster/conf(作为mysql master 文件夹) 创建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
1.2.1、 在上诉文件/usr/local/mysqlCluster/mysqlSlave/conf(作为mysql slave 文件夹) 创建 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
1.3 启动容器
启动主容器
docker run --privileged=true \
--name mysqlmaster -d \
-v /usr/local/mysqlCluster/mysqlMaster/data:/var/lib/mysql \
-v /usr/local/mysqlCluster/mysqlMaster/conf:/etc/mysql/conf.d \
-v /usr/local/mysqlCluster/mysqlMaster/log:/logs -e MYSQL_ROOT_PASSWORD=123456 \
-p 3307:3306 mysql:5.7
启动从容器
docker run --privileged=true \
--name mysqlslave -d \
-v /usr/local/mysqlCluster/mysqlSlave/data:/var/lib/mysql \
-v /usr/local/mysqlCluster/mysqlSlave/conf:/etc/mysql/conf.d \
-v /usr/local/mysqlCluster/mysqlSlave/log:/logs \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3308:3306 mysql:5.7
使用命令查看,docker ps 启动成功,应该有如下图:
正常启动后,进入主库(mysqlmaster) 输入命令可查看到日志文件信息
1.4 设置主库(mysqlmaster)的权限,从库(mysqlslave)访问请求,远程连接
进入成功输入一下命令
命令:
mysql -uroot -p
grant all privileges on *.* to root@"%" identified by "123456";
flush privileges;
1.5 设置从库(mysqlslave)配置,连接主库
change master to master_host='172.17.0.2',master_user='root',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=154;
执行命令后重启 start slave; (作用:让配置起效)
说明:
master_host:master数据库的ip地址,ip可以使用docker inspect mysqlmaster 查看
master_user:连接master数据库的用户名
master_password:master_user的密码
master_log_file:主文件名,对应上面
master_log_pos:主文件位置点,对应上面
1.6 验证是否起效
SHOW SLAVE STATUS;
其中属性:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
都是yes 表示成功。
2:本人搭建的时候遇到的问题:
远程工具navicat无法连接, 主库开启了远程权限,从库忘记开了
主库show master status; 日志文件没有生成,最开始时候创建的my.cnf 文件简单,只有server-id 及bin-log,其他都有没写,后期上网查找资料补录其他属性信息。
云服务器,需要在云控制台打开对应端口防火墙,本例使用的是3307、3308两个端口