Processing math: 100%

MySQL读写分离

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介:

环境描述 :
系统版本:CentOS release 6.6 (Final)  2.6.32-504.el6.x86_64 
mysql版本:5.5.32

192.168.0.149:test-A(mysql-proxy)

192.168.0.150:test-B(master)

192.168.0.151:test-C(slave)

注:主从同步已做好,请移步:http://linuxzkq.blog.51cto.com/9379412/1700094


准备工作(test-A:mysql-proxy):

useradd mysql-proxy -s /sbin/nologin -M
cd tools

wget http://mirrors.sohu.com/mysql/MySQL-Proxy/mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz 
tar xf mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz
mv mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit /application/mysql-proxy
echo "export PATH=/application/mysql-proxy/bin/:$PATH" >>/etc/profile
tail -1 /etc/profile
source /etc/profile
which mysql-proxy
cd /application
chown -R mysql-proxy.root mysql-proxy


mysql-proxy命令参数:
[root@test-A ~]# mysql-proxy --help
Usage:
 mysql-proxy [OPTION...] - MySQL Proxy                           
--help-all                                                   Show all help options
--help-proxy                                              Show options for the proxy-module

Application Options:
 -V, --version                                              Show version
 --defaults-file=<file>                                configuration file
 --verbose-shutdown                                 Always log the exit code when shutting down
 --daemon                                                  Start in daemon-mode
 --user=<user>                                          Run mysql-proxy as user
 --basedir=<absolute path>                      Base directory to prepend to relative paths in the config
 --pid-file=<file>                                        PID file in case we are started as daemon
 --plugin-dir=<path>                                 path to the plugins
 --plugins=<name>                                    plugins to load
 --log-level=(error|warning|info|message|debug)          log all messages of level ... or higher
 --log-file=<file>                                        log all messages in a file
 --log-use-syslog                                        log all messages to syslog
 --log-backtrace-on-crash                          try to invoke debugger on crash
 --keepalive                                                 try to restart the proxy if it crashed
 --max-open-files                                        maximum number of open files (ulimit -n)
 --event-threads                                          number of event-handling threads (default: 1)
 --lua-path=<...>                                         set the LUA_PATH
 --lua-cpath=<...>                                       set the LUA_CPATH

 
 
master操作:
mysql> grant all on *.* to 'mysql-proxy'@'192.168.0.%' identified by '123456';
mysql> select user,host from mysql.user;

slave操作:
mysql> grant all on *.* to 'mysql-proxy'@'192.168.0.%' identified by '123456';
mysql> select user,host from mysql.user; 


vi /etc/init.d/mysql-proxy  #编辑mysql-proxy管理脚本(test-A:mysql-proxy)
#mysql-proxy.scripts,2015-11.25,linux
############################################
#!/bin/sh
export LUA_PATH=/application/mysql-proxy/share/doc/mysql-prox
y/?.lua:/applicaton/mysql-proxy/lib/mysql-proxy/lua/?.lua

mode=1if[z"mode" ] ; then
  mode="start"
fi

case mode in    'start')  mysql-proxy --daemon \  --log-level=debug \  --user=mysql-proxy \  --keepalive \  --log-file=/var/log/mysql-proxy.log \  --plugins="proxy" \  #mysql-proxy.scripts     mode="start"  fi   casemode in
  'start')
mysql-proxy --daemon \
--log-level=debug \
--user=mysql-proxy \
--keepalive \
--log-file=/var/log/mysql-proxy.log \
--plugins="proxy" \
--proxy-backend-addresses=192.168.0.150:3306 \
--proxy-read-only-backend-addresses=192.168.0.151:3307 \
--plugins="admin" \
--admin-username="admin" \
--admin-password="admin" \
    ;;

  'stop')
    killall mysql-proxy
    ;;

  'restart')
    if 0stop;then0 start
    else
      echo  "retart failed!!!"
      exit 1
    fi
    ;;
esac

exit 0
####################################################
chmod 700 /etc/init.d/mysql-proxy
/etc/init.d/mysql-proxy start


[root@test-A application]# netstat -tunlp|grep mysql-proxy
tcp        0      0 0.0.0.0:4040                0.0.0.0:*                   LISTEN      4022/mysql-proxy    
tcp        0      0 0.0.0.0:4041                0.0.0.0:*                   LISTEN      4022/mysql-proxy   
#4040是proxy端口;4041是admin端口,也就是管理

[root@test-C ~]# mysql -umysql-proxy -p -h 192.168.0.149 --port 4040
Enter password:   #多开几个终端,测试连接
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 32
Server version: 5.5.32-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select user,host from mysql.user;    


[root@test-C ~]# mysql -uadmin -p -h 192.168.0.149 --port 4041
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.99-agent-admin

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select * from backends;          
+-------------+--------------------+---------+------+------+-------------------+
| backend_ndx | address            | state   | type | uuid | connected_clients |
+-------------+--------------------+---------+------+------+-------------------+
|           1 | 192.168.0.150:3306 | up      | rw   | NULL |                 0 |
|           2 | 192.168.0.151:3307 | unknown | ro   | NULL |                 0 |
+-------------+--------------------+---------+------+------+-------------------+
2 rows in set (0.00 sec)
注意:因为rw-splitting.lua脚本默认有4个链接才启用分离;所以多开启几个终端;多测试几下;你也可以去修改里面的相关值

