mysql集群一:主从复制,通过mysql-proxy做负载均衡_MySQL
Mysql集群
mysql集群架构方式很多,根据不同的需求做不一样的架构,简单一点的就是mysql的replication,也就是Mysql的复制功能,模式有:master-slaves,master-slaves-slaves,master-master-slaves等可以有多个分层,那么现在我所要说的是master-slaves的模式(其他的模式原理基本都一样),然后再通过mysql官方提供的Mysql-proxy实现读写分离,达到负载均衡的效果。
环境:
主机:master:192.168.1.109,slave1:192.168.1.110,mysqlProxy:192.168.1.112。(workstation10平台创建虚拟机模拟)
操作系统:Red Hat Enterprise Linux Server release 5.8
mysql:mysql-5.5.37-linux2.6-i686.tar.gz
mysql-proxy:mysql-proxy-0.8.4-linux-glibc2.3-x86-32bit.tar.gz
1、安装mysql:
[root@localhost ~]# cd /usr/local //进入到安装目录
[root@localhost local]# tar -zxvf mysql-5.5.37-linux2.6-i686.tar.gz //解压mysql二进制包
[root@localhost local]# ln -s mysql-5.5.37-linux2.6-i686 mysql //解压之后加上链接
[root@localhost local]# groupadd mysql //增加mysql用户组
[root@localhost local]# useradd -g mysql mysql //增加mysql用户帐号
[root@localhost local]# cd mysql //进入安装之后的mysql目录
[root@localhost mysql]# chown -R root:mysql . //修改文件属组和属主,注意该命令后面有一个点,表示当前目录
[root@localhost mysql]# chown -R mysql:mysql data/ //把data目录的属主改为mysql
[root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf //拷贝mysql配置文件放在etc目录下并改名为my.cnf
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld //拷贝mysql的启动脚本放在/etc/init.d/目录下
[root@localhost mysql]# service mysqld start //启动mysql
至此mysql安装已经结束,接下来就是mysql的主从配置。(其他两台主机也照此安装mysql)
2、配置master:
[root@localhost mysql]# vi /etc/my.cnf //编辑配置文件
server-id=1 //默认是1,不是的话则改为1
thread_concurrency = 2 //线程并发数(CPU*2)
log-bin=mysql-bin //打开二进制日志功能
保存退出,重新启动mysql。
登入mysql:
[root@localhost mysql]# mysql -uroot -p //默认密码空,直接敲回车进入
mysql> grant replication slave on *.* to repl@'192.168.1.%' identified by '123456';
mysql>flush privileges;
mysql>show grants for 'repl'@'192.168.1.%'; //查看授权,有记录说明ok
mysql>show master status/G; //查看master服务器状态,有二进制日志文件名和记录位置(position)
至此mysql配置完成。
3、配置slave
[root@localhost mysql]#vi /etc/my.cnf //编辑配置文件
server-id=11 //设置与master不一样就行,
thread_concurrency = 2 //线程并发数(CPU*2)
relay-log=mysql-log //打开中继日志
relay-log-index=mysql-log.index //设置relay-log-index文件名
保存退出,重新启动mysql。
登入mysql:
[root@localhost mysql]# mysql -uroot -p //默认密码空,直接敲回车进入
mysql>change master to
->master_host='192.168.1.109', //master服务器ip
->mastet_user='repl', //用户名
->mastet_password='123456', //密码
->master_log_file='mysql-bin.000001', //master服务器二进制日志名
->master_log_pos=107;
mysql>slave start; //启动slave
mysql> SHOW SLAVE STATUS/G; //查看slave状态
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.109
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 4
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: yes
Slave_SQL_Running: yes
...omitted...
Seconds_Behind_Master: NULL
如果 Slave_IO_Running和Slave_SQL_Running显示为yes则配置ok,过程中可能会出现:
Slave I/O: error connecting to master 'repl@192.168.1.109:3306' - retry-time: 60 retries: 86400, Error_code: 2003
引起问题原因是防火墙拦截了,解决办法就是修改防火墙配置,简单直接的办法就是把master服务器的防火墙关掉,执行/etc/init.d/iptables stop命令。
接下来测试,在master中创建一个数据库db_test,查看slave中是是否存在db_test。
至此slave配置结束,其他从服务器按此方式配置。
4、配置mysql-proxy,代理服务最好另外部署一台机器,尤其是上线运行后,所以这里测试也是用了单独一台虚拟机,没有部署在master或slave机器上。
[root@localhost local]# tar -zxvf mysql-proxy-0.8.4-linux-glibc2.3-x86-32bit.tar.gz //解压
[root@localhost local]# ln -s mysql-proxy-0.8.4-linux-glibc2.3-x86-32bit mysql-proxy //加一个快捷链接
[root@localhost local]# groupadd mysql-proxy //创建用户组
[root@localhost local]# useradd -g mysql-proxy mysql-proxy //创建用户
[root@localhost local]# cd mysql-proxy //进入mysql-proxy目录
[root@localhost mysql-proxy]# chown -R root:mysql-proxy . //更改目录属主和属组,命令后面有一个点(.)表示当前目录
[root@localhost mysql-proxy]# vi /etc/profile.d/mysql-proxy.sh //把mysql的bin目录加到PATH变量中,
export
PATH=$PATH:
/usr/local/mysql-proxy/bin
[root@localhost mysql-proxy]# source /etc/profile //让配置文件立刻生效
[root@localhost mysql-proxy]# mysql-proxy --help-all //查看mysql-proxy命令,出现如下信息:
<br>
mysql-proxy 命令
--help-all ———— 用于获取全部帮助信息
--proxy-address=host:port ———— 代理服务监听的地址和端口
--admin-address=host:port ———— 管理模块监听的地址和端口
--proxy-backend-addresses=host:port ———— 后端mysql服务器的地址和端口(主服务器)
--proxy-read-only-backend-addresses=host:port ———— 后端只读mysql服务器的地址和端口(从服务器)
--proxy-lua-script=file ———— 完成mysql代理功能的Lua脚本
--daemon ———— 以守护进程模式启动mysql-proxy
--defaults-file=/path/to/conf_file_name ———— 默认使用的配置文件路径
--log-file=/path/to/log_file_name ———— 日志文件名称
--log-level=level ———— 日志级别
--log-use-syslog ———— 基于syslog记录日志
--user=user_name ———— 运行mysql-proxy进程的用户
2014-05-10 00:09:22: (critical) plugin proxy 0.8.4 started<br>2014-05-10 00:09:22: (debug) max open file-descriptors = 1024<br>2014-05-10 00:09:22: (message) proxy listening on port :4040<br>2014-05-10 00:09:22: (message) added read/write backend: 192.168.1.109:3306<br>2014-05-10 00:09:22: (message) added read-only backend: 192.168.1.110:3306
[root@localhost mysql-proxy]#netstat -ntulp | grep :4040 //查看监听的端口
tcp 0 0 0.0.0.0:4040 0.0.0.0:* LISTEN 10056/mysql-proxy
现在我们需要在master服务器中创建一个远程登入的mysql账号
master:
mysql> GRANT ALL ON *.* TO root@
'192.168.1.%'
IDENTIFIED BY
'123456'
;
Query OK, 0 rows affected (0.07 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.04 sec)
在从服务器上做测试:slave:[root@bogon ~]
# mysql -uroot -p123456 -h192.168.1.112 --port=4040


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.
