Centos7 mysql 5.6 数据库主主复制

主机系统是:centos7.4 64位

内存:2G

CPU:4核心

安装Mysql

docker pull mysql

主的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock

[mysqld]
binlog_cache_size = 64K
thread_stack = 256K
join_buffer_size = 1024K
query_cache_type = 1
max_heap_table_size = 128M
port = 3306
socket = /tmp/mysql.sock
datadir = /www/server/data
skip-external-locking
performance_schema_max_table_instances=400
table_definition_cache=400
key_buffer_size = 128M
max_allowed_packet = 100G
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 8K
read_buffer_size = 768K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 64
query_cache_size = 128M
tmp_table_size = 128M

explicit_defaults_for_timestamp = true
#skip-networking
#skip-name-resolve
max_connections = 100
max_connect_errors = 100
open_files_limit = 65535
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

log-bin=mysql-bin
binlog_format=mixed
server-id = 1
expire_logs_days = 10
slow_query_log=1
slow-query-log-file=/www/server/data/mysql-slow.log
long_query_time=3
#log_queries_not_using_indexes=on

#loose-innodb-trx=0
#loose-innodb-locks=0
#loose-innodb-lock-waits=0
#loose-innodb-cmp=0
#loose-innodb-cmp-per-index=0
#loose-innodb-cmp-per-index-reset=0
#loose-innodb-cmp-reset=0
#loose-innodb-cmpmem=0
#loose-innodb-cmpmem-reset=0
#loose-innodb-buffer-page=0
#loose-innodb-buffer-page-lru=0
#loose-innodb-buffer-pool-stats=0
#loose-innodb-metrics=0
#loose-innodb-ft-default-stopword=0
#loose-innodb-ft-inserted=0
#loose-innodb-ft-deleted=0
#loose-innodb-ft-being-deleted=0
#loose-innodb-ft-config=0
#loose-innodb-ft-index-cache=0
#loose-innodb-ft-index-table=0
#loose-innodb-sys-tables=0
#loose-innodb-sys-tablestats=0
#loose-innodb-sys-indexes=0
#loose-innodb-sys-columns=0
#loose-innodb-sys-fields=0
#loose-innodb-sys-foreign=0
#loose-innodb-sys-foreign-cols=0

default_storage_engine = InnoDB
innodb_data_home_dir = /www/server/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /www/server/data
innodb_buffer_pool_size = 256M
innodb_log_file_size = 64M
innodb_log_buffer_size = 368M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 120
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 1
innodb_write_io_threads = 1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 768K
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

从节点一抹一样只是Server_id 修改了一下而已

在主备份一下数据库。全备吧。这样也就行

1
#mysqldump –uroot –p’123456’ --all-databases >liang.sql

scp 发送到对端服务器

从服务器把文件导入进去

1
mysql –uroot –p’123456’   <liang.sql

进入主服务器。添加一个用户授权

1
mysql>grant replication slave on *.* to  rep@'192.168.132.%' identified by '123456';

这里给主服务器锁表

1
2
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.08 sec)

查看一下用户

1
mysql> select user,host from mysql.user;

后面show master

现在在从服务器操作

1
2
3
4
5
6
7
8
9
10
11
12
13
在salves实例中操作
Mysql>start slave ##########开启主从复制开关
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.132.139',
-> MASTER_PORT=3306,
-> MASTER_USER='rep',
-> MASTER_PASSWORD='123456',
-> MASTER_LOG_FILE='mysql-bin.000002',
-> MASTER_LOG_POS=120;

Mysql>start slave ##########开启主从复制开关
查看slave 的一个状态
mysql> show slave status\G;

这两个都为yes时候表示是已经成功了

测试是否同步成功了。我在matsrt中新建一个数据库看看salave是否能同步成功。

我在master中新建了一个aa数据库,现在查看一下salves是否同步了

同步完之后,然后就设置从服务器为主节点

首先给从服务器锁表

1
2
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.08 sec)

查看master status

然后从节点也是一样的

1
2
3
4
5
6
7
8
9
10
11
12
13
在salves实例中操作
Mysql>start slave ##########开启主从复制开关
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.132.139',
-> MASTER_PORT=3306,
-> MASTER_USER='rep',
-> MASTER_PASSWORD='123456',
-> MASTER_LOG_FILE='mysql-bin.000002',
-> MASTER_LOG_POS=120;

Mysql>start slave ##########开启主从复制开关
查看slave 的一个状态
mysql> show slave status\G;

然后查看到两个yes 就在两边自己新建数据库看看看

0%