Mysql cluster structures (isolated from the master copy, read and write)

A, MySQL multi-instance

(A), MySQL introduce multi-instance

1. What is multi-instance MySQL

MySQL multi-instance is to open on a single machine multiple different service ports (such as: 3306,3307,3308), run multiple MySQL service process, through different socket listening different service ports to provide their services.

2, MySQL multi-instance features are the following

(1), efficient use of server resources, the server when the single remaining resources, the remaining resources can take advantage of providing more services.

(2) save the server resources

(3), preemption problem with each other, when a service instance or service concurrent high turn slow query will consume more memory, CPU, disk IO resources, causing other instances on the server to provide the quality of service decline;

3, multi-instance deployment of two ways mysql

(1), first is to use multiple configuration files to start different processes to achieve multi-instance, the advantage of this approach is simple logic, simple configuration, the disadvantage is not very easy to manage;

(2), the second is through the official's own mysqld_multi to implement multi-instance use a separate configuration file, this approach is not a custom configuration for each instance of aspects, advantages, management is very convenient, centralized management;

4, install multiple databases, the following issues must be addressed under the same development environment

(1), the installation configuration file path can not be the same

(2), the database directory can not be the same

(3), the script does not start with the same name

(4), the same port can not

(5), can not generate the same socket path

(B), copy the master-slave principle

MySQL master-slave replication is a feature that comes with MySQL itself, does not require additional third-party software can be achieved , it is not a copy function to copy files to achieve, but with the master binlog log file inside the SQL commands implemented from copy, and then I can be understood as a master-side implementation of SQL commands, it will perform again at the same Salve end, so as to achieve master-slave replication.

Host MySql will dmlMySql (create, update, delete, insert, which does not include a query sql) statement written to the binary log file (binLog).

Library is generated from two threads, one I / O thread, a thread SQL ; i / o requests binlog thread to the main library, and writes the obtained log binlog relay log (relay log) file;

Primary library will generate a log dump thread, from a library used to i / o threads pass the binlog ; the SQL thread reads the relay log log file, and parsed into specific operation to achieve consistent master-slave operation, and finally consistent data;

IO is a thread after obtaining binaries from the host, execution binaries by Sql threads: the role from a node in Sql thread. sql threads depend on IOx thread .

Long connected main library from the library will be created a problem may arise if the data is not synchronized to the network generates a delay.

Note: Mysql master-slave replication is Mysql comes with its own function. myCat separate read and write and do Nginx similar.

If you accidentally deleted data can be recovered from the binlog log.

Two, MySQL master-slave replication configurations

The master node server address 192.168.1.105

From the node server address 192.168.1.107

(A), the master server node configuration

1. Go to the configuration page command

   we /etc/my.cnf

2. Configure the server id server_id

3 herein after server_id possible to configure the server address to be used to distinguish

  server_id=234

3. Enable the log file (binLog)

  log-bin=mysql-bin

The address should be in the configuration file: # Recommended in standard MySQL setup, otherwise the configuration does not work

As shown below

 

4. Restart the mysql service

service mysqld restart

Verify that the configuration has been successful:

5.show variables like '%server_id%';

Server_id able to query the corresponding description in the configuration file has been successfully configured, as shown, the configuration is successful. server_id = 234

 

6.show master status;

Able to see the files synchronized, and the number of lines have configured successfully.

 

Third, from the server node

1. Go to the configuration page command

   we /etc/my.cnf

2. Configure the server id server_id

3 herein after server_id possible to configure the server address to be used to distinguish

  server_id=30

3. Enable the log file (binLog)

  log-bin=mysql-bin

4. Add the required synchronization database

binlog_do_db=test  

5. Restart mysql service

service mysqld restart

Verify that the configuration has been successful

6.show variables like '%server_id%';

Server_id instructions correspond to query the configuration file has been configured successfully

7. synchronization server configuration from the primary server

master_host primary server address

master_user primary server user name

master_password master password

master_log_file main server configuration file

master_log_pos main server configuration file reading start position, that is, how much to start reading from the first row.

change master to master_host='192.168.1.107',master_user=“用户名”,master_password=“密码”,master_log_file='mysql-bin.000001',master_log_pos=120;

8. Start Sync

start slave

9. Check status of replication from the server

SHOW SLAVE STATUS

If two cloning master server from the server , the case will this time appears below, two servers are the same server-uuid as clone over. This time there will be run SHOW SLAVE STATUS Slave_IO_Running to No, and Slave_SQL_Running to Yes. Note IO synchronization error, the error message can be seen in Last_IO_Error field, as follows. Meaning the error message is server-uuid repeat. The following figure shows the effect of which is the.

Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work。

If this happens, you need to / var / lib / mysql  generated files under the uuid file deletion, and then restart the mysql service will be re-generated in a server-uuid, Figure 2 below also shows this regenerated the server-uuid.

If your server is not cloned from the master server but re-installed , then it will not happen. Run directly  SHOW SLAVE STATUS command, as shown below. It indicates a successful synchronization

Storing server-uuid address, you can /etc/my.cnf view file

How to build a successful test our mysql master-slave replication cluster?

New test node of the primary database server 234, if the node 30 (from the server) can synchronize over, then the environmental structures success.

If the synchronization fails, modified as follows:

show variables like '%server_id%';

show master status;

STOP SLAVE;

set GLOBAL sql_slave_skip_counter=1;

start slave;

SHOW SLAVE STATUS;

 

note:

1. Remember that operate from the server read-only, does not operate CRUD. To keep data in synch, so that when a server failure, the other can go to the top (in fact, when the primary server goes down will definitely lose part of the latest data ).

2. From the server's bandwidth must> = master server, minimizing synchronization delay. If the small pipe, slow step synchronization natural thing.

Released five original articles · won praise 4 · Views 174

Guess you like

Origin blog.csdn.net/zhuxiang997/article/details/105064509