设为首页 收藏本站
查看: 606|回复: 0

[经验分享] MySQL主从复制--MySQL5.6基于GTID及多线程复制

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2016-1-18 09:26:25 | 显示全部楼层 |阅读模式
大纲
一、系统环境
二、MySQL初始化安装过程
三、基于GTID的主从模式配置过程


一、系统环境
系统环境
CentOS5.8 x86_64
master.network.com    master    172.16.1.101
slave.network.com     slave     172.16.1.105
软件包
  • mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz(二进制通用安装包)



拓扑图
wKiom1aZya_QYkAoAADt1IBXJMY763.jpg


二、MySQL初始化安装过程
1、时间同步
1
2
3
4
5
6
7
8
9
[iyunv@master ~]# ntpdate s2c.time.edu.cn
[iyunv@slave ~]# ntpdate s2c.time.edu.cn

可根据需要在每个节点上定义crontab任务
[iyunv@master ~]# which ntpdate
/sbin/ntpdate
[iyunv@master ~]# echo "*/5 * * * * /sbin/ntpdate s2c.time.edu.cn &> /dev/null" >> /var/spool/cron/root
[iyunv@master ~]# crontab -l
*/5 * * * * /sbin/ntpdate s2c.time.edu.cn &> /dev/null



2、主机名称要与uname -n保持一致,并通过/etc/hosts解析
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
master
[iyunv@master ~]# hostname master.network.com
[iyunv@master ~]# uname -n
master.network.com
[iyunv@master ~]# sed -i 's@\(HOSTNAME=\).*@\1master.network.com@g'  /etc/sysconfig/network

slave
[iyunv@slave ~]# hostname slave.network.com
[iyunv@slave ~]# uname -n
slave.network.com
[iyunv@slave ~]# sed -i 's@\(HOSTNAME=\).*@\1slave.network.com@g'  /etc/sysconfig/network

master添加hosts解析
[iyunv@master ~]# vim /etc/hosts
[iyunv@master ~]# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1       CentOS5.8 CentOS5 localhost.localdomain localhost
::1     localhost6.localdomain6 localhost6
172.16.1.101    master.network.com     master
172.16.1.105    slave.network.com  slave

拷贝此hosts文件至slave
[iyunv@master ~]# scp /etc/hosts slave:/etc/
root@slave's password:
hosts                  100%  233     0.2KB/s   00:00



3、关闭iptables和selinux
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
master
[iyunv@master ~]# service iptables stop
[iyunv@master ~]# vim /etc/sysconfig/selinux
[iyunv@master ~]# cat /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#   enforcing - SELinux security policy is enforced.
#   permissive - SELinux prints warnings instead of enforcing.
#   disabled - SELinux is fully disabled.
#SELINUX=permissive
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#   targeted - Only targeted network daemons are protected.
#   strict - Full SELinux protection.
SELINUXTYPE=targeted

slave
[iyunv@slave ~]# service iptables stop
[iyunv@slave ~]# vim /etc/sysconfig/selinux
[iyunv@slave ~]# cat /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#   enforcing - SELinux security policy is enforced.
#   permissive - SELinux prints warnings instead of enforcing.
#   disabled - SELinux is fully disabled.
#SELINUX=permissive
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#   targeted - Only targeted network daemons are protected.
#   strict - Full SELinux protection.
SELINUXTYPE=targeted



4、Master安装并配置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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
数据目录底层最好是个逻辑卷,我这里就不再演示逻辑卷的创建了,我的博文中有,此处没有使用逻辑卷

准备数据目录并添加mysql用户
[iyunv@Master ~]# mkdir -pv /mydata/data
mkdir: created directory `/mydata'
mkdir: created directory `/mydata/data'
[iyunv@Master ~]# groupadd -g 3306 mysql
[iyunv@Master ~]# useradd -u 3306 -g mysql -M -s /sbin/nologin -d /mydata/data/ mysql
[iyunv@Master ~]# chown -R mysql.mysql /mydata/data/

