当前位置:网站首页>MySQL master-slave replication

MySQL master-slave replication

2022-06-24 12:52:00 User 1168904

1. The system versions are rhel5 mysql Same version

2. The primary server is running , Can't stop .

3. master server IP by :192.168.1.100

From the server IP by :192.168.1.101

4. From the server MSYQL slave It is in the stop state

Configuration steps :

1、 Main library creation /etc/my.cnf, modify [mysqld] The key value inside is increased

server-id=1

log-bin=binlog_name

2、 Add user to main library , Used to read the main library log from the library .

grant replication slave,reload,super on *.* to'slave'@'192.168.1.101'identified by '123456';

flush privileges;

3、 Connect from the library to the main library for testing .

mysql -u slave -p -h 192.168.1.100 //master Of IP

4、 Stop from library , Modify slave Library /etc/my.cnf, Add options :

[mysqld]

server-id=2

master-host=192.168.1.100

master-user=slave

master-password=123456

5. Lock table

mysql > FLUSH TABLES WITH READ LOCK;

Be careful : In order to ensure FLUSH TABLES Statement to keep the read lock in effect .( If you exit the client program , The lock is released ). Set up new SSH Connect , Then, the data on the primary server is processed quickly

mirror .

6. Take a snapshot

tar -cvf data.tar /var/lib/mysql

Copy the compressed package and decompress it

It can also be used. scp Copy the past :

scp -r /var/lib/mysql/* 192.168.1.101:/var/lib/mysql/

Pay attention to the permission settings after copying

chown -R mysql.mysql /var/lib/mysql

A large amount of data can be used mysqldump export .

7. Unlock

mysql > UNLOCK TABLES;

8. Write down the file pos value

When FLUSH TABLES WITH READ LOCK When the set read lock is valid , Read the current binary log name on the primary server (file) And offset values (pos):

mysql > SHOW MASTER STATUS;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.003 | 73 | test | manual,mysql |

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

File The column displays the log name , and Position Display offset . In this example , The binary log value is mysql-bin.003, The offset for the 73. Record the value . You need to set up the slave server in the future

Use these values . They represent duplicate coordinates , The slave server should start from this point ( It can also be any point ) Start a new update from the primary server .

9. Perform synchronization from the server

# mysql>start slave;

# mysql>load data from master;

10. Verify configuration

Log in and input the following command from the server :

mysql> show slave status\G;

You will get a list similar to the following :

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

All for yes, The configuration is correct .

11. To test :

Main database create table ,

mysql>create database sampdb;

mysql>use sampdb;

mysql>create table new (name char(20),phone char(20));

mysql>insert into new (’abc‘,’0532555555’);

The master and slave servers are restarted ;

Open from library , see :

#mysql -u root -p

mysql>show databases; // You should be able to see master Created library sampdb

mysql

sampdb

test

mysql>use sampdb;

mysql>show tables;

new

This indicates that the master-slave database is successfully created .

attach :

Some error information processing , Commands on master and slave servers , And status information .

Use... On the slave server show slave status\G

Slave_IO_Running, by No,

shows IO_THREAD Has not started , Please perform start slave io_thread

Slave_SQL_Running by No

Copy error , see Last_error After the field eliminates the error, execute start slave sql_thread

see Slave_IO_State Field empty // Replication did not start

Connecting to master// Not connected maste

Waiting for master to send event// It's connected to

Related commands on the master server :

show master status

show slave hosts

show logs

show binlog events

purge logs to 'log_name'

purge logs before 'date'

reset master( The old version flush master)

set sql_log_bin=

From the relevant commands on the server :

slave start

slave stop

SLAVE STOP IO_THREAD // This thread puts master The log of segment is written to local

SLAVE start IO_THREAD

SLAVE STOP SQL_THREAD // This thread applies logs written locally to the database

SLAVE start SQL_THREAD

reset slave

SET GLOBAL SQL_SLAVE_SKIP_COUNTER

load data from maste

show slave status(SUPER,REPLICATION CLIENT)

CHANGE MASTER TO MASTER_HOST=, MASTER_PORT=,MASTER_USER=, MASTER_PASSWORD= // The dynamic change master Information

PURGE MASTER [before 'date'] Delete master The logs that have been synchronized at the end

Several related to hot standby mysql command :( Need to be in mysql Command line interface or query )

* stop slave # Stop syncing

* start slave # Start syncing , Update from the end of the log .

* SET SQL_LOG_BIN=0|1 # The host side runs , need super jurisdiction , Used to start and stop the log , Random start and stop , This will cause inconsistency between host and slave data , Cause mistakes

* SET GLOBAL SQL_SLAVE_SKIP_COUNTER=n # Client running , Used to skip several events , Only when the synchronization process stops due to an error can it be executed .

* RESET MASTER # The host side runs , Clear all logs , This command is the original FLUSH MASTER

* RESET SLAVE # Slave operation , Clear the log synchronization location flag , And regenerate master.info

Although regenerated master.info, But it doesn't work , best , Will be driven from mysql Restart the process ,

* LOAD TABLE tblname FROM MASTER # Slave operation , Reread the data of the specified table from the host , Only one... Can be read at a time , suffer timeout The time limit , Need to adjust timeout Time . To execute this command, you need to synchronize the accounts reload and super jurisdiction . And the corresponding library has select jurisdiction . If the watch is bigger , To increase net_read_timeout and net_write_timeout Value

* LOAD DATA FROM MASTER # Slave execution , Re read all data from the host . To execute this command, you need to synchronize the accounts reload and super jurisdiction . And the corresponding library has select jurisdiction . If the watch is bigger , To increase net_read_timeout and net_write_timeout Value

* CHANGE MASTER TO master_def_list # Change some host settings online , Multiple are separated by commas , such as

CHANGE MASTER TO

MASTER_HOST='master2.mycompany.com',

MASTER_USER='replication',

MASTER_PASSWORD='bigs3cret'

* MASTER_POS_WAIT() # Slave operation

* SHOW MASTER STATUS # Host running , See the log export information

* SHOW SLAVE HOSTS # Host running , Look at the connected slave .

* SHOW SLAVE STATUS (slave)

* SHOW MASTER LOGS (master)

* SHOW BINLOG EVENTS [ IN 'logname' ] [ FROM pos ] [ LIMIT [offset,] rows ]

* PURGE [MASTER] LOGS TO 'logname' ; PURGE [MASTER] LOGS BEFORE 'date'

原网站

版权声明
本文为[User 1168904]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/05/20210526103537741B.html