docker部署mysql主从

如题

1.拉取镜像

docker pull mysql:5.7

2.创建目录

mkdir -p /opt/mysql/data
mkdir -p /opt/mysql/log
mkdir -p /opt/mysql/conf
chmod 777 -R /opt/mysql/log

3.创建mysql配置文件

vim /opt/mysql/conf/my.cnf

4.启动

docker run -dit -p 3306:3306 -e TZ=Asia/Shanghai  --privileged=true --name mysql_master  -v  /opt/mysql/conf/my.cnf:/etc/my.cnf  -v /opt/mysql/data:/var/lib/mysql -v /opt/mysql/log:/var/log/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7

docker run -dit -p 3306:3306 -e TZ=Asia/Shanghai   --privileged=true --name mysql_slave -v  /opt/mysql/conf/my.cnf:/etc/my.cnf  -v /opt/mysql/data:/var/lib/mysql -v /opt/mysql/log:/var/log/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7

单节点
docker run -dit -p 3306:3306 -e TZ=Asia/Shanghai  --privileged=true --name mysql  -v  /opt/mysql/conf/my.cnf:/etc/mysql/my.cnf  -v /opt/mysql/data:/var/lib/mysql -v /opt/mysql/log:/var/log/mysql -e MYSQL_ROOT_PASSWORD=thundersoft mysql:5.7

5.验证,数据目录,日志等

mysql -u -p
查看时区
	select now();
	show variables like '%time_zone%';

6.创建同步用户

create user 'repl'@'%' identified by 'repl';
grant replication slave,replication client on *.* to 'repl'@'%';
flush privileges;

7. 设置主主

 show master status;


change master to
master_user='repl',
master_password='repl', 
master_host='172.18.3.186', 
master_log_file='mysql-bin.000003', 
master_log_pos=782,
master_port=3306;

change master to
master_user='repl',
master_password='repl', 
master_host='172.18.3.187', 
master_log_file='mysql-bin.000003', 
master_log_pos=782,
master_port=3306;

8.启动主从

start slave;
show slave status \G

更新

因为需要监控zabbix,需要获取mysqld.sock文件,因此进行更新

1.新建目录

mkdir -p /opt/mysql/run/mysqld
chown -R mysql.mysql /opt/mysql/run
chmod 777 -R /opt/mysql/run

2.配置文件更新,且挂载位置需要更新(注意配置文件)

2.1更新sock文件位置
启动挂载目录更新  /opt/mysql/conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf

docker run -dit -p 3306:3306 --privileged=true --name mysql-master-new  -v  /opt/mysql/conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf  -v /opt/mysql/data:/var/lib/mysql -v /opt/mysql/log:/var/log/mysql -v /opt/mysql/run/mysqld:/var/run/mysqld -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7

附录

1、my.cnf

[mysqld]
user=root
default-time-zone='+8:00'
port = 3306
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
datadir=/var/lib/mysql
log-error=/var/log/mysql/error.log
bind-address = 0.0.0.0

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8

2、my.cnf-master

[mysql]
prompt="MySQL [\d]> "
no-auto-rehash

[mysqld]
default-time-zone='+8:00'
log-bin=mysql-bin
server-id=1
#gtid_mode=ON
#enforce-gtid-consistency=true
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin

auto_increment_increment=2
auto_increment_offset=1

log-bin-trust-function-creators=1
local-infile = 0
#skip-grant-tables

port = 3306
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock

datadir=/var/lib/mysql
log-error=/var/log/mysql/error.log
bind-address = 0.0.0.0

init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
lower_case_table_names=1
log_bin_trust_function_creators=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
skip-name-resolve
#skip-networking
back_log = 300

max_connections = 2000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 1024
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 128M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 256M

thread_cache_size = 64

query_cache_type = 1
query_cache_size = 128M
query_cache_limit = 4M

ft_min_word_len = 4

binlog_format = mixed
expire_logs_days = 7

#slow_query_log = 1
#long_query_time = 1
#slow_query_log_file = /tmp/mysql-slow.log

performance_schema = 0
explicit_defaults_for_timestamp

#lower_case_table_names = 1

skip-external-locking

default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 1024M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120

bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1

interactive_timeout = 86400
wait_timeout = 86400

[mysqldump]
quick
max_allowed_packet = 500M

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

3、my.cnf-slave

[mysql]
prompt="MySQL [\d]> "
no-auto-rehash

[mysqld]
default-time-zone='+8:00'
log-bin=mysql-bin
server-id=2
#gtid_mode=ON
#enforce-gtid-consistency=true
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin

auto_increment_increment=2
auto_increment_offset=2

log-bin-trust-function-creators=1
local-infile = 0
#skip-grant-tables

port = 3306
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock

datadir=/var/lib/mysql
log-error=/var/log/mysql/error.log
bind-address = 0.0.0.0

init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
lower_case_table_names=1
log_bin_trust_function_creators=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
skip-name-resolve
#skip-networking
back_log = 300

max_connections = 2000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 1024
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 128M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 256M

thread_cache_size = 64

query_cache_type = 1
query_cache_size = 128M
query_cache_limit = 4M

ft_min_word_len = 4

binlog_format = mixed
expire_logs_days = 7

#slow_query_log = 1
#long_query_time = 1
#slow_query_log_file = /tmp/mysql-slow.log

performance_schema = 0
explicit_defaults_for_timestamp

#lower_case_table_names = 1

skip-external-locking

default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 1024M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120

bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1

interactive_timeout = 86400
wait_timeout = 86400

[mysqldump]
quick
max_allowed_packet = 500M

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

4、mysqld.cnf

[mysql]
prompt="MySQL [\d]> "
no-auto-rehash
port        = 3306
socket      = /opt/mysql/run/mysqld/mysqld.sock
user=zabbix
password=zabbix



[mysqld]
default-time-zone='+8:00'
log-bin=mysql-bin
server-id=1
#gtid_mode=ON
#enforce-gtid-consistency=true
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin

auto_increment_increment=2
auto_increment_offset=1

log-bin-trust-function-creators=1
local-infile = 0
#skip-grant-tables

port = 3306
pid-file	= /opt/mysql/run/mysqld/mysqld.pid
socket		= /opt/mysql/run/mysqld/mysqld.sock

datadir=/var/lib/mysql
log-error=/var/log/mysql/error.log
bind-address = 0.0.0.0

init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
lower_case_table_names=1
log_bin_trust_function_creators=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
skip-name-resolve
#skip-networking
back_log = 300

max_connections = 2000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 1024
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 128M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 256M

thread_cache_size = 64

query_cache_type = 1
query_cache_size = 128M
query_cache_limit = 4M

ft_min_word_len = 4

binlog_format = mixed
expire_logs_days = 7

#slow_query_log = 1
#long_query_time = 1
#slow_query_log_file = /tmp/mysql-slow.log

performance_schema = 0
explicit_defaults_for_timestamp

#lower_case_table_names = 1

skip-external-locking

default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 1024M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120

bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1

interactive_timeout = 86400
wait_timeout = 86400

[mysqldump]
quick
max_allowed_packet = 500M

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

[client]
port        = 3306
socket      = /opt/mysql/run/mysqld/mysqld.sock
user=zabbix
password=zabbix

[mysqladmin]
port        = 3306
socket      = /opt/mysql/run/mysqld/mysqld.sock
user=zabbix
password=zabbix