mysql主从复制
主要涉及三个线程:binlog 线程、I/O 线程和 SQL 线程。
由于我这里使用docker搭建,所以需要把配置文件和数据文件映射到宿主机,让容器运行时挂载数据
所以在本地合适的地方创建目录,并添加配置文件
├── master
│ ├── conf
│ └── data
└── slave
├── conf
└── data
master的配置文件为
[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
slave的配置文件为
[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 --name=master-mysql -p 3307:3306 -v /Users/earthchen/docker/data/cluster/mysql/master/conf:/etc/mysql/conf.d -v /Users/earthchen/docker/data/cluster/mysql/master/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
docker run --name=slave-mysql -p 3308:3306 -v /Users/earthchen/docker/data/cluster/mysql/slave/conf:/etc/mysql/conf.d -v /Users/earthchen/docker/data/cluster/mysql/slave/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
创建远程连接用户,并赋予查询数据库,以及查询的权限,可以用于读写分离
mysql -uroot -p
grant SHOW DATABASES,SELECT on *.* to 'slave'@'%' identified by '123456';
FLUSH PRIVILEGES;
最后查看一下master的状态,
show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 675 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
上面的mysql-bin.000004,要根据你自己的输出情况来看
主要就是配置一下master的配置,然后开启集群配置
change master to master_host='192.168.1.140',master_user='reader',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=0;
start slave;
# 查看集群状态,看是否有错误,然后解决掉就可以了
show slave status\G
在master上进行相关操作,然后看slave中是否变化。。。。