MariaDB master-slave (relay) slave configuration

Environment description (see the bottom note to unlock the table):
Oracle Linux 7.5
MariaDB 10.2.14
Master DB: 192.168.168.10
Slave-1 DB(relay) DB: 192.168.168.11 172.16.216.11

Slave-2 DB:172.16.216.12

1. Install dependency packages

yum -y install make cmake gcc gcc-c++ autoconf automake zlib* libxml* ncurses-devel libtool-ltdl-devel* bison libevent openssl-devel openssl

2. Install and configure the main mariadb

1) Create mariadb installation directory and data storage directory

mkdir -p /opt/mariadb10/data

2) Create users and user groups and grant data storage directory permissions

groupadd -g 86 mysql
useradd -u 86 mysql -g mysql -d /opt/mariadb10 -s /sbin/nologin -M
chown -R mysql:root /opt/mariadb10

3) Install mariadb

Delete the system's own mariadb information

rpm -qa|grep mariadb-libs
rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps
find -H /etc/ | grep my.c
rm -rf /etc/my.cnf /etc/my.cnf.d/

Only the following two files are left
/etc/pki/tls/certs/make-dummy-cert

/etc/pki/tls/certs/renew-dummy-cert

mariadb download address

tar -zxvf mariadb-10.2.14.tar.gz
cd mariadb-10.2.14
cmake . -DCMAKE_INSTALL_PREFIX=/opt/mariadb10 \
-DMYSQL_DATADIR=/opt/mariadb10/data \
-DSYSCONFDIR=/etc \
-DMYSQL_UNIX_ADDR=/opt/mariadb10/data/mysql.sock \
-DENABLED_LOCAL_INFILE=1 \ #Enable loading local data
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ #Support InnoDB engine
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \ #Support ARCHIVE engine
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ #Support BLACKHOLE engine
-DWITHOUT_TOKUDB=1 \ #Do not install tokudb engine
-DMYSQL_TCP_PORT=3389 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DEXTRA_CHARSETS=all \ #Support all extended character support
-DDEFAULT_CHARSET=utf8 \ #The default character set is utf8
-DDEFAULT_COLLATION=utf8_general_ci \ #default character collation utf8
-DMYSQL_USER=mysql \ #Specify the mariadb startup user
-DWITH_SAFEMALLOC=OFF \
-DWITH_DEBUG=0

make && make install

4) Set environment variables and initialize the database

vi /etc/profile.d/mariadb.sh #Set mariadb environment variables
   MARIADB_HOME=/opt/mariadb10
   export PATH=$MARIADB_HOME/bin:$PATH
source /etc/profile.d/mariadb.sh
Initialize the database
/opt/mariadb10/scripts/mysql_install_db --user=mysql --basedir=/opt/mariadb10 --datadir=/opt/mariadb10/data

5) Copy the configuration file

cp support-files/my-large.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chmod u+x /etc/init.d/mysqld
vi /etc/init.d/mysqld
Will
basedir=
datadir=
change to
basedir=/opt/mariadb10
datadir=/opt/mariadb10/data

chkconfig --add mysqld #Add boot items
chkconfig mysqld on

6) Start the database

/etc/init.d/mysqld start

7) Initialize mariadb

/opt/mariadb10/bin/mysql_secure_installation
  Enter current password for root (enter for none):     #回车
  Set root password? [Y/n] y #Whether to set root password
  New password: mariadb
  Re-enter new password: mariadb
  Remove anonymous users? [Y/n] #Delete anonymous accounts
  Disallow root login remotely? [Y/n] #Whether root account remote login is prohibited, it must be prohibited in the production environment
  Remove test database and access to it? [Y/n] y #Whether to clear the test database
  Reload privilege tables now? [Y/n] y                  #重载

8) Test whether you can enter the database

mysql -uroot -p‘mariadb’ -P3389

9) Create a log storage directory

mkdir /var/log/mariadb/
chown mysql.root /var/log/mariadb/
Query log: query log
Slow query log: slow query log #Query time exceeds the specified limit
error log: error log
Binary log: binary log # This is where the statements to be changed or potentially changed are stored
Relay log: reley log
Transaction log: transaction log

10) my.cnf configuration

#password       = your_password
port            = 3389
socket          = /opt/mariadb10/data/mysql.sock
# Here follows entries for some specific programs
# The MariaDB server

