Master-Slave database separate read and write

Master-Slave database separate read and write

 

A platform or system with the passage of time and increase the amount of time users, database operations tend to slow down, then we need some effective optimizations to speed up execution of the database; such as SQL optimization, table structure optimization, index optimization, engine optimization and optimization of separate read and write means.

1, SQL optimization (simple column points) :

Try to avoid using SELECT *;
only when a query uses limit1 record;
use connection query instead of a subquery;
try to use some keywords can index query.

2, optimization table structure:

Try using a digital type field, improving the efficiency of contrast;
constant length and a high speed data query requirements may be considered to use char, otherwise the VARCHAR;
may be suitable when the vertical split excessive field table, move to another part of the field table;
table proper amount of data can be split horizontally, move to another part of the data table.

3, index optimization:

High-frequency field of inquiry appropriate indexing and improve efficiency. (Indexed on the field for frequently used)

4, Engine Optimization:

Select the appropriate database engine to improve the performance, such as InnoDB and MyISAM, InnoDB and MyISAM are many people in the two most commonly used MySQL table types, advantages and disadvantages of these two types of tables, depending on the specific application. The basic difference is: MyISAM type of advanced processing transactions, which do not support, and support for InnoDB type. MyISAM table type to emphasize that performance, which performs several times faster than InnoDB type, but does not provide transaction support, and InnoDB provides transaction support advanced database features have external keys.
InnoDB:
support transaction processing, support for foreign keys, support for crash repair capacity and concurrency control. If you need integrity requirements for transactions is relatively high (such as banks) required to achieve concurrency control (such as tickets), then select the InnoDB has a great advantage. If you frequently need to update, delete the database, you can also choose InnoDB, because the author (commit) and rollback (rollback) transaction support.
MyISAM:
inserting data fast, space and memory usage is relatively low. If the table is mainly used to insert new records recording and reading, the selection MyISAM efficient processing can be realized. If the application integrity, concurrency requirements are relatively low, it can also be used.
MEMORY:
All data in memory, fast data processing speed, but security is not high. If desired fast access speed, the lower the data security requirements may be selected MEMOEY. It has a table size requirements, you can not build much of a table. Therefore, this type of database used only in a relatively small database tables.
Note:
with a variety of database storage engines can also use a table, a table if requirements are relatively high transaction processing, you can choose InnoDB; this database can query requires relatively high table selection MyISAM storage; If the database requires a use in the temporary table query, you can select mEMORY storage engine.

5, separate read and write optimization:

With the increase in volume of users, database operations often become a bottleneck of the system, but the general system "read" the pressure is far greater than the "write", So we can be isolated by implementing a database to read and write - to improve the master-slave replication system performance.

From the main design ideas:

By providing separate read and write from the master database, the master database is responsible for "write", responsible for the "read" from the database, according to the pressure conditions, can be deployed to improve the plurality of "read" speed from the database, thereby improving overall system performance. Of course, we may need to configure multiple from the library based on other projects.

As shown in FIG,读写分离的实现,主要是解决主从数据库数据同步的问题,在主数据库写入数据后要保证从数据库的数据也要更新。主服务器master记录数据库操作日志到Binary log,从服务器开启i/o线程将二进制日志记录的操作同步到relay log(存在从服务器的缓存中),另外sql线程将relay log日志记录的操作在从服务器执行。

Master-Slave具体步骤

Preparatory work before this need to prepare two servers were installed Mysql database on it, as a Master, the other is Slave, of course, need more from a library can engage themselves more Slave. As shown below:

Two virtual machine servers:

Mysql database were installed:

1. master database profile modification mysql

[root@tjt03 ~]# vim /etc/my.cnf

Binary log opening arranged on the master server master, add the following mainly in [mysqld]:

ID = 132-Server 
log-bin bin-Master // = [must] enable binary logging 
log-bin-index = master- bin.index // [ must] unique server ID, the default is 1, and generally the last paragraph of IP

After configuration changes so heavy main library Mysql:

[root@tjt03 ~]# sudo service mysqld stop
Stopping mysqld (via systemctl):                           [  OK  ]
[root@tjt03 ~]# sudo service mysqld start
Starting mysqld (via systemctl):                           [  OK  ]
[root@tjt03 ~]# 

在主数据库检查配置效果:

mysql> SHOW MASTER STATUS;

可以看到下图表示配置没问题,这里面的File名:master-bin.000001 我们接下来在从数据库的配置会使用:

2.从mysql数据库配置文件修改

[root@tjt02 mysql]# vim /etc/my.cnf

在从服务器slave上的[mysqld]下面添加:

server-id=131    //这里面的server-id 一定要和主库的不同
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin

配置修改好后重从库Mysql:

[root@tjt02 mysql]# service mysqld stop
Stopping mysqld (via systemctl):                           [  OK  ]
[root@tjt02 mysql]# service mysqld start
Starting mysqld (via systemctl):                           [  OK  ]
[root@tjt02 mysql]# 

3、配置连个数据库的关联

首先我们先建立一个操作主从同步的数据库用户,切换到主数据库执行:

mysql> GRANT REPLICATION SLAVE ON *.* to 'tjt'@'%' identified by 'TANjintao@520';

这个配置的含义就是创建了一个数据库用户tjt,密码是TANjintao@520, 在从服务器使用tjt这个账号和主服务器连接的时候,就赋予其REPLICATION SLAVE的权限, *.* 表面这个权限是针对主库的所有表的,“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如192.168.145.226,加强安全。

进入从数据库执行授权Slave:

mysql> change master to master_host='172.16.114.132',master_port=3306,master_user='tjt',master_password='TANjintao@520',master_log_file='master-bin.000001',master_log_pos=0;

上述步骤执行完毕后执行start slave启动配置:

mysql> start slave;

停止主从同步的命令为:

mysql> stop slave;

查看状态命令,\G表示换行查看:

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.114.132
                  Master_User: tjt
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 870
               Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 1085
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB:

可以看到状态如下:

这里看到从数据库已经在等待主库的消息了,接下来在主库的操作,在从库都会执行了。我们可以主库负责写,从库负责读,达到读写分离的效果。

注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。

简单测试

在主数据库中创建一个新的数据库:

mysql> create database tjt0702;

在从数据库查看数据库:

mysql> show databases;

到这里,数据库的主从复制Master-Slave已经OK了。

代码层面实现读写分离

假设我们使用的是主流的SpringBoot框架开发的web项目,实现数据库读写分离如下。

配置了一个从库Slave:

https://blog.csdn.net/zhouzeqiang/article/details/87800590

配置了两个从库Slave:

https://my.oschina.net/u/560547/blog/1843462  

 

 

配置两个数据库的关联

配置两个数据库的关联

 

Guess you like

Origin www.cnblogs.com/taojietaoge/p/11117806.html