MySQL Cluster Solution (a): master-slave replication

ready

I was CentOSusing the dockerdeployment, it is necessary to prepare in advance and dockerrelated operations.

docker

CentOS installation

Configuration

Mapping

A host for multiple virtual machines, so you need to use a different configuration file named folder, such as /etc/mysql/my.cnf.d/master01do different mysql-dockerinstances of the configuration startup items distinction in addition. Data document configuration distinguished:/var/lib/mysql/master01

mysql-dockerGeneral default configuration data items and corresponding starting /var/lib/mysqland /etc/my.cnf.d, correspondence relationship is as follows:

/etc/mysql/my.cnf.d/master01:/etc/my.cnf.d
/var/lib/mysql/master01:/var/lib/mysql

It may involve creating folders

mkdir -p /etc/mysql/my.cnf.d/master01 /var/lib/mysql/master01
# 权限设置要严格,否则有些配置文件,比如my.cnf会失效
chmod 645 -R /etc/mysql/my.cnf.d/master01
chmod 777 -R /var/lib/mysql/master01

port

By default 3306, I mapped6306

Mirroring

According to the case, here to build PXC(Percona XtraDB Cluster)a cluster, then choosepercona:5.7.23

Competence

This refers to rootthe initial password for the user on how to define and set up the problem, you can have three options, one of three:

  1. No password: MYSQL_ALLOW_EMPTY_PASSWORD=true

  2. Specify a password: MYSQL_ROOT_PASSWORD=XXX

  3. Random password: MYSQL_RANDOM_ROOT_PASSWORD=true.

    Not recommended! Because it means you need to reset their own password and need to advance in my.cnfthe [mysqld]next configuration skip-grant-tables, define your own password after also need to restart, too much trouble.

start up

The master node

Create a container

Preparations roughly sketched out above the container to create a command:

docker create --name percona-master01 -v /var/lib/mysql/master01:/var/lib/mysql -v /etc/mysql/my.cnf.d/master01:/etc/my.cnf.d -p 6306:3306 -e MYSQL_ROOT_PASSWORD=master01 percona:5.7.23

my.cnf

Clusters do, first of all you need to configure at least the following items:

  1. Openlog-bin
  2. Set upserver-id

Therefore, in the corresponding directory /etc/mysql/my.cnf.d/master01is created under my.cnfand writes:

[mysqld]
log-bin=mysql-bin
server-id=1
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

Here sql_modeit is possible to solve the 1055 error , and has nothing to do with whether a cluster configuration.

Provide some initialization convenient script:

cat << EOF > /etc/mysql/my.cnf.d/master01/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=1
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF

Boot image

Run master01and view the log:

docker start percona-master01  && docker logs -f percona-master01

verification

Wait startup is complete, you can view the configuration is in effect on the host, such as log-bin:

ll /var/lib/mysql/master01 | grep mysql-bin

Or directly login authentication, can be connected to the host port can also be connected docker virtual machine, but you need to query its IP.

  1. Host
# 不能省略-h127.0.0.1,否则会认为是localhost而拒绝登录
mysql -h127.0.0.1 -P6306 -uroot -pmaster01 -Dmysql
  1. virtual machine
docker exec -it percona-master01 mysql -uroot -pmaster01 -Dmysql 

Authorize

At this point the server build better, but also connected, also need to do some initialization operations, such as creating user pxcand give permission.

Can the server, the client can connect and operate, whether it is the server or the client, the actual sqloperation of the same nature.

To create a cluster users , and give permission to view the status of the cluster core sql, for example:

use mysql;
create user 'pxc'@'%' identified by 'pxc';
grant replication slave on *.* to 'pxc'@'%';
flush privileges;

After the restart execution:

# 查看集群状态
show master status;
# 查看二进制日志相关的配置项
show global variables like 'binlog%';
# 查看server相关的配置项
show global variables like 'server%';
summary

Regardless of how even, as long as the command can be used to achieve such a shell script to create a user

# -D数据库名 -e需要执行的SQL脚本
mysql -h127.0.0.1 -P6306 -uroot -pmaster01 -Dmysql -e"create user 'pxc'@'%' identified by 'pxc';"

From node

The master node of the configuration is substantially the same, only the differences here are critical configuration.

Permissions to be strict, otherwise some configuration files, such my.cnf fail