[mysqld]
port            = 3389
socket          = /opt/mariadb10/data/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M

innodb_file_per_table = on
skip_name_resolve = on
expire_logs_days = 15
max_connections = 3000
sync-binlog = 1
log_slave_updates = on

# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 2 #Number of server CPU cores*2

# binary logging is required for replication
log-bin = mysql-bin
log-bin-index = mysql-bin

# binary logging format - mixed recommended
binlog_format = mixed

# but will not function as a master if omitted
server-id = 1

#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50
innodb_flush_log_at_trx_commit = 2
innodb_log_file_size = 256M
innodb_log_buffer_size = 16M
innodb_buffer_pool_size = 4G #Set to account for 70%-80% of the memory, such as the server 16G memory setting value is 4G
innodb_flush_method = O_DIRECT

#output different types of log information
general_log = on
general_log_file = /var/log/mariadb/general.log
log_error = /var/log/mariadb/error.log
slow_query_log = on
slow_query_log_file = /var/log/mariadb/slow_query.log
long_query_time = 1

#GTID master to slave replication
binlog-checksum = CRC32 #Validation code
sync-master-info = 1 # A value of 1 ensures that information is not lost
sync_relay_log_info = 1
master-verify-checksum = 1 #Start master server verification
slave-sql-verify-checksum = 1 #Start the slave server verification

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

[mysqld_safe]
pid-file=/opt/mariadb10/data/mysql.pid

11) Create replication user

mysql -uroot -p'mariadb'

MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.168.%' IDENTIFIED BY 'replpass';
MariaDB [(none)]> FLUSH PRIVILEGES;

MariaDB [(none)]> show slave hosts; #View slave server connection status
MariaDB [(none)]> show global status like "rpl%"; #View client

12) Database Migration

MariaDB [(none)]> FLUSH TABLES WITH READ LOCK; #Add read-only locks to all databases
MariaDB [(none)]> SHOW MASTER STATUS; #View binlog and pos values, record some of the values ​​of File and Position
MariaDB [(none)]> SELECT BINLOG_GTID_POS("mysql-bin.00001", 1000); #Use the binlog point value obtained in the previous step to calculate the GTID value        
#mysqldump -uroot -p test > /opt/test.sql; #Pour the libraries everywhere to the slave library

3. Install and configure relay mariadb

1) my.cnf configuration

#password       = your_password
port            = 3389
socket          = /opt/mariadb10/data/mysql.sock

# Here follows entries for some specific programs

# The MariaDB server
[mysqld]
port            = 3389
socket          = /opt/mariadb10/data/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M

innodb_file_per_table = on
skip_name_resolve = on
expire_logs_days = 30
max_connections = 3000
log_slave_updates = on
read_only = on

# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 2 #Number of server CPU cores*2

# binary logging is required for replication
log-bin = mysql-bin

replication-ignore-db = mysql #Set the replication database to be ignored (multiple databases are separated by commas)

# binary logging format - mixed recommended
binlog_format = mixed

# but will not function as a master if omitted
server-id=2 #The authority here is greater than masterdb

#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50
innodb_flush_log_at_trx_commit = 2
innodb_log_file_size = 256M
innodb_log_buffer_size = 16M
innodb_buffer_pool_size = 4G #Set to account for 70%-80% of the memory, such as the server 16G memory setting value is 4G
innodb_flush_method = O_DIRECT

#output different types of log information
general_log = on
general_log_file = /var/log/mariadb/general.log
log_error = /var/log/mariadb/error.log
slow_query_log = on
slow_query_log_file = /var/log/mariadb/slow_query.log
long_query_time = 1

#GTID master to slave replication
relay-log = relay-bin
slave-parallel-threads = 2 #How many replication threads are started at the same time, at most equal to the number of databases to be replicated
binlog-checksum = CRC32
sync-master-info = 1
sync_relay_log_info = 1
master-verify-checksum = 1
slave-sql-verify-checksum = 1
relay_log_recovery = 1
log-slave-updates = 1
relay-log-index = relay-bin

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

[mysqld_safe]
pid-file=/opt/mariadb10/data/mysql.pid
2) Enable master-slave replication

a. Use GTID method

