版本:5.7.28
mysql51:192.168.4.51 主
mysql52:192.168.4.52 从
mysql50:192.168.4.50 客户机
1.实现MySQL读写分离
搭建一主一从结构
配置maxscale代理服务器
测试分离配置
1.1 搭建一主一从结构
192.168.4.51 主
192.168.4.52 从
测试OK
1.2 实现mysql读写分离
1.2.1 配置数据读写分离服务器:192.168.4.50
]# wget https://downloads.mariadb.com/MaxScale/2.1.2/rhel/7/x86_64/maxscale-2.1.2-1.rhel.7.x86_64.rpm
]# yum -y install maxscale-2.1.2-1.rhel.7.x86_64.rpm
1.2.2 配置maxscale
]# vim /etc/maxscale.cnf
[maxscale]
threads=auto //运行的线程的数量
[server1] //定义数据库服务器
type=server
address=192.168.4.51 //数据库服务器的ip
port=3306
protocol=MySQLBackend //后端数据库
[server2]
type=server
address=192.168.4.52
port=3306
protocol=MySQLBackend
[MySQL Monitor] //定义监控的数据库服务器
type=monitor
module=mysqlmon
servers=server1, server2 //监控的数据库列表,不能写ip
user=scalemon //监视数据库服务器时连接的用户名scalemon
passwd=123qqq...A //密码123456
monitor_interval=10000 //监视的频率 单位为秒
#[Read-Only Service] //不定义只读服务器
#type=service
#router=readconnroute
#servers=server1
#user=myuser
#passwd=mypwd
#router_options=slave
[Read-Write Service] //定义读写分离服务
type=service
router=readwritesplit
servers=server1, server2
user=maxscaled //用户名 验证连接代理服务时访问数据库服务器的用户是否存在
passwd=123qqq...A //密码
max_slave_connections=100%
[MaxAdmin Service] //定义管理服务
type=service
router=cli
#[Read-Only Listener] //不定义只读服务使用的端口号
#type=listener
#service=Read-Only Service
#protocol=MySQLClient
#port=4008
[Read-Write Listener] //定义读写服务使用的端口号
type=listener
service=Read-Write Service
protocol=MySQLClient
port=4006
[MaxAdmin Listener] //管理服务使用的端口号
type=listener
service=MaxAdmin Service
protocol=maxscaled
socket=default
port=4099 //手动添加,不指定时使用的是默认端口在启动服务以后可以知道默认端口是多少
1.2.3 根据配置文件的设置,在数据库服务器上添加授权用户(主库执行,从库查看)
mysql> grant replication slave,replication client on *.* to scalemon@'%' identified by "123qqq...A";
//监控数据库服务器时,连接数据库服务器的用户
mysql> grant select on mysql.* to maxscaled@"%" identified by "123qqq...A";
//验证 访问数据时,连接数据库服务器使用的用户,是否在数据库服务器上存在的,连接用户
查看授权用户
在主库上面查看
mysql> select user,host from mysql.user where user in ("scalemon","maxscaled");
在从库上面查看
mysql> select user,host from mysql.user where user in ("scalemon","maxscaled");
+-----------+------+
| user | host |
+-----------+------+
| maxscaled | % |
| scalemon | % |
+-----------+------+
1.2.4 测试授权用户(50)
]# mysql -h 192.168.4.51 -u scalemon -p123qqq...A
]# mysql -h 192.168.4.52 -u scalemon -p123qqq...A
]# mysql -h 192.168.4.51 -u maxscaled -p123qqq...A
]# mysql -h 192.168.4.52 -u maxscaled -p123qqq...A
1.2.5 启动服务(50)
]# maxscale -f /etc/maxscale.cnf
]# ps -C maxscale
PID TTY TIME CMD
13665 ? 00:00:00 maxscale
]# netstat -antup | grep maxscale
tcp 0 0 192.168.4.50:46746 192.168.4.51:3306 ESTABLISHED 13665/maxscale
tcp 0 0 192.168.4.50:34570 192.168.4.52:3306 ESTABLISHED 13665/maxscale
tcp6 0 0 :::4099 :::* LISTEN 13665/maxscale
tcp6 0 0 :::4006 :::* LISTEN 13665/maxscale
测试,在本机访问管理端口查看监控状态
maxadmin -P端口 -u用户名 -p密码
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
-------------------+-----------------+-------+-------------+--------------------
1.2.6 在客户端访问读写分离服务器(53)
51上授权一个可以访问数据的用户
mysql> GRANT ALL ON *.* TO jim@"%" identified by "123qqq...A";
从库52查看
mysql> SELECT user,host FROM mysql.user where user='jim';
+------+------+
| user | host |
+------+------+
| jim | % |
+------+------+
53连接读写分离服务器50
]# mysql -h192.168.4.50 -P4006 -ujim -p123qqq...A
mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| mysql52 |
+------------+
mysql> CREATE DATABASE lisi;
此时,53从50登录(写入数据),数据直接写入51,同步到52,
既读在52,写在51,读写分离
2.配置MySQL多实例
直接用50主机
2.1 清理配置
]# systemctl stop mysqld
]# mv /etc/my.cnf /root/
]# tar -zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
]# mv mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql
]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
2.1 编写配置文件
]# vim /etc/my.cnf
[mysqld_multi] //启用多实例
mysqld = /usr/local/mysql/bin/mysqld_safe //指定进程文件路径
mysqladmin = /usr/local/mysql/bin/mysqladmin //指定管理命令路径
user = root //指定进程用户
[mysqld1] //实例进程名称
port=3307 //端口号
datadir=/data3307 //数据库目录 ,要手动创建
socket=/data3307/mysql1.sock //指定sock文件的路径和名称
pid-file=/data3307/mysql1.pid //进程pid号文件位置
log-error=/data3307/mysql1.err //错误日志位置
[mysqld2]
port=3308
datadir=/data3308
socket=/data3308/mysql2.sock
pid-file=/data3308/mysql2.pid
log-error=/data3308/mysql2.err
2.2 创建数据库目录
]# mkdir -p /data3307
]# mkdir -p /data3308
2.3 创建进程运行的所有者和组 mysql
]# useradd mysql
]# chown mysql:mysql /data*
2.4 初始化授权库
]# mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/data3307 --initialize
...A temporary password is generated for root@localhost: uB,:4o7TLw5d
]# mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/data3308 --initialize
...A temporary password is generated for root@localhost: B37?XppeaIqc
2.5 调整PATH变量
]# echo "export PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile
]# source /etc/profile
]# echo $PATH
2.6 启动多实例
]# mysqld_multi start 1 //1为实例编号
]# mysqld_multi start 2
2.7 查看端口
]# netstat -utnlp | grep :3307
tcp6 0 0 :::3307 :::* LISTEN 17757/mysqld
]# netstat -utnlp | grep :3308
tcp6 0 0 :::3308 :::* LISTEN 17918/mysqld
mysql]# ps -C mysqld
PID TTY TIME CMD
17757 pts/0 00:00:00 mysqld
17918 pts/0 00:00:00 mysqld
2.8 访问多实例
使用初始化密码登录多实例1(可以使用了)
]# mysql -u root -p'uB,:4o7TLw5d' -S /data3307/mysql1.sock
mysql> alter user root@"localhost" identified by '123456';
使用初始化密码登录多实例2(可以使用了)
]# mysql -u root -p'B37?XppeaIqc' -S /data3308/mysql2.sock
mysql> alter user root@"localhost" identified by '123456';
3.MySQL性能优化
练习相关优化选项
启用慢查询日志
查看各种系统变量、状态变量
3.1 mysql性能优化
3.1.1 查看服务运行时的参数配置
mysql> show variables\G
524个
mysql> show variables like "%innodb%";
134个
3.1.2 并发连接数量
mysql> FLUSH STATUS;
mysql> show global status like "Max_used_connections";
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 3 |
+----------------------+-------+
查看默认的最大连接数
mysql> show variables like "max_connections%";
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
3.1.3 连接超时时间
mysql> show variables like "%timeout%";
+-----------------------------+----------+
| Variable_name | Value |
+-----------------------------+----------+
| connect_timeout | 10 |
| delayed_insert_timeout | 300 |
| have_statement_timeout | YES |
| innodb_flush_log_at_timeout | 1 |
| innodb_lock_wait_timeout | 50 |
| innodb_rollback_on_timeout | OFF |
| interactive_timeout | 28800 |
| lock_wait_timeout | 31536000 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| rpl_stop_slave_timeout | 31536000 |
| slave_net_timeout | 60 |
| wait_timeout | 28800 |
+-----------------------------+----------+
3.1.4 允许保存在缓存中被重用的线程数量
mysql> show variables like "thread_cache_size";
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| thread_cache_size | 9 |
+-------------------+-------+
3.1.5 用于MyISAM引擎的关键索引缓存大小
mysql> show variables like "key_buffer_size";
+-----------------+---------+
| Variable_name | Value |
+-----------------+---------+
| key_buffer_size | 8388608 |
+-----------------+---------+
3.1.6 为每个要排序的线程分配此大小的缓存空间
mysql> show variables like "sort_buffer_size";
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| sort_buffer_size | 262144 |
+------------------+--------+
3.1.7 为顺序读取表记录保留的缓存大小
mysql> show variables like "read_buffer_size";
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| read_buffer_size | 131072 |
+------------------+--------+
3.1.8 为所有线程缓存的打开的表的数量
mysql> show variables like "table_open_cache";
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| table_open_cache | 2000 |
+------------------+-------+
3.2 SQL查询优化
启用慢查询日志
]# vim /etc/my.cnf
...
slow_query_log=1
slow_query_log_file=mysql-slow.log
long_query_time=5
log_queries_not_using_indexes=1
查看慢查询日志
]# mysqldumpslow /var/lib/mysql/mysql-slow.log
Reading mysql slow query log from /var/lib/mysql/mysql-slow.log
Count: 1 Time=0.00s (0s) Lock=0.00s (0s) Rows=0.0 (0), 0users@0hosts
查看缓存的大小
mysql> show variables like "query_cache%";
+------------------------------+---------+
| Variable_name | Value |
+------------------------------+---------+
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 1048576 |
| query_cache_type | OFF |
| query_cache_wlock_invalidate | OFF |
+------------------------------+---------+
查看当前的查询缓存统计
mysql> show global status like "qcache%";
+-------------------------+---------+
| Variable_name | Value |
+-------------------------+---------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 1031832 |
| Qcache_hits | 0 |
| Qcache_inserts | 0 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 24 |
| Qcache_queries_in_cache | 0 |
| Qcache_total_blocks | 1 |
+-------------------------+---------+
来源:https://www.cnblogs.com/luwei0915/p/12258221.html