Home >Database >Mysql Tutorial >[MySQL 5.1 体验]MySQL 实例管理器 mysqlmanager 初试

[MySQL 5.1 体验]MySQL 实例管理器 mysqlmanager 初试

WBOY
WBOYOriginal
2016-06-07 15:26:031145browse

作/译者:叶金荣(Email: ),来源:http://imysql.cn MySQL实例管理器(IM)是通过TCP/IP端口运行的后台程序,用来监视和管理MySQL数据库服务器实例。MySQL实例管理器适合Unix-类操作系统和Windows。 可以在mysqld_safe脚本使用MySQL实例管理器来启动和停止My

作/译者:叶金荣(Email: [MySQL 5.1 体验]MySQL 实例管理器 mysqlmanager 初试),来源:http://imysql.cn

MySQL实例管理器(IM)是通过TCP/IP端口运行的后台程序,用来监视和管理MySQL数据库服务器实例。MySQL实例管理器适合Unix-类操作系统和Windows。

可以在mysqld_safe脚本使用MySQL实例管理器来启动和停止MySQL服务器,甚至可以从一个远程主机。MySQL实例管理器还执行mysqld_multi脚本的功能(和大多数语法)。

一、 mysqlmanager 配置文件
一个最常见的 mysqlmanager 配置文件如下:

[manager]<br>port	= 1999<br>socket	= /tmp/manager.sock<br>pid-file= /tmp/manager.pid<br>run-as-service	= true<br>monitoring-interval	= 10<br>default-mysqld-path	= /usr/local/mysql/bin/mysqld<br>password-file = /etc/mysqlmanager.passwd<br>log	=	/usr/local/mysql/bin/mysqld/mysqlmanager.log<br><br>[mysqld1]<br>basedir = /usr/local/mysql<br>datadir = /usr/local/mysql/data1<br>general-log	= true<br>log-error	= /usr/local/mysql/data1/error.log<br>pid-file	= /usr/local/mysql/data1/mysql.pid<br>port      = 13306<br>socket		= /tmp/mysql13306.sock<br><br>[mysqld2]<br>basedir = /usr/local/mysql<br>datadir = /usr/local/mysql/data2<br>general-log	= true<br>log-error	= /usr/local/mysql/data2/error.log<br>pid-file	= /usr/local/mysql/data2/mysql.pid<br>port      = 13307<br>socket		= /tmp/mysql13307.sock<br><br>[mysqld3]<br>basedir = /usr/local/mysql<br>datadir = /usr/local/mysql/data3<br>general-log	= true<br>log-error	= /usr/local/mysql/data3/error.log<br>pid-file	= /usr/local/mysql/data3/mysql.pid<br>port      = 13308<br>socket		= /tmp/mysql13308.sock<br>

首先,第一个区间是 [manager],它用于指定 mysqlmanager 程序启动时的选项。其中 run-as-service 选项是指 mysqlmanager 以后台守护进程方式运行, default-mysqld-path 选项则指定了 mysqld 程序所在的位置。 password-file 选项则指定了连接到 mysqlmanager 的用户密码文件所在位置。

接下来,后面的 [mysqld1], [mysqld2], [mysqld3] 区间则分别设定了3个 MySQL 运行实例。这些参数就是常见的设置了,在这里不再细说。

二、 mysqlmanager 用户管理
1、添加用户

[root@localhost]# /usr/local/mysql/bin/mysqlmanager --add-user --username=yejr<br>[20910/136318976] [07/06/19 11:03:01] [INFO] IM: started.<br>[20910/136318976] [07/06/19 11:03:01] [INFO] Loading config file 'my.cnf'...<br>[20910/136318976] [07/06/19 11:03:01] [INFO] Loading the password database...<br>[20910/136318976] [07/06/19 11:03:01] [INFO] The password database loaded successfully.<br>Enter password:<br>Re-type password:<br>[20910/136318976] [07/06/19 11:03:04] [INFO] IM: finished.<br>[root@localhost]# <br>[root@localhost]# cat /etc/mysqlmanager.passwd<br>yejr:*0E558D9FBD602CDA0C9F3F7A8BC5F4F53401CD7C<br>

2、修改密码