MariaDB [(none)]> SET GLOBAL gtid_slave_pos = "0-1-51"; #The main DB gets the GTID value   
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.168.10',MASTER_PORT = 3389,MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_USE_GTID=slave_pos;                 #从授权
MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> SHOW SLAVE STATUS\G
MariaDB [(none)]> show variables like '%relay%'; #View all related parameters of relay from server Slave

If both Slave_IO_Running and Slave_SQL_Running are YES, the slave service has been running without any problem, and executed on the master DB:
MariaDB [(none)]> unlock tables #Unlock data tables
MariaDB [(none)]> FLUSH PRIVILEGES;

b.mysql5.6 and later versions

CHANGE MASTER TO MASTER_HOST='192.168.168.10', MASTER_USER='repluser', MASTER_PASSWORD='replpass', MASTER_AUTO_POSITION=1;

c. Ordinary way (requires the main library lock library to record bin-log and pos values)

CHANGE MASTER TO MASTER_HOST='192.168.168.10',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-bin.000003',MASTER_LOG_POS=1174;
Note: If reporting Could not initialize master info structure for '';
execute
reset slave;

FLUSH PRIVILEGES;

3) Create a relay user

MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'relayuser'@'172.16.216.%' IDENTIFIED BY 'relaypass';
MariaDB [(none)]> FLUSH PRIVILEGES;

4) View the GTID value of the relay slave master

MariaDB [(none)]> SHOW MASTER STATUS; #View binlog and pos values, record some of the values ​​of File and Position
MariaDB [(none)]> SELECT BINLOG_GTID_POS("mysql-bin.000053", 358); #Use the binlog point value obtained in the previous step to calculate the GTID value  

4. Install and configure remote from mariadb

1) my.cnf configuration

[client]
#password       = your_password
port            = 3389
socket          = /opt/mariadb10/data/mysql.sock

# Here follows entries for some specific programs

# The MariaDB server
[mysqld]
port            = 3389
socket          = /opt/mariadb10/data/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M

innodb_file_per_table = on
skip_name_resolve = on
expire_logs_days = 30
max_connections = 3000
sync_binlog = 1
log_slave_updates = on

# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 2 #Number of server CPU cores*2

# binary logging is required for replication
log-bin = mysql-bin

replication-ignore-db = mysql #Set the replication database to be ignored (multiple databases are separated by commas)

# binary logging format - mixed recommended
binlog_format = mixed

# but will not function as a master if omitted
server-id = 3 #The permission value configuration here is greater than the relay slave

#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50
innodb_flush_log_at_trx_commit = 2
innodb_log_file_size = 256M
innodb_log_buffer_size = 16M
innodb_buffer_pool_size = 4G #Set to account for 70%-80% of the memory, such as the server 16G memory setting value is 4G
innodb_flush_method = O_DIRECT

#output different types of log information
general_log = on
general_log_file = /var/log/mariadb/general.log
log_error = /var/log/mariadb/error.log
slow_query_log = on
slow_query_log_file = /var/log/mariadb/slow_query.log
long_query_time = 1

#GTID master to slave replication
slave-parallel-threads = 2
binlog-checksum = CRC32
sync-master-info = 1
sync_relay_log_info = 1
master-verify-checksum = 1
slave-sql-verify-checksum = 1
log-slave-updates = 1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

[mysqld_safe]
pid-file=/opt/mariadb10/data/mysql.pid

2) Enable master-slave replication

MariaDB [(none)]> SET GLOBAL gtid_slave_pos = "0-1-80"; #Relay the GTID value of the master obtained by the DB, this step can be omitted
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.216.12',MASTER_PORT = 3389,MASTER_USER='relayuser',MASTER_PASSWORD='relaypass',MASTER_USE_GTID=slave_pos;
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> SHOW SLAVE STATUS\G
Note: Slave synchronization in the non-locking table
1. Establish a master-slave relationship first
2. Execute stop slave in the new slave library;

3. From slave1, use the following command to export the pre-synchronized library from the library

mysqldump -uroot -p'mariadb' --skip-lock-tables --master-data=1 -B {database1 database2} > /opt/{database}.sql

4. Copy to the new slave library slave2 and import

mysql -uroot -p'mariadb' < /opt/{database}.sql
5. Execute start slave on slave2;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325560564&siteId=291194637