The above configuration Centos7.6 Mysql from master database (master / slave), separate read and write

In the previous article, explains how to use nginx under high concurrency scenarios high load do background service load balancing: Configure nginx + uwsgi + load balancing configuration on Ali cloud Centos, but do not think that doing so is to once and for all , to the data service layer, data access layer, or if traditional data structures, or just simply rely on a single server load, so many database connections, the database is bound to collapse, if the database is down, the consequences even more disastrous. At this time, we will consider how to reduce connection to the database, while using good code framework, to optimize the code, using excellent technologies such as data cache: redis, if funds are huge, it is bound to think of erecting a cluster mysql service, to share pressure of the main database. Today summarize use MySQL master-slave configuration, read and write separation, reducing database stress.

    A clear purpose, mysql cluster deployment, using tactics from the main one, the write operation using the main libraries, the main library to synchronize data in real time from the library, the business is responsible for reading from the library, thus completing the purpose of separate read and write.

    mysql master principle is simple from the synchronization generates two threads from the library, an I / O thread, a thread SQL; i / o requests binlog thread to the main library (binary log), and the resulting log writes relay binlog log (relay log) file; primary library will generate a log dump thread, from a library used to binlog i / o threads pass;

    SQL thread, reads the relay log log file, and parsed into specific operation to achieve consistent master-slave operation, and consistent final data.

First, prepare two Ali cloud servers, one as the host (master), as a slave (slave), are installed mysql5.7

    Into the master server

    Mysql modify the configuration file vim /etc/my.cnf, add the following configuration

server-id=1
innodb_flush_log_at_trx_commit=2
sync_binlog=1
log-bin=mysql-bin-1

Configuration instructions:

# Set the main service ID (id can easily set up their own but to ensure that the id and the slave is not the same)
Server-id = 1

# Set 1 is the safest course, but the performance is the worst (relative to the other two parameters words, but not unacceptable). If you do not ask for data consistency and integrity, can be set to 2, if only the most performance requirements, such as high concurrent write log server, set to 0 to obtain higher performance
innodb_flush_log_at_trx_commit = 2

# open binlog-with the step function
sync_binlog 1 = 

#binlog log file name
log-MySQL-bin-bin = 200 

# this indicates that only sync a library (if not this indicate that synchronization of all libraries)
binlog-do-db = xxxx 

Once saved, the restart mysql

systemctl restart mysqld

Enter the mysql command line mysql -uroot -p your password

Enter the authorization command

GRANT REPLICATION SLAVE ON *.* to 'repl'@'%' identified by 'Admin123!'; 

Meaning that all slave can! Synchronize master by repl account and password data Admin123

Then check the status of the master:

show master status;

At this point Master configuration is already done, log in at slave (slave)

Similarly modify slave mysql server configuration vim /etc/my.cnf add the following configuration should be noted that server-id and not as master

server-id=201 
innodb_flush_log_at_trx_commit=2 
sync_binlog=1 
log-bin=mysql-bin-201

After saving restart the service systemctl restart mysqld

Enter the mysql command line mysql -uroot -p your password

input the command:

change master to master_host='39.106.228.179',master_user='repl' ,master_password='Admin123!', master_log_file='mysql-bin.000002' ,master_log_pos=154

Command Description:

master_host: host ip

. MASTER_USER: Host authorized user

master_password: fill in the password authorization when the host

master_log_file: Host show master status; the File

MASTER_LOG_POS: Host show master status; in Position.

Enter the command to start the slave

start slave;

Then we can test for master writing to see if the salve can synchronize data

 

 Of course, separate read and write main mysql not a panacea, selecting different policies from the configuration according to different application scenarios, MySQL replication master from a certain latency, if the real-time requirements for data consistency higher scenes not recommended.

 

Guess you like

Origin www.cnblogs.com/xcsg/p/10982958.html