[root@localhost]# /usr/local/mysql/bin/mysqlmanager --edit-user --username=yejr<br>[20943/136318976] [07/06/19 11:05:01] [INFO] IM: started.<br>[20943/136318976] [07/06/19 11:05:01] [INFO] Loading config file 'my.cnf'...<br>[20943/136318976] [07/06/19 11:05:01] [INFO] Loading the password database...<br>[20943/136318976] [07/06/19 11:05:01] [INFO] Loaded user 'yejr'.<br>[20943/136318976] [07/06/19 11:05:01] [INFO] The password database loaded successfully.<br>Enter password:<br>Re-type password:<br>[20943/136318976] [07/06/19 11:05:05] [INFO] IM: finished.<br>[root@localhost]# <br>[root@localhost]# cat /etc/mysqlmanager.passwd<br>yejr:*9DB91006131E32B22135599033C6A9C196EC3C6B<br>

3、删除用户

[root@localhost]# /usr/local/mysql/bin/mysqlmanager --drop-user --username=yejr<br>[20967/136318976] [07/06/19 11:06:30] [INFO] IM: started.<br>[20967/136318976] [07/06/19 11:06:30] [INFO] Loading config file 'my.cnf'...<br>[20967/136318976] [07/06/19 11:06:30] [INFO] Loading the password database...<br>[20967/136318976] [07/06/19 11:06:30] [INFO] Loaded user 'yejr'.<br>[20967/136318976] [07/06/19 11:06:30] [INFO] The password database loaded successfully.<br>[20967/136318976] [07/06/19 11:06:30] [INFO] IM: finished.<br>

默认情况下,mysqlmanager 的密码文件是 /etc/mysqlmanager.passwd,如果你的密码文件不是放在这里,那么就需要自行指定,增加一个参数 --password-file=path_to_passwd_file,让 mysqlmanager 根据指定的位置去找到正确的密码文件。如:

[root@localhost]# /usr/local/mysql/bin/mysqlmanager --password-file=/usr/local/mysql/.mysqlmanager.passwd --add-user --username=yejr<br>

注意:修改或者删除用户后,只有重启 mysqlmanager 才能生效,而不是立刻生效。

三、 mysqlmanager 管理
1、mysqlmanager 启动

[root@localhost]# /usr/local/mysql/bin/mysqlmanager --defaults-file=/usr/local/mysql/my.cnf<br>[21032/136318976] [07/06/19 11:11:03] [INFO] IM: started.<br>[21032/136318976] [07/06/19 11:11:03] [INFO] Loading config file '/usr/local/mysql/my.cnf'...<br>[21032/136318976] [07/06/19 11:11:03] [INFO] Angel: started.<br>[21032/136318976] [07/06/19 11:11:03] [INFO] Angel: opening log file '/usr/local/mysql/bin/mysqld/mysqlmanager.log'...<br>[21032/136318976] [07/06/19 11:11:03] [INFO] Angel: daemonizing...<br>[21032/136318976] [07/06/19 11:11:03] [INFO] Angel: exiting from the original process...<br>[21032/136318976] [07/06/19 11:11:03] [INFO] IM: finished.<br>[21033/136318976] [07/06/19 11:11:03] [INFO] Angel: preparing standard streams.<br>

在启动 mysqlmanager 的同时,也会把它管理的所有 MySQL实例 全部启动。

[root@localhost]# mysql -uyejr -P1999 -hlocalhost -S/tmp/manager.sock -p<br>Enter password:<br>Welcome to the MySQL monitor.  Commands end with ; or /g.<br>Your MySQL connection id is 1 to server version: 1.0-beta<br><br>Type 'help;' or '/h' for help. Type '/c' to clear the buffer.<br><br>(yejr:localhost:)(none)> SHOW INSTANCES;<br>+---------------+--------+<br>| instance_name | state  |<br>+---------------+--------+<br>| mysqld1       | online |<br>| mysqld2       | online |<br>| mysqld3       | online |<br>+---------------+--------+<br>

2、mysqlmanager 状态查看