bash-4.2$ mysql -uroot -p
mysql: [Warning] World-writable config file '/etc/my.cnf.d/my.cnf' is ignored.
mysql: [Warning] World-writable config file '/etc/mysql/my.cnf' is ignored.
mkdir -p /etc/mysql/my.cnf.d/slave01 /var/lib/mysql/slave01
chmod 644 -R /etc/mysql/my.cnf.d 
chmod g+w,o+w -R /var/lib/mysql

my.cnf

Still the same idea in /etc/mysql/my.cnf.d/slave01creation under my.cnfand writes:

[mysqld]
# 服务ID,不与master重复即可
server-id=2
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
docker create --privileged --name percona-slave01 -v /var/lib/mysql/slave01:/var/lib/mysql -v /etc/mysql/my.cnf.d/slave01:/etc/my.cnf.d -p 6307:3306 -e MYSQL_ROOT_PASSWORD=slave01 percona:5.7.23
docker start percona-slave01  && docker logs -f percona-slave01
mysql -h0.0.0.0 -P6307 -uroot -pslave01 -Dmysql

Check the master node (port 6306) of the binary log ( bin-log) Information:

mysql -h0.0.0.0 -P6306 -uroot -pmaster01 -Dmysql -e'show master status;'

Output is as follows:

+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

The associated master node

CHANGE MASTER TO
master_host='192.168.0.122',
master_user='pxc',
master_password='pxc',
master_port=6306,
master_log_file='mysql-bin.000001',
master_log_pos=154;
start slave;
show slave status;

From the start node and view from a node status

start slave;
show slave status;

master02 and slave02 build command Falls

Here is a complete record of its own operation, do not directly executed.

#!/bin/bash

###### master02 ###### 

mkdir -p /etc/mysql/my.cnf.d/master02 /var/lib/mysql/master02
chmod 645 -R /etc/mysql/my.cnf.d/master02
chmod 777 -R /var/lib/mysql/master02

docker create --name percona-master02 -v /var/lib/mysql/master02:/var/lib/mysql -v /etc/mysql/my.cnf.d/master02:/etc/my.cnf.d -p 7306:3306 -e MYSQL_ROOT_PASSWORD=master02 percona:5.7.23

cat << EOF > /etc/mysql/my.cnf.d/master02/my.cnf
[mysqld]
log-bin=mysql-bin
server-id=10
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF

docker start percona-master02 && docker logs -f percona-master02

docker exec percona-master02 mysql -uroot -pmaster02 -e"show variables like 'log_bin'"
docker exec percona-master02 mysql -uroot -pmaster02 -e"show master status;"
docker exec percona-master02 mysql -uroot -pmaster02 -e"show global variables like 'server%';"

docker exec percona-master02 mysql -uroot -pmaster02 -Dmysql -e"create user 'pxc'@'%' identified by 'pxc';"
docker exec percona-master02 mysql -uroot -pmaster02 -Dmysql -e"grant replication slave on *.* to 'pxc'@'%';"
docker exec percona-master02 mysql -uroot -pmaster02 -Dmysql -e"flush privileges;"


###### slave02 ######
mkdir -p /etc/mysql/my.cnf.d/slave02 /var/lib/mysql/slave02
chmod 645 -R /etc/mysql/my.cnf.d/slave02
chmod 777 -R /var/lib/mysql/slave02

docker create --name percona-slave02 -v /var/lib/mysql/slave02:/var/lib/mysql -v /etc/mysql/my.cnf.d/slave02:/etc/my.cnf.d -p 7307:3306 -e MYSQL_ROOT_PASSWORD=slave02 percona:5.7.23

cat << EOF > /etc/mysql/my.cnf.d/slave02/my.cnf
[mysqld]
server-id=12
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF

docker start percona-slave02

docker exec percona-slave02 mysql -uroot -pslave02 -e"CHANGE MASTER TO master_host='192.168.0.122', master_user='pxc', master_password='pxc', master_port=7306, master_log_file='mysql-bin.000003', master_log_pos=154;start slave;"
docker exec percona-slave02 mysql -uroot -pslave02 -e"start slave;"
docker exec percona-slave02 mysql -uroot -pslave02 -e"show slave status;"

reference

  1. mysql shell script
  2. Percona database
He published 189 original articles · won praise 48 · views 210 000 +

Guess you like

Origin blog.csdn.net/Young4Dream/article/details/104043810