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

AI Hentai Generator
Generate AI Hentai for free.

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



In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

To fill in the MySQL username and password: 1. Determine the username and password; 2. Connect to the database; 3. Use the username and password to execute queries and commands.

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

Detailed explanation of database ACID attributes ACID attributes are a set of rules to ensure the reliability and consistency of database transactions. They define how database systems handle transactions, and ensure data integrity and accuracy even in case of system crashes, power interruptions, or multiple users concurrent access. ACID Attribute Overview Atomicity: A transaction is regarded as an indivisible unit. Any part fails, the entire transaction is rolled back, and the database does not retain any changes. For example, if a bank transfer is deducted from one account but not increased to another, the entire operation is revoked. begintransaction; updateaccountssetbalance=balance-100wh