(yejr:localhost:)(none)> SHOW INSTANCE STATUS mysqld1/G<br>*************************** 1. row ***************************<br>    instance_name: mysqld1<br>            state: online<br>   version_number: 5.1.19<br>          version: 5.1.19-beta for unknown-freebsd6.0 on i386 (MySQL Community Server (GPL))<br>mysqld_compatible: no<br><br>(yejr:localhost:)(none)> SHOW INSTANCE OPTIONS mysqld1;<br>+---------------+----------------------------------------------+<br>| option_name   | value                                        |<br>+---------------+----------------------------------------------+<br>| instance_name | mysqld1                                      |<br>| basedir       | /usr/local/mysql                             |<br>| datadir       | /usr/local/mysql/data1                       |<br>| general-log   | true                                         |<br>| log-error     | /usr/local/mysql/data1/error.log             |<br>| pid-file      | /usr/local/mysql/data1/mysql.pid             |<br>| port          | 13306                                        |<br>| socket        | /tmp/mysql13306.sock                         |<br>+---------------+----------------------------------------------+<br><br>(yejr:localhost:)(none)> SHOW mysqld1 LOG FILES;<br>+-----------+----------------------------------+-----------+<br>| Logfile   | Path                             | File size |<br>+-----------+----------------------------------+-----------+<br>| ERROR LOG | /usr/local/mysql/data1/error.log | 2976      |<br>+-----------+----------------------------------+-----------+<br><br>(yejr:localhost:)(none)> SHOW mysqld13306 LOG ERROR 2976/G<br>*************************** 1. row ***************************<br>070619 11:17:57 [Warning] Server variable data_file_path of plugin InnoDB was forced to be read-only: string variable<br>without update_func and PLUGIN_VAR_MEMALLOC flag<br>070619 11:17:57 [Warning] Server variable data_home_dir of plugin InnoDB was forced to be read-only: string variable<br>without update_func and PLUGIN_VAR_MEMALLOC flag<br>070619 11:17:57 [Warning] Server variable flush_method of plugin InnoDB was forced to be read-only: string variable<br>without update_func and PLUGIN_VAR_MEMALLOC flag<br>070619 11:17:57 [Warning] Server variable log_arch_dir of plugin InnoDB was forced to be read-only: string variable<br>without update_func and PLUGIN_VAR_MEMALLOC flag<br>070619 11:17:57 [Warning] Server variable log_group_home_dir of plugin InnoDB was forced to be read-only: string<br>variable without update_func and PLUGIN_VAR_MEMALLOC flag<br>070619 11:17:57  InnoDB: Started; log sequence number 0 48402<br>070619 11:17:57 [Note] Event Scheduler: Loaded 0 events<br>070619 11:17:57 [Note] /usr/local/mysql/bin/mysqld: ready for connections.<br>Version: '5.1.19-beta'  socket: '/tmp/mysql13306.sock'  port: 13306  MySQL Community Server (GPL)<br>

更多的可操作命令可以参考手册。

3、mysqlmanager 管理
停止 MySQL实例:

(yejr:localhost:)(none)> STOP INSTANCE mysqld1;<br>Query OK, 0 rows affected (1.81 sec)<br><br>(yejr:localhost:)(none)> SHOW INSTANCES;<br>+---------------+---------+<br>| instance_name | state   |<br>+---------------+---------+<br>| mysqld1       | offline |<br>| mysqld2       | online  |<br>| mysqld3       | online  |<br>+---------------+---------+<br><br>(yejr:localhost:)(none)> START INSTANCE mysqld1;<br>Query OK, 0 rows affected (0.00 sec)<br>Instance started<br><br>(yejr:localhost:)(none)> SHOW INSTANCES;<br>+---------------+--------+<br>| instance_name | state  |<br>+---------------+--------+<br>| mysqld1       | online |<br>| mysqld2       | online |<br>| mysqld3       | online |<br>+---------------+--------+<br><br>(yejr:localhost:)(none)> STOP INSTANCE mysqld2;<br>Query OK, 0 rows affected (1.81 sec)<br><br>(yejr:localhost:)(none)> STOP INSTANCE mysqld3;<br>Query OK, 0 rows affected (1.81 sec)<br><br>(yejr:localhost:)(none)> SHOW INSTANCES;<br>+---------------+---------+<br>| instance_name | state   |<br>+---------------+---------+<br>| mysqld1       | offline |<br>| mysqld2       | offline |<br>| mysqld3       | offline |<br>+---------------+---------+<br><br>(yejr:localhost:)(none)> FLUSH INSTANCES;<br>Query OK, 0 rows affected (0.09 sec)<br><br>(yejr:localhost:)(none)> SHOW INSTANCES;<br>+---------------+--------+<br>| instance_name | state  |<br>+---------------+--------+<br>| mysqld1       | online |<br>| mysqld2       | online |<br>| mysqld3       | online |<br>+---------------+--------+<br>

在 MySQL实例 停止的状态下,还可以动态的修改端口等参数。
注意: FLUSH INSTANCES 语法会在 MySQL 5.2 以后不再使用。

总结:通过 mysqlmanager 我们就可以远程来管理 mysqld,包括重启,查看日志,设定系统参数等。而无需直接登录服务器或者通过 mysql 客户端登录服务器,也进一步保护了 MySQL 账户的安全,这在有较多数量的 MySQL 服务器引用环境中还是很有帮助的。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn