(DB2.2) MySQL separate read and write, tuning

A, MySQL separate read and write data: query request to access the data repository and almost ends each write request to a different data processing servers

Query select A slave database servers
write insert update delete B database server master

Read and write separation principle
  • Multiple MySQL servers
    - are provided reading, writing services, a balanced flow
    - to maintain the consistency of data from the main copy
  • MySQL proxy for the client
    - received write SQL requests to the server A process
    - SQL read receipt request to the server B process
    - specific policy by the distinguished service settings
Why read and write data separation?
 减轻单台数据库服务器的并发访压力
 提高机器硬件的利用率
How to separate read and write data

1, the program realization (let programmers write code to achieve) by
2, the services provided by installing software (middleware)
myCat MySQL-Proxy maxScale ...

Separate read and write configuration data

client
middleware
mysql-server
configuration steps:

1, the deployment of a master-slave synchronization mysql structure
1.1 Configuring the master server 51 (binlog log enable users authorized to view binlog log information)
[root@51 ~]# vim /etc/my.cnf
[mysqld]
server_id=51
log-bin=master51
[root@51 ~]# systemctl restart mysqld
[root@51 ~]# mysql -uroot -p123456
mysql> grant replication slave on *.* to repluser@"%" identified by "123qqq...A";
mysql> show master status;
1.2 Configuration (designated primary server information start the slave viewer information specifying the server_id) from the server 52
[root@52 ~]# vim /etc/my.cnf
[mysqld]
server_id=52
[root@52 ~]# systemctl restart mysqld
[root@52 ~]# mysql -uroot -p"Taren1.com"
mysql> change master to master_host="192.168.4.51",master_user="repluser",master_password="123qqq...A",master_log_file="master51.000005",master_log_pos=441;
mysql> start slave;
mysql> show slave status\G;
2, separate read and write the configuration data server 57
2.1 copy packages to the host 57

[root @ room9pc01 ~] # scp '/ root / Desktop / aaaaa / Database Management / package /soft/mysql/maxscale-2.1.2-1.rhel.7.x86_64.rpm' [email protected] : / root

2.2 Installing the software

[root@57 ~]# yum -y install maxscale-2.1.2-1.rhel.7.x86_64.rpm

2.3 modify the configuration file
[root@57 ~]# vim /etc/maxscale.cnf
   9 [maxscale]
 10 threads=1    //定义线程个数
 18 [server1]    //定义数据库服务器主机名
 19 type=server
 20 address=192.168.4.51  //master主机ip地址
 21 port=3306
 22 protocol=MySQLBackend
 23 
 24 [server2]     //定义数据库服务器
 25 type=server
 26 address=192.168.4.52      //slave主机ip地址
 27 port=3306
 28 protocol=MySQLBackend 

 35 [MySQL Monitor]   //定义要监视的数据库节点
 36 type=monitor
 37 module=mysqlmon
 38 servers=server1,server2  //主从数据库的主机名
 39 user=scalemon  //用户名
 40 passwd=123qqq...A    //密码
 41 monitor_interval=10000

 52 #[Read-Only Service]     
 53 #type=service
 54 #router=readconnroute
 55 #servers=server1
 56 #user=myuser
 57 #passwd=mypwd
 58 #router_options=slave

 63 [Read-Write Service]   //路由用户(定义读写分离的数据库节点)
 64 type=service
 65 router=readwritesplit
 66 servers=server1,server2    //主、从数据库的主机名
 67 user=scaleroute      //用户名
 68 passwd=123qqq...A    //密码
 69 max_slave_connections=100%

 85 #[Read-Only Listener]
 86 #type=listener
 87 #service=Read-Only Service
 88 #protocol=MySQLClient
 89 #port=4008

 97 [MaxAdmin Listener]    //定义管理服务的端口号
 98 type=listener
 99 service=MaxAdmin Service
100 protocol=maxscaled
101 socket=default
102 port=4016
2.4 User authorization (add corresponding user on database server 2 according to the profile settings)

Monitoring user

mysql> grant replication slave,replication client on *.* to scalemon@"%" identified by "123qqq...A";

Routing users

mysql> grant select on mysql.* to scaleroute@"%" identified by "123qqq...A";
2.5 start the service (192.168.4.57)

2.51 Verify authorized users

[root@57 ~]# mysql -h192.168.4.51 -uscalemon -p123qqq...A
[root@57 ~]# mysql -h192.168.4.51 -uscaleroute -p123qqq...A

2.52 Start Service

[root@57 ~]# maxscale -f /etc/maxscale.cnf     启动服务
[root@57 ~]# ps -C maxscale
[root@57 ~]# ss -ntulp | grep maxscale
[root@57 ~]# killall -9 maxscale       //关闭服务
3 test configuration
3.1 Information Management View
[root@57 ~]# maxadmin -uadmin -pmariadb -P4016
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server             | Address         | Port  | Connections | Status              
-------------------+-----------------+-------+-------------+--------------------
server1            | 192.168.4.51    |  3306 |           0 | Master, Running
server2            | 192.168.4.52    |  3306 |           0 | Slave, Running
-------------------+-----------------+-------+-------------+--------------------
3.2 separation test data read (data access client connection 192.168.4.57)