解压并初始化mysql
[iyunv@Master ~]# cd /tmp/
[iyunv@Master tmp]# tar xf mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
[iyunv@Master tmp]# cd /usr/local/
[iyunv@master local]# ln -sv mysql-5.6.26-linux-glibc2.5-x86_64  mysql
create symbolic link `mysql' to `mysql-5.6.26-linux-glibc2.5-x86_64'
[iyunv@Master local]# cd mysql
[iyunv@Master mysql]# chown -R root.mysql ./*
[iyunv@master mysql]# ll
total 232
drwxr-xr-x  2 root mysql   4096 Jan 16 12:48 bin
-rw-r--r--  1 root mysql  17987 Jul 15  2015 COPYING
drwxr-xr-x  3 root mysql   4096 Jan 16 12:49 data
drwxr-xr-x  2 root mysql   4096 Jan 16 12:49 docs
drwxr-xr-x  3 root mysql   4096 Jan 16 12:48 include
-rw-r--r--  1 root mysql 104897 Jul 15  2015 INSTALL-BINARY
drwxr-xr-x  3 root mysql   4096 Jan 16 12:49 lib
drwxr-xr-x  4 root mysql   4096 Jan 16 12:49 man
drwxr-xr-x 10 root mysql   4096 Jan 16 12:49 mysql-test
-rw-r--r--  1 root mysql   2496 Jul 15  2015 README
drwxr-xr-x  2 root mysql   4096 Jan 16 12:49 scripts
drwxr-xr-x 28 root mysql   4096 Jan 16 12:49 share
drwxr-xr-x  4 root mysql   4096 Jan 16 12:49 sql-bench
drwxr-xr-x  2 root mysql   4096 Jan 16 12:49 support-files

