直接从命令行上启动 MySQL Proxy :
shell> mysql-proxy
在大多数情况下,你至少应该确定 MySQL Proxy 应该将 query 转发到哪个 backend MySQL server ,即需要指定其 host 名或地址,以及端口号信息。
可以在命令行中直接指定选项,也可以在配置文件中指定,同时使用 --defaults-file 命令行参数指定配置文件的位置。
如果你采用了配置文件的方式,其格式需要按照如下配置:
1.在 [mysql-proxy] 配置组中指定选项,例如:
[mysql-proxy]
admin-address = host:port
2.以配置名和配置值对的形式指定全部配置选项。
3.对于简单开关形式的命令行选项(例如 --proxy-skip-profiling),使用 true 或者 false 。例如,下面的形式是不合法的:
[mysql-proxy]
proxy-skip-profiling
但下面是合法的
[mysql-proxy]
proxy-skip-profiling = true
4.设置配置文件的 Unix 权限为 0660(对于用户和组可读、可写,对于其他人不可访问)
上述条件任意一个不满足都会导致 mysql-proxy 在启动时产生错误。
下面的表格中列出了支持的选项配置。
Table 15.4. mysql-proxy Help Options
Format | Option File | Description |
---|---|---|
--help | Show help options | |
--help-admin | Show admin module options | |
--help-all | Show all help options | |
--help-proxy | Show proxy module options |
Table 15.5. mysql-proxy Admin Options
Format | Option File | Description |
---|---|---|
--admin-address=host:port | admin-address=host:port | The admin module listening host and port |
--admin-lua-script=file_name | admin-lua-script=file_name | Script to execute by the admin module |
--admin-password=password | admin-password=password | Authentication password for admin module |
--admin-username=user_name | admin-username=user_name | Authentication user name for admin module |
--proxy-address=host:port | proxy-address=host:port | The listening proxy server host and port |
Table 15.6. mysql-proxy Proxy Options
Format | Option File | Description | Removed |
---|---|---|---|
--no-proxy | no-proxy | Do not start the proxy module | |
--proxy-backend-addresses=host:port | proxy-backend-addresses=host:port | The MySQL server host and port | |
--proxy-fix-bug-25371 | proxy-fix-bug-25371 | Enable the fix for Bug #25371 for older libmysql versions | 0.8.1 |
--proxy-lua-script=file_name | proxy-lua-script=file_name | Filename for Lua script for proxy operations | |
--proxy-pool-no-change-user | proxy-pool-no-change-user | Do not use the protocol CHANGE_USER command to reset the connection when coming from the connection pool | |
--proxy-read-only-backend-addresses=host:port | proxy-read-only-backend-addresses=host:port | The MySQL server host and port (read only) | |
--proxy-skip-profiling | proxy-skip-profiling | Disable query profiling |
Table 15.7. mysql-proxy Applications Options
Format | Option File | Description |
---|---|---|
--basedir=dir_name | basedir=dir_name | The base directory prefix for paths in the configuration |
--daemon | daemon | Start in daemon mode |
--defaults-file=file_name | The configuration file to use | |
--event-threads=count | event-threads=count | The number of event-handling threads |
--keepalive | keepalive | Try to restart the proxy if a crash occurs |
--log-backtrace-on-crash | log-backtrace-on-crash | Try to invoke the debugger and generate a backtrace on crash |
--log-file=file_name | log-file=file_name | The file where error messages are logged |
--log-level=level | log-level=level | The logging level |
--log-use-syslog | log-use-syslog | Log errors to syslog |
--lua-cpath=dir_name | lua-cpath=dir_name | Set the LUA_CPATH |
--lua-path=dir_name | lua-path=dir_name | Set the LUA_PATH |
--max-open-files=count | max-open-files=count | The maximum number of open files to support |
--pid-file=file_name | pid-file=file_name | File in which to store the process ID |
--plugin-dir=dir_name | plugin-dir=dir_name | Directory containing plugin files |
--plugins=plugin,... | plugins=plugin,... | List of plugins to load |
--user=user_name | user=user_name | The user to use when running mysql-proxy |
--version | Show version information |
(略)
最通常的用法为作为一个简单的 proxy 服务使用(即不使用任何附件的脚本)。在基本 proxy 运行中,你必须通过 proxy-backend-addresses 选项指定至少一个默认要连接的 MySQL 服务器:
shell> mysql-proxy --proxy-backend-addresses=MySQL.example.com:3306
默认 proxy 端口为 4040 ,故你可以通过指定主机名和端口号来使用 proxy 连接到 MySQL 服务器。
shell> mysql --host=localhost --port=4040
如果你的服务器要求提供鉴权信息 - 这些鉴权信息均会被 mysql-proxy 不作任何改变的透传 - 故你必须指定所需的鉴权信息:
shell> mysql --host=localhost --port=4040 \
--user=user_name --password=password
你同样可以连接到一个只读端口上(在该端口上会对 UPDATE 和 INSERT 类型的 query 进行过滤)。默认情况下,只读服务的主机名为默认,端口为 4042 ,但是你可以通过 --proxy-read-only-backend-addresses 命令行选项来改变其相应主机名和端口值。
更多使用命令行选项,以及如何组合使用 mysql-proxy 与 Lua 脚本的详细信息,请参阅 15.7.5, “Using MySQL Proxy”