3.2.1 Adding access to data on the primary database server user

[root@51 ~]# mysql -uroot -p123456
mysql> create database db8;
mysql> create table db8.t1(id int);
mysql> grant select,insert on db8.* to yaya@"%" identified by "123qqq...A";

3.2.2 client 50, the host 57 to access the data connection

[root@51 ~]# mysql -uroot -p123456
mysql> insert into db8.t1 values(11111);
[root@52 ~]# mysql -uroot -p"Taren1.com"
mysql> insert into db8.t1 values(222222);
[root@50 ~]# mysql -h192.168.4.57 -P4006 -uyaya -p123qqq...A
mysql> select * from db8.t1;
mysql> insert into db8.t1 values(11111);
mysql> select * from db8.t1;

Two, MySQL instances

1. What is multi-instance? Running multiple database servers on a single server
2, Why use multi-instance? Saving operation and maintenance costs, improve hardware utilization
3, configuring multiple instances
3.1 to install the software

[root@50 ~]# mv mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql
[root@50 ~]# cd /usr/local/mysql/
[root@50 mysql]# ls bin

3.2 modify the main configuration file services

[root@50 mysql]# systemctl stop mysqld
[root@50 mysql]# systemctl disable mysqld
Removed symlink /etc/systemd/system/multi-user.target.wants/mysqld.service.
[root@50 mysql]# mv /etc/my.cnf /root/
[root@50 mysql]# vim /etc/my.cnf

[mysqld_multi]   //启用多实例
mysqld = /usr/local/mysql/bin/mysqld_safe   //指定进程文件路径
mysqladmin = /usr/local/mysql/bin/mysqladmin   //指定管理命令路径
user = root    //指定进程用户

[mysqld1]   /实例进程名称
datadir = /dir1   //数据库目录
port = 3307    //端口号
log_error = /dir/mysql3307.log   //错误日志文件
pid-file = /dir1/mysql3307.pid  //进程 pid 文件位置
socket = /dir1/mysql3307.sock   //指定sock文件的路径和名称

[mysqld2]
datadir = /dir2   
port = 3308 
log_error = /dir/mysql3308.log 
pid-file = /dir2/mysql3308.pid 
socket = /dir2/mysql3308.sock 
[root@50 ~]# mkdir /dir1
[root@50 ~]# mkdir /dir2

Database Directory
port number
error log file
pid document
socket
3.3 start the service
[root @ 50 MySQL] # / usr / local / MySQL / bin / mysqld_multi Start 1
[root @ 50 MySQL] # / usr / local / MySQL / bin / MySQL -p'f -uroot-: zfya1q5kBB '-S /dir1/mysql3307.sock
MySQL> User ALTER the root @ "localhost" IDENTIFIED by "123456";
[@ 50 MySQL the root] # / usr / local / MySQL / bin / the mysqld_multi - the root --password = 123456 = -user STOP. 1
[@ 50 MySQL the root] # / usr / local / MySQL / bin / Start the mysqld_multi 2
[the root MySQL @ 50] # / usr / local / MySQL / bin / MySQL -uroot-- p'ok) the I * 1ft the H8 & < '-S /dir2/mysql3308.sock
MySQL> User ALTER the root @ "localhost" IDENTIFIED by "123456";
[@ 50 MySQL the root] # / usr / local / MySQL / bin / the mysqld_multi - the root --password = 123456 = -user STOP 2
. 4, verify the configuration
4.1 access multiple instances of a service, the data for the operation

Three, MySQL Performance Tuning

1 upgrade hardware (cpu, memory, storage) operation and maintenance unit
2 operating parameters to optimize database service

mysql> show variables;  //显示当前所有变量
mysql> show variables like "%关键字%";
mysql> show variables like "%timeout%";
mysql> set globel 变量名=值;
mysql> set globel connect_timeout=5;
[root@50 ] vim /etc/my.cof
[mysqld]
	  	 变量名=值
mysql> show status;
mysql> show status like "%connect%";
mysql> show status like "Connections";
常用设置参数:
max_connections	 允许的最大并发连接数
mysql>  show status like "Max_used_connections";
最大并发连接/最大并发连接数 = 0.85 x  100% = 85%
并发及连接控制
Max_used_connections/max_connections=0.85
connect_timeout	 等待连接超时,默认10秒,仅登录时有效
wait_timeout 等待关闭连接的不活动超时秒数,默认28800妙(8小时)
缓存参数控制
key_buffer_size  用于MyISAM引擎的关键索引缓存大小
sort_buffer_size   为每个要排序的线程分配此大小的缓存空间
read_buffer_size   为顺序读取表记录保留的缓存大小
thread_cache_size   允许保存在缓存中被重用的线程数量
table_open_cache   为所有线程缓存的打开的表的数量

3 allows programmers to access data optimized sql command
optimization services query cache
data services during query processing requests?
View variables associated with the cache
show variables like "query_cache%";

Network bandwidth 4
5 Optimization Service Architecture (check whether there is data transmission bottleneck in the network architecture)

Guess you like

Origin blog.csdn.net/weixin_45048541/article/details/90200491