[iyunv@master mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
Installing MySQL system tables...2016-01-16 12:57:52 0 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
2016-01-16 12:57:52 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-01-16 12:57:52 0 [Note] ./bin/mysqld (mysqld 5.6.26-log) starting as process 19105 ...
2016-01-16 12:57:53 19105 [Note] InnoDB: Using atomics to ref count buffer pool pages
2016-01-16 12:57:53 19105 [Note] InnoDB: The InnoDB memory heap is disabled
2016-01-16 12:57:53 19105 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-01-16 12:57:53 19105 [Note] InnoDB: Memory barrier is not used
2016-01-16 12:57:53 19105 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-01-16 12:57:53 19105 [Note] InnoDB: Using Linux native AIO
2016-01-16 12:57:53 19105 [Note] InnoDB: Not using CPU crc32 instructions
2016-01-16 12:57:53 19105 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-01-16 12:57:53 19105 [Note] InnoDB: Completed initialization of buffer pool
2016-01-16 12:57:53 19105 [Note] InnoDB: Highest supported file format is Barracuda.
2016-01-16 12:57:53 19105 [Note] InnoDB: 128 rollback segment(s) are active.
2016-01-16 12:57:53 19105 [Note] InnoDB: 5.6.26 started; log sequence number 1600627
2016-01-16 12:57:54 19105 [Note] Binlog end
2016-01-16 12:57:54 19105 [Note] InnoDB: FTS optimize thread exiting.
2016-01-16 12:57:54 19105 [Note] InnoDB: Starting shutdown...
2016-01-16 12:57:56 19105 [Note] InnoDB: Shutdown completed; log sequence number 1626007
OK

Filling help tables...2016-01-16 12:57:56 0 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
2016-01-16 12:57:56 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-01-16 12:57:56 0 [Note] ./bin/mysqld (mysqld 5.6.26-log) starting as process 19128 ...
2016-01-16 12:57:56 19128 [Note] InnoDB: Using atomics to ref count buffer pool pages
2016-01-16 12:57:56 19128 [Note] InnoDB: The InnoDB memory heap is disabled
2016-01-16 12:57:56 19128 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-01-16 12:57:56 19128 [Note] InnoDB: Memory barrier is not used
2016-01-16 12:57:56 19128 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-01-16 12:57:56 19128 [Note] InnoDB: Using Linux native AIO
2016-01-16 12:57:56 19128 [Note] InnoDB: Not using CPU crc32 instructions
2016-01-16 12:57:56 19128 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-01-16 12:57:56 19128 [Note] InnoDB: Completed initialization of buffer pool
2016-01-16 12:57:56 19128 [Note] InnoDB: Highest supported file format is Barracuda.
2016-01-16 12:57:56 19128 [Note] InnoDB: 128 rollback segment(s) are active.
2016-01-16 12:57:56 19128 [Note] InnoDB: Waiting for purge to start
2016-01-16 12:57:56 19128 [Note] InnoDB: 5.6.26 started; log sequence number 1626007
2016-01-16 12:57:56 19128 [Note] Binlog end
2016-01-16 12:57:57 19128 [Note] InnoDB: FTS optimize thread exiting.
2016-01-16 12:57:57 19128 [Note] InnoDB: Starting shutdown...
2016-01-16 12:57:58 19128 [Note] InnoDB: Shutdown completed; log sequence number 1626017
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

  ./bin/mysqladmin -u root password 'new-password'
  ./bin/mysqladmin -u root -h master.network.com password 'new-password'

Alternatively you can run:

  ./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

  cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

  cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

  http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

WARNING: Found existing config file ./my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as ./my-new.cnf,
please compare it with your file and take the changes you need.

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

复制服务启动脚本
[iyunv@Master mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[iyunv@Master mysql]# chkconfig --add mysqld

增加PATH环境变量并使之生效
[iyunv@master mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin/mysql' > /etc/profile.d/mysql.sh
[iyunv@master mysql]# . /etc/profile.d/mysql.sh

将mysql5.6.26的安装包复制到slave节点上
[iyunv@master mysql]# scp  /tmp/mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz slave.network.com:/tmp/
The authenticity of host 'slave.network.com (172.16.1.105)' can't be established.
RSA key fingerprint is 13:42:92:7b:ff:61:d8:f3:7c:97:5f:22:f6:71:b3:24.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'slave.network.com' (RSA) to the list of known hosts.
mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz                100%  298MB   4.7MB/s   01:03



5、Master安装并配置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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
准备数据目录并添加mysql用户
[iyunv@slave ~]# mkdir -pv /mydata/data
mkdir: created directory `/mydata'
mkdir: created directory `/mydata/data'
[iyunv@slave ~]# groupadd -g 3306 mysql
[iyunv@slave ~]# useradd -u 3306 -g mysql -M -s /sbin/nologin -d /mydata/data/ mysql
[iyunv@slave ~]# chown -R mysql.mysql /mydata/data/

解压并初始化mysql
[iyunv@slave ~]# cd /tmp/
[iyunv@slave tmp]# tar xf mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
[iyunv@slave tmp]# cd /usr/local/
[iyunv@slave local]# ln -sv mysql-5.6.26-linux-glibc2.5-x86_64  mysql
create symbolic link `mysql' to `mysql-5.6.26-linux-glibc2.5-x86_64'
[iyunv@slave local]# cd mysql
[iyunv@slave mysql]# chown -R root.mysql ./*

[iyunv@slave mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
Installing MySQL system tables...2016-01-16 13:55:00 0 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
2016-01-16 13:55:00 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-01-16 13:55:00 0 [Note] ./bin/mysqld (mysqld 5.6.26) starting as process 18199 ...
2016-01-16 13:55:00 18199 [Warning] You need to use --log-bin to make --binlog-format work.
2016-01-16 13:55:00 18199 [Note] InnoDB: Using atomics to ref count buffer pool pages
2016-01-16 13:55:00 18199 [Note] InnoDB: The InnoDB memory heap is disabled
2016-01-16 13:55:00 18199 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-01-16 13:55:00 18199 [Note] InnoDB: Memory barrier is not used
2016-01-16 13:55:00 18199 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-01-16 13:55:00 18199 [Note] InnoDB: Using Linux native AIO
2016-01-16 13:55:00 18199 [Note] InnoDB: Not using CPU crc32 instructions
2016-01-16 13:55:00 18199 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-01-16 13:55:00 18199 [Note] InnoDB: Completed initialization of buffer pool
2016-01-16 13:55:00 18199 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2016-01-16 13:55:00 18199 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2016-01-16 13:55:00 18199 [Note] InnoDB: Database physically writes the file full: wait...
2016-01-16 13:55:01 18199 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2016-01-16 13:55:03 18199 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2016-01-16 13:55:06 18199 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2016-01-16 13:55:06 18199 [Warning] InnoDB: New log files created, LSN=45781
2016-01-16 13:55:06 18199 [Note] InnoDB: Doublewrite buffer not found: creating new
2016-01-16 13:55:06 18199 [Note] InnoDB: Doublewrite buffer created
2016-01-16 13:55:06 18199 [Note] InnoDB: 128 rollback segment(s) are active.
2016-01-16 13:55:06 18199 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-01-16 13:55:06 18199 [Note] InnoDB: Foreign key constraint system tables created
2016-01-16 13:55:06 18199 [Note] InnoDB: Creating tablespace and datafile system tables.
2016-01-16 13:55:07 18199 [Note] InnoDB: Tablespace and datafile system tables created.
2016-01-16 13:55:07 18199 [Note] InnoDB: Waiting for purge to start
2016-01-16 13:55:07 18199 [Note] InnoDB: 5.6.26 started; log sequence number 0
2016-01-16 13:55:09 18199 [Note] Binlog end
2016-01-16 13:55:09 18199 [Note] InnoDB: FTS optimize thread exiting.
2016-01-16 13:55:09 18199 [Note] InnoDB: Starting shutdown...
2016-01-16 13:55:11 18199 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK

Filling help tables...2016-01-16 13:55:11 0 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
2016-01-16 13:55:11 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-01-16 13:55:11 0 [Note] ./bin/mysqld (mysqld 5.6.26) starting as process 18224 ...
2016-01-16 13:55:11 18224 [Warning] You need to use --log-bin to make --binlog-format work.
2016-01-16 13:55:11 18224 [Note] InnoDB: Using atomics to ref count buffer pool pages
2016-01-16 13:55:11 18224 [Note] InnoDB: The InnoDB memory heap is disabled
2016-01-16 13:55:11 18224 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-01-16 13:55:11 18224 [Note] InnoDB: Memory barrier is not used
2016-01-16 13:55:11 18224 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-01-16 13:55:11 18224 [Note] InnoDB: Using Linux native AIO
2016-01-16 13:55:11 18224 [Note] InnoDB: Not using CPU crc32 instructions
2016-01-16 13:55:11 18224 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-01-16 13:55:11 18224 [Note] InnoDB: Completed initialization of buffer pool
2016-01-16 13:55:11 18224 [Note] InnoDB: Highest supported file format is Barracuda.
2016-01-16 13:55:11 18224 [Note] InnoDB: 128 rollback segment(s) are active.
2016-01-16 13:55:11 18224 [Note] InnoDB: 5.6.26 started; log sequence number 1625977
2016-01-16 13:55:12 18224 [Note] Binlog end
2016-01-16 13:55:12 18224 [Note] InnoDB: FTS optimize thread exiting.
2016-01-16 13:55:12 18224 [Note] InnoDB: Starting shutdown...
2016-01-16 13:55:13 18224 [Note] InnoDB: Shutdown completed; log sequence number 1625987
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

  ./bin/mysqladmin -u root password 'new-password'
  ./bin/mysqladmin -u root -h slave.network.com password 'new-password'

Alternatively you can run:

  ./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

  cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

  cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

  http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

New default config file was created as ./my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

复制服务启动脚本
[iyunv@slave mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[iyunv@slave mysql]# chkconfig --add mysqld



三、基于GTID的主从模式配置过程
1、编辑master的配置文件
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
编辑配置文件,就在当前目录下(/usr/local/mysql)
[iyunv@master mysql]# vim my.cnf
[mysqld]
binlog-format=ROW
log-bin=master-bin
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
server-id=1
report-port=3306
port=3306
datadir=/mydata/data
innodb_file_per_table = ON
socket=/tmp/mysql.sock
report-host=172.16.1.101

补充
binlog-format:二进制日志的格式,有row、statement和mixed几种类型;
需要注意的是:当设置隔离级别为READ-COMMITED必须设置二进制日志格式为ROW
现在MySQL官方认为STATEMENT这个已经不再适合继续使用;
但mixed类型在默认的事务隔离级别下,可能会导致主从数据不一致;
log-slave-updates、gtid-mode、enforce-gtid-consistency、report-port和report-host:用于启动GTID及满足附属的其它需求;
master-info-repository和relay-log-info-repository:启用此两项,可用于实现在崩溃时保证二进制及从服务器安全的功能;
sync-master-info:启用之可确保无信息丢失;
slave-paralles-workers:设定从服务器的SQL线程数;0表示关闭多线程复制功能;
binlog-checksum、master-verify-checksum和slave-sql-verify-checksum:启用复制有关的所有校验功能;
binlog-rows-query-log-events:启用之可用于在二进制日志记录事件相关的信息,可降低故障排除的复杂度;
log-bin:启用二进制日志,这是保证复制功能的基本前提;
server-id:同一个复制拓扑中的所有服务器的id号必须惟一;

启动服务并查看gtid是否生效
[iyunv@master mysql]# service mysqld start
Starting MySQL.........                                    [  OK  ]

[iyunv@master mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.26-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SHOW GLOBAL VARIABLES LIKE '%gtid%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| binlog_gtid_simple_recovery     | OFF   |
| enforce_gtid_consistency        | ON    |
| gtid_executed                   |       |
| gtid_mode                       | ON    |
| gtid_owned                      |       |
| gtid_purged                     |       |
| simplified_binlog_gtid_recovery | OFF   |
+---------------------------------+-------+
7 rows in set (0.00 sec)

查看master的uuid
mysql> SHOW GLOBAL VARIABLES LIKE '%uuid%';
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | dfc512d8-bc12-11e5-97ec-000c29fe8238 |
+---------------+--------------------------------------+
1 row in set (0.00 sec)



2、编辑slave的配置文件
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
编辑配置文件,就在当前目录下(/usr/local/mysql)
[iyunv@slave mysql]# vim my.cnf
[mysqld]
binlog-format=ROW
log-slave-updates=true
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
server-id=11
report-port=3306
port=3306
log-bin=master-bin.log
relay-log=relay-log
datadir=/mydata/data
socket=/tmp/mysql.sock
report-host=172.16.1.105

启动服务并查看gtid是否生效
[iyunv@slave mysql]# service mysqld start
Starting MySQL.....                                        [  OK  ]
[iyunv@slave mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.26-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SHOW GLOBAL VARIABLES LIKE '%gtid%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| binlog_gtid_simple_recovery     | OFF   |
| enforce_gtid_consistency        | ON    |
| gtid_executed                   |       |
| gtid_mode                       | ON    |
| gtid_owned                      |       |
| gtid_purged                     |       |
| simplified_binlog_gtid_recovery | OFF   |
+---------------------------------+-------+
7 rows in set (0.00 sec)

mysql> SHOW GLOBAL VARIABLES LIKE '%uuid%';
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | ec8230c2-bc18-11e5-9813-000c296634d1 |
+---------------+--------------------------------------+
1 row in set (0.09 sec)



3、master建立复制账号
1
2
3
4
5
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repuser'@'172.16.%.%' IDENTIFIED BY 'reppass';
Query OK, 0 rows affected (0.09 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)



4、启动从节点的复制线程
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
mysql> CHANGE MASTER TO MASTER_HOST='master.network.com', MASTER_USER='repuser', MASTER_PASSWORD='reppass', MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 2 warnings (0.30 sec)

显示警告信息
mysql> SHOW WARNINGS\G
*************************** 1. row ***************************
  Level: Note
   Code: 1759
Message: Sending passwords in plain text without SSL/TLS is extremely insecure.
*************************** 2. row ***************************
  Level: Note
   Code: 1760
Message: Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
2 rows in set (0.00 sec)

查看slave状态信息
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: master.network.com
                  Master_User: repuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File:
          Read_Master_Log_Pos: 4
               Relay_Log_File: relay-log.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File:
             Slave_IO_Running: No
            Slave_SQL_Running: No
              Replicate_Do_DB: mydb
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 0
              Relay_Log_Space: 151
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 0
                  Master_UUID:
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State:
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 1
1 row in set (0.00 sec)

启动slave线程
mysql> START SLAVE;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: master.network.com
                  Master_User: repuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 536
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 748
        Relay_Master_Log_File: master-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: mydb
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 536
              Relay_Log_Space: 946
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: dfc512d8-bc12-11e5-97ec-000c29fe8238
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set: dfc512d8-bc12-11e5-97ec-000c29fe8238:1-2
            Executed_Gtid_Set: dfc512d8-bc12-11e5-97ec-000c29fe8238:1-2
                Auto_Position: 1
1 row in set (0.01 sec)



5、测试复制功能
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
在master上创建一个测试库
mysql> CREATE DATABASE mydb;
Query OK, 1 row affected (0.08 sec)

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.01 sec)

然后在slave上查看是否有此库
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |                 # 可以看到复制功能已然实现
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

此时master上可以查看到slave的相关信息
mysql> SHOW SLAVE HOSTS;  
+-----------+--------------+------+-----------+--------------------------------------+
| Server_id | Host         | Port | Master_id | Slave_UUID                           |
+-----------+--------------+------+-----------+--------------------------------------+
|        11 | 172.16.1.105 | 3306 |         1 | ec8230c2-bc18-11e5-9813-000c296634d1 |
+-----------+--------------+------+-----------+--------------------------------------+
1 row in set (0.03 sec)











运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-165833-1-1.html 上篇帖子: mysql select 语句用法 下篇帖子: 手动编译安装mysql,报错没有libaio模块 多线程
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表