客户端访问数据的查询请求和写请求分别给不同的数据库服务处理
查询 --> select --> A数据库数据库 slave 写 --> insert update delete --> B数据库服务器 master |
1)减轻单台数据库服务器的并发访问压力
2)提高硬件的利用率
通过程序实现(让程序员写代码实现)
通过安装软件提供的服务来实现 [中间件] --->mycat ,mysql-proxy ,maxscale
采用
(Maxscale + mysql一主一从同步):
client [192.168.4.50]
中间件 Maxscale [192.168.4.57]
mysql-server (写) [192.168.4.51] msyql-server(读) [192.168.4.52]
master slave
--------------------环境准备[部署一主一从]
配置[192.168.4.51]主库
1.启用binlog日志
vim /etc/my.cnf |
validate_password_policy=0 log_bin[=master51] #[ ]内可写可不写 |
server_id=51 |
2.授权从库用户
3.查看binlog日志信息
配置 [192.168.4.52]从库
1.指定server_id
vim /etc/my.cnf |
[mysqld] |
validate_password_policy=0 validate_password_length=6 |
server_id=52 |
2.指定主服务器信息
mysql>change master to master_host="192.168.4.51",master_user="用户名",master_password="密码",master_log_file="参考主库binlog日志信息",master_log_pos=参考主库binlog日志 ;
3.启用slave进程
msyql>statr slave;
mysql>show slave status\G;
--------------------装包配置启服务
[192.168.5.57]#yum -y install maxscale-2.1.2-1.rhel.7.x86_64.rpm #该软件需从别的地方拷贝过来
]#rpm -ql maxscale
]#vim /etc/maxscale.cnf
[maxscale] [MySQL Monitor] #定义要监视的数据库节点 #[Read-Only Service] #注释掉,没有定义哪台服务器,只做请求 ... [Read-Write Service] [MaxAdmin Service] #管理服务 #[Read-Only Listener] #注释掉 [Read-Write Listener] ... [MaxAdmin Listener] |
--------------------用户授权[]
mysql>grant replication slave, replication client on *.* to scalemon@"%" identified by "123qqq...A"; #创建监控用户
mysql> grant select on mysql.* to scaleroute@"%" identified by "123qqq...A"; #路由用户
mysql> select user,host from mysql.user where user in ("scalemon","scaleroute");
--------------------启动服务
[192.168.4.57]#yum -y install mariadb #该机没有SQL命令,需装一个提供命令的软件【mariadb,mysql...】
[192.168.4.57]#mysql -h192.168.4.51 -uscalemon -p123qqq...A #测试用户是否能使用
[192.168.4.57]#mysql -h192.168.4.51 -uscaleroute -p123qqq...A
[192.168.4.57]#maxscale -f /etc/maxscale.cnf
[192.168.4.57]#netstat -nutlp | grep maxscale
tcp6 0 0 :::4016 :::* LISTEN 4948/maxscale
tcp6 0 0 :::4006 :::* LISTEN 4948/maxscale
[192.168.4.57]# killall -9 maxscale #关服务
--------------------测试配置
[root@host57 ~]# 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
-------------------+-----------------+-------+-------------+--------------------
mysql>create database db8;
mysql>create table db8.t1(id int);
mysql>grant select ,insert on db8.* to yaya8@"%" identified by "123qqq...A";
[192.168.4.50]#mysql -h192.1684.57 -P4006 -uyaya8 -p123qqq...A
mysql> select * from db8.t1;
mysql>inssert into db8.t1 values(50);
[192.168.4.52]#mysql -uroot -p123qqq...A -e "insert into db8.t1 values(52)"
[192.168.4.52]#mysql -uroot -p123qqq...A -e "select * from db8.t1"
[192.168.4.51]#mysql -uroot -p密码 -e "show master status"
在1台服务器上运行多的数据库服务
1)节约运维成本
2)提高硬件利用率
1)安装支持多实例服务的软件包 mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
解压软件,修改目录名 ,调整PATH变量
调整PATH变量[非必须操作选项]
]#vim /etc/profile --> export PATH=/usr/local/mysql/bin:$PATH ]#source /etc/profile |
[root@host50 ~]# mv mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql
[root@host50 ~]# cd /usr/local/mysql
2)修改服务的主配置文件
[root@host50 ~]#systemctl stop mysqld
[root@host50 ~]#systemctl disable mysqld #把之前的mysql服务停掉,并取消开机自启
]#mv /etc/my.cnf /root #把之前的移走,并停服务
配置格式:
[192.168.4.50]# 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=/dir1/mysql3307.log
pid-file=/dir1/mysql3307.pid
socket=/dir1/mysql3307.sock[mysqld2]
datadir=/dir2
port=3308
log-error=/dir2/mysql3308.log
pid-file=/dir2/mysql3308.pid
socket=/dir2/mysql3308.sock
[192.168.4.50]# mkdir /dir1 /dir2
3)启动服务
[192.168.4.50]# /usr/local/mysql/bin/mysqld_multi start 1 #初始化, 启动编号是1的服务
2019-05-14T08:34:45.136200Z 1 [Note] A temporary password is generated for root@localhost: 2orhEH+QLtG! 密码
]# /usr/local/mysql/bin/mysql -uroot -p'2orhEH+QLtG!' -S /dir1/mysql3307.sock #连接实例
mysql> alter user root@"localhost" identified by "123456";
mysql> show databases;
[192.168.4.50]#netstat -nutlp | grep mysqld
tcp6 0 0 :::3307 :::* LISTEN 6554/mysqld
[192.168.4.50]#killall -9 mysqld #关服务,不是必要操作
[root@host50 mysql]# ls /dir1
auto.cnf ibdata1 ib_logfile1 mysql mysql3307.pid mysql3307.sock.lock sys
ib_buffer_pool ib_logfile0 ibtmp1 mysql3307.log mysql3307.sock performance_schema
[192.168.4.50]# /usr/local/mysql/bin/mysqld_multi start 2
2019-05-14T08:45:33.522934Z 1 [Note] A temporary password is generated for root@localhost: MgY,rf0k;>XM
[192.168.4.50] # /usr/local/mysql/bin/mysql -uroot -p'MgY,rf0k;>XM' -S /dir2/mysql3308.sock
mysql> alter user root@"localhost" identified by "123456";
mysql> show databases;
mysql> exit
]# /usr/local/mysql/bin/mysql -uroot -p'123456' -S /dir2/mysql3308.sock #验证新密码
]# netstat -nutlp | grep -i mysqld
tcp6 0 0 :::3307 :::* LISTEN 6554/mysqld tcp6 0 0 :::3308 :::* LISTEN 6891/mysqld |
停止服务
]# /usr/local/mysql/bin/mysqld_multi --user=root --password=123456 stop 1
]# /usr/local/mysql/bin/mysqld_multi --user=root --password=123456 stop 2
访问多实例服务,对数据做操作
软优化(当你了解慢的问题出在哪里):
硬优化(设备优化):
3.升级硬件 (CPU 内存 存储)
4.网络带宽
5.优化服务架构(网络架构是否有数据传输瓶颈【单点故障】)
mysql> show variables;
mysql> show variables like "%关键字%"; #模糊查找
mysql> show variables like "%timeout%";
mysql> show variables like "connect_timeout";
mysql> show status;
mysql> show status like "%connect%";
mysql> show status like "Connections"
mysql> set [global] 变量=值; #临时修改变量,服务重启就还原了 global全局变量
]# vim /etc/my.cnf #配置永久修改变量
[mysqld]
变量名=值
.....
max_connections 允许的最大并发连接数
connect_timeout 等待连接超时,默认10s,仅登陆有效
wait_timeout 等待关闭连接的不活动超时秒数,默认28800s(8h)
key_buffer_size myisam引擎索引大小
mysql>show status like "Max_used_connections ";
公式:最大并发连接/最大并发连接数= 0.85
查询日志:记录在服务器上执行的所有命令
]#vim/ etc/my.cnf
general-log
]#重启
]# talif /var/lib/mysql
数据服务处理查询请求的过程
query_cache_size 默认1M
query_cache_type ON 查询缓存开启[默认不开]
查看当前的查询缓存统计
Qcache_hits 0 在缓存你查寻找到一次+1
Qcache_inserts 0 接收到查询请求+1