MySQL Proxy的安装及基本命令使用教程_MySQL
MySQL Proxy最壮大的一项功能是告终“读写离别(Read/Write Splitting)”。它的原理是让主数据库处理事务性查询,而从数据库处理SELECT查询。数据库复制被用来把事务性查询导致的改变同步到集群中的从数据库。
0.必备软件:
1、LUA
能够去LUA的官方下载:dpa.nsysu.edu.tw/Downloads/MySQL-Proxy/。
可能去MYSQL官方下载源代码。
我这里下载了:
mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/mysql-proxy-0.6.0.tar.gz
3、测验过程中废止了B和C的REPLICATION。这么SQL语句一下子就看出来从哪里来的。
万一是M-S(能够先在SLAVE上举行STOP SLAVE)
1. 安装
下载已经编译好的安装包,或者预编译安装包均可,在这里,使用预编译版本。
[@s1.yejr.com ~]# tar zxf mysql-proxy-0.6.0-linux-rhas4-x86.tar.gz [@s1.yejr.com ~]# cd mysql-proxy-0.6.0-linux-rhas4-x86 #可以看到有2个目录 [@s1.yejr.com mysql-proxy-0.6.0-linux-rhas4-x86]# ls sbin share [@s1.yejr.com mysql-proxy-0.6.0-linux-rhas4-x86]# mv sbin/mysql-proxy /usr/local/sbin/ [@s1.yejr.com mysql-proxy-0.6.0-linux-rhas4-x86]# ls share mysql-proxy tutorial-constants.lua tutorial-packets.lua tutorial-rewrite.lua tutorial-warnings.lua tutorial-basic.lua tutorial-inject.lua tutorial-query-time.lua tutorial-states.lua #将lua脚本放到/usr/local/share下,以备他用 [@s1.yejr.com mysql-proxy-0.6.0-linux-rhas4-x86]# mv share/mysql-proxy /usr/local/share/ #删除符号连接等垃圾代码 [@s1.yejr.com mysql-proxy-0.6.0-linux-rhas4-x86]# strip /usr/local/sbin/mysql-proxy
2. 启动
编译一下启动管理脚本:
[@s1.yejr.com ~]# vi /etc/init.d/mysql-proxy #!/bin/sh export LUA_PATH=/usr/local/share/mysql-proxy/?.lua mode=$1 if [ -z "$mode" ] ; then mode="start" fi case $mode in 'start') mysql-proxy --daemon \ --admin-address=:4401 \ --proxy-address=:3307 \ --proxy-backend-addresses=:3306 \ --proxy-read-only-backend-addresses=192.168.133.232:3306 \ --proxy-read-only-backend-addresses=10.10.74.61:3306 \ --proxy-lua-script=/usr/local/share/mysql-proxy/rw-splitting.lua ;; 'stop') killall mysql-proxy ;; 'restart') if $0 stop ; then $0 start else echo "retart failed!!!" exit 1 fi ;; esac exit 0
现在解释一下启动脚本:
--daemon 采用daemon方式启动
--admin-address=:4401 指定mysql proxy的管理端口,在这里,表示本机的4401端口
--proxy-address=:3307 指定mysql proxy的监听端口,也可以用 127.0.0.1:3307 表示
--proxy-backend-addresses=:3306 指定mysql主机的端口
--proxy-read-only-backend-addresses=192.168.1.1:3306 指定只读的mysql主机端口
--proxy-read-only-backend-addresses=192.168.1.2:3306 指定另一个只读的mysql主机端口
--proxy-lua-script=/usr/local/share/mysql-proxy/rw-splitting.lua 指定lua脚本,在这里,使用的是rw-splitting脚本,用于读写分离
完整的参数可以运行以下命令查看:
mysql-proxy --help-all
运行以下命令启动/停止/重启mysql proxy:
[@s1.yejr.com ~]# /etc/init.d/mysql-proxy start [@s1.yejr.com ~]# /etc/init.d/mysql-proxy stop [@s1.yejr.com ~]# /etc/init.d/mysql-proxy restart
3. 试用
[@s1.yejr.com ~]# mysql -h127.0.0.1 -uroot -P3307
mysql> show processlist;
+-------+------+----------------+------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +-------+------+----------------+------+---------+------+-------+------------------+ | 30052 | root | localhost:9656 | NULL | Query | 0 | NULL | show processlist | +-------+------+----------------+------+---------+------+-------+------------------+
可以看到,产生了一个新连接。
用sysbench测试一下,看会不会挂掉:
[@s1.yejr.com ~]# sysbench --test=oltp --mysql-table-engine=innodb --oltp-table-size=1000000 \ --mysql-socket=/tmp/mysql.sock --mysql-user=root --mysql-db=test prepare [@s1.yejr.com ~]# sysbench --test=oltp --mysql-table-engine=innodb --oltp-table-size=1000000 \ --mysql-socket=/tmp/mysql.sock --mysql-user=root --mysql-db=test run
......... ......... Threads fairness: events (avg/stddev): 10000.0000/0.00 execution time (avg/stddev): 23.0387/0.00
还好,没给大家丢脸,剩下的测试自己完成吧 :)
4. 其他
mysql proxy还可以实现连接池的功能,这在很多LAMP开发中是软肋,因此,有了mysql proxy,就可以不用再担心连接数超限的问题了。
如果使用rw-splitting.lua脚本的话,最好修改以下2个参数的默认值:
min_idle_connections = 1 max_idle_connections = 3
以上就是MySQL Proxy的安装及基本命令使用教程_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!

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

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