mysql> select * from backends;
+-------------+--------------------+-------+------+------+-------------------+
| backend_ndx | address            | state | type | uuid | connected_clients |
+-------------+--------------------+-------+------+------+-------------------+
|           1 | 192.168.0.150:3306 | up    | rw   | NULL |                 0 |
|           2 | 192.168.0.151:3307 | up    | ro   | NULL |                 0 |
+-------------+--------------------+-------+------+------+-------------------+
2 rows in set (0.00 sec)
#出现两个up,代表读写分离成功。

[root@test-A ~]# lsof -i:4040
COMMAND    PID        USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysql-pro 4062 mysql-proxy   10u  IPv4 156977      0t0  TCP *:yo-main (LISTEN)
mysql-pro 4062 mysql-proxy   12u  IPv4 160117      0t0  TCP test-A:yo-main->test-C:53173 (ESTABLISHED)
mysql-pro 4062 mysql-proxy   16u  IPv4 160132      0t0  TCP test-A:yo-main->192.168.0.254:56915 (ESTABLISHED)
mysql-pro 4062 mysql-proxy   17u  IPv4 160060      0t0  TCP test-A:yo-main->test-B:50068 (ESTABLISHED)
mysql-pro 4062 mysql-proxy   18u  IPv4 160145      0t0  TCP test-A:yo-main->192.168.0.254:56916 (ESTABLISHED)
mysql-pro 4062 mysql-proxy   19u  IPv4 159920      0t0  TCP test-A:yo-main->192.168.0.254:56914 (ESTABLISHED)



本文转自 linuxzkq 51CTO博客,原文链接:http://blog.51cto.com/linuxzkq/1716885

相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
打赏
0
0
0
0
344
分享
相关文章
MySQL的主从复制 && SpringBoot整合Sharding-JDBC解决读写分离
MySQL的主从复制 && SpringBoot整合Sharding-JDBC解决读写分离
233 0
Mycat【Mycat部署安装(核心配置及目录结构、安装以及管理命令详解)Mycat高级特性(读写分离概述、搭建读写分离、MySQL双主双从原理)】(三)-全面详解(学习总结---从入门到深化)
Mycat【Mycat部署安装(核心配置及目录结构、安装以及管理命令详解)Mycat高级特性(读写分离概述、搭建读写分离、MySQL双主双从原理)】(三)-全面详解(学习总结---从入门到深化)
886 0
如何实现 MySQL 的读写分离?
本文介绍了 MySQL 读写分离的实现方式及其主从复制原理,解释了如何通过主从架构提升读并发能力。重点分析了主从同步延时问题及解决方案,如半同步复制、并行复制等技术手段,并结合实际案例探讨了高并发场景下的优化策略。文章还提醒开发者在编写代码时需谨慎处理插入后立即查询的情况,避免因主从延时导致的数据不一致问题。
230 44
如何实现 MySQL 的读写分离?
MySQL 官宣:支持读写分离了!!
【10月更文挑战第8天】MySQL的读写分离功能显著提升了数据库性能、可用性和可靠性。通过将读写操作分配至不同服务器,有效减轻单个服务器负载,提高响应速度与吞吐量,并增强系统稳定性。此外,它还支持便捷的扩展方式,可通过增加只读服务器提升读操作性能。实现读写分离的方法包括软件层面(如使用数据库中间件)和硬件层面(使用独立服务器)。使用时需注意数据一致性、负载均衡及监控管理等问题。
345 0
MySQL主从复制实现读写分离
MySQL主从复制(二进制日志)、 Sharding-JDBC实现读写分离
MySQL主从复制实现读写分离
(二十五)MySQL主从实践篇:超详细版读写分离、双主热备架构搭建教学
在上篇《主从原理篇》中,基本上把主从复制原理、主从架构模式、数据同步方式、复制技术优化.....等各类细枝末节讲清楚了,本章则准备真正对聊到的几种主从模式落地实践,但实践的内容通常比较枯燥乏味,因为就是调整各种配置、设置各种参数等步骤。
803 3
开发者必看:MySQL主从复制与Laravel读写分离的完美搭配
开发者必看:MySQL主从复制与Laravel读写分离的完美搭配
131 2
mysql读写分离,主从同步
本文介绍了如何在Laravel项目中配置数据库读写分离,并实现MySQL主从同步。主要步骤包括:在`config/database.php`中设置读写分离配置;为主机授予从机访问权限;配置各MySQL服务器的`/etc/my.cnf`文件以确保唯一的`server-id`;以及通过SQL命令设置主从关系并启动从服务。文章还针对一些常见错误提供了排查方法。最后通过验证确认主从同步是否成功。[原文链接](https://juejin.cn/post/6901581801458958344)。版权所有者为作者佤邦帮主,转载请遵循相关规定。
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等