MySQL5.7.16 gtid复制

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL5.7.16 gtid复制相关的知识,希望对你有一定的参考价值。

《基础环境准备:》

首先安装两台mysql5.7.16数据库,安装如下步骤即可:


一、系统环境准备:

①:系统yum源配置:

[linux]

name=linux hae

baseurl=file:///media/

gpgcheck=1

gpgkey=file:///media/RPM-GPG-KEY-redhat-release


②:挂载Linux7.1系统盘安装必要的软件

yum -y install gcc* gcc-c++ ncurses ncurses-devel cmake bison libgcrypt perl


③:卸载系统自带的MySQL软件:


[[email protected] ~]# rpm -qa | grep maria*

[[email protected] ~]# rpm -qa | grep mysql

[[email protected] ~]# rpm -e --nodeps mariadb-server

[[email protected] ~]# rpm -e --nodeps mariadb


④:修改系统参数

[[email protected] ~]# vim /etc/security/limits.conf

*    soft    nproc     65535

*    hard    nproc     65535

*    soft    nofile    65535

*    hard    nofile    65535


[[email protected] ~]# vi /etc/profile

ulimit -u 65535

ulimit -n 65535


[[email protected] ~]# vim /etc/pam.d/login 

session    required     pam_limits.so


[[email protected] ~]# source /etc/profile


⑤:创建安装MySQL的必要目录

[[email protected] ~]# useradd mysql

[[email protected] ~]# passwd mysql

[[email protected] ~]# mkdir -p /mysql/mysql

[[email protected] ~]# mkdir -p /mysql/data

[[email protected] ~]# chown -R mysql.mysql /mysql

[[email protected] ~]# chmod -R 755 /mysql


二、开始安装MySQL5.7.16

①:解压MySQL软件

[[email protected] mysql]$ tar -zxvf boost_1_59_0.tar.gz 

[[email protected] mysql]$ tar -zxvf mysql-5.7.16.tar.gz 


②:编译MySQL5.7.16

[[email protected] mysql-5.7.16]$ cmake . -DCMAKE_INSTALL_PREFIX=/mysql/mysql \

 -DMYSQL_DATADIR=/mysql/data \

 -DDOWNLOAD_BOOST=1 \

 -DWITH_BOOST=/mysql/boost_1_59_0 


[[email protected] mysql-5.7.16]$ make -j 4

[[email protected] mysql-5.7.16]$ make install


③:初始化数据

/mysql/mysql/bin/mysqld --initialize --user=mysql --basedir=/mysql/mysql/ --datadir=/mysql/data/

(初始化数据库后,会生成root用户的默认密码,要记住)


④:编辑my.cnf配置文件,启动数据库

[[email protected] data]$ vim my.cnf 

[client]

port = 3306

socket = /mysql/mysql/mysql.sock

default-character-set = utf8mb4

 

[mysqld]

port = 3306

socket = /mysql/mysql/mysql.sock

 

basedir = /mysql/mysql

datadir = /mysql/data

pid-file =/mysql/data/mysql.pid

user = mysql

bind-address = 0.0.0.0

server-id = 1       ------从库的编号可以设置 2   不能和主库一致;

 

init-connect = ‘SET NAMES utf8mb4‘

character-set-server = utf8mb4

 

back_log = 300

 

max_connections = 1000

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 = 8M

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

expire_logs_days = 30

 

log_error = /mysql/data/mysql-error.log

slow_query_log = 1

long_query_time = 1

slow_query_log_file = /mysql/data/mysql-slow.log

 

performance_schema = 0

explicit_defaults_for_timestamp

 

skip-external-locking

 

default_storage_engine = InnoDB

innodb_file_per_table = 1

innodb_open_files = 500

innodb_buffer_pool_size = 64M

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

 

interactive_timeout = 28800

wait_timeout = 28800

 

[mysqldump]

quick

max_allowed_packet = 16M

 

[myisamchk]

key_buffer_size = 8M

sort_buffer_size = 8M

read_buffer = 4M

write_buffer = 4M


启动MySQL数据库:

/mysql/mysql/bin/mysqld_safe --defaults-file=/mysql/data/my.cnf   &


⑤:启动数据库修改默认密码

[[email protected] ~]$ mysql -u root -p   (使用刚才的密码)


注意:如果提示密码过期不让登陆,可以在my.cnf配置文件加入:

[mysqld]

skip-grant-tables

然后重启MySQL,不用输入密码就可以登录数据库了!

最后修改默认密码:

mysql> update mysql.user set authentication_string=password(‘123456‘) where user=‘root‘ ; 

mysql> update mysql.user set password_expired=‘N‘;

mysql> flush privileges;

mysql> alter user ‘root‘@‘localhost‘ identified by ‘123456‘;

mysql> flush privileges;


⑥:创建同步用用户(在主库)

mysql> grant replication slave,replication client on *.* to [email protected]‘192.168.2.200‘ identified by ‘feige‘;


###########################################################################

                      gtid 复制操作

一、主库操作

1、编辑主库的my.cnf配置文件,添加如下:

gtid-mode = on

enforce-gtid-consistency = 1 

binlog_format = row 

skip_slave_start=1


2、重新启动MySQL主库;

3、在主库创建测试用schema和表

mysql> create database zhang;

mysql> use zhang

mysql> show tables;

mysql> create table emp (id int);

mysql> insert into emp values (1);


mysql> show master status;

+------------------+----------+--------------+------------------+------------------------------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |

+------------------+----------+--------------+------------------+------------------------------------------+

| mysql-bin.000008 |      738 |              |                  | ac3842d2-ba9a-11e6-9e1b-000c298cd705:1-3 |

+------------------+----------+--------------+------------------+------------------------------------------+

注意:我们在主库开启了gtid复制,而且创建了测试用schema和表 并且查看状态可以得知目前的gtid号:1-3


二、从库操作:

1、编辑从库的my.cnf配置文件,添加如下:

gtid-mode = on

enforce-gtid-consistency = 1 

binlog_format = row 

skip_slave_start=1


2、重新启动MySQL主库;

3、配置主从同步:

mysql> CHANGE MASTER TO  MASTER_HOST=‘192.168.2.100‘, MASTER_USER=‘repl‘,  MASTER_PASSWORD=‘feige‘,   MASTER_PORT=3306,   MASTER_AUTO_POSITION = 1;


4、开启从库主从同步:

mysql> start slave;


5、开始验证主从同步状态:

注意:开启主从后,主库刚才创建的测试库和表都会复制过来,因为在主库开启gtid的时候 主库的每一步操作都会有个gtid号,等着从库开启同步后,会把所有记录的操作都会同步到从库;


注意:gtid复制 有很多限制:


本文出自 “笨小孩的dba之路” 博客,请务必保留此出处http://fengfeng688.blog.51cto.com/4896812/1928541

以上是关于MySQL5.7.16 gtid复制的主要内容,如果未能解决你的问题,请参考以下文章

GTID主从复制

MySQL 8 复制——GTID与复制

深入MySQL复制:基于GTID复制

MySQLMySQL5.7传统复制切换为GTID复制

MySQL5.7 传统复制到GTID复制

MySQL基于GTID的主从复制