这里使用mysqld_multi来安装多实例的方式 ,安装mysql略,可以采取yum直接安装方式。
由于资源有限,环境需求单台机器建立两个mysql实例来作为测试使用:
路径分别为
/var/lib/mysql3306
/var/lib/mysql3307
更改属主、组权限
[root@dev02 lib]#chown mysql.mysql /var/lib/mysql3306 -R
[root@dev02 lib]#chown mysql.mysql /var/lib/mysql3307 -R
初始化mysql:
[root@dev02 lib]#cd /var/lib/mysql3306/
[root@dev02 lib]#mysql_install_db --datadir=/var/lib/mysql3306 --user=mysql
[root@dev02 lib]#cd /var/lib/mysql3307/
[root@dev02 lib]#mysql_install_db --datadir=/var/lib/mysql3307 --user=mysql
创建多实例的配置文件,可以在任何路径下,只要后面启动时指定此配置文件路径即可。
注意两实例的socket,port,datadir,pid的配置要区别开.
[root@dev02 lib]# more /var/lib/my_multi.cnf
[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = root
[mysqld1]
socket = /tmp/mysql_3306.sock
port = 3306
pid-file = /var/lib/mysql3306/mysql3306.pid
datadir = /var/lib/mysql3306
log = /var/lib/mysql3306/mysql3306.log
user = mysql
#----------------parameter optimization---------- 参数优化部分可忽略
symbolic-links=0
max_allowed_packet=50M
character-set-server = utf8
#lower_case_table_names=1
general_log=/var/log/codfei.log
skip-external-locking
#skip-name-resolve
tmp_table_size = 256M
sql-mode= STRICT_TRANS_TABLES
default-storage-engine=INNODB
#query_cache_size = 16M
sort_buffer_size = 16M
read_buffer_size = 16M
table_cache = 512
ignore_builtin_innodb
plugin_dir=/usr/lib64/mysql/plugin
plugin_load=innodb=ha_innodb_plugin.so
;innodb_trx=ha_innodb_plugin.so
;innodb_locks=ha_innodb_plugin.so
;innodb_lock_waits=ha_innodb_plugin.so
;innodb_cmp=ha_innodb_plugin.so
;innodb_cmp_reset=ha_innodb_plugin.so
;innodb_cmpmem=ha_innodb_plugin.so
;innodb_cmpmem_reset=ha_innodb_plugin.so
max_connections = 1024
slow_query_log = /var/log/mysqlslowqueries.log
long_query_time = 4
skip-host-cache
max_allowed_packet = 160M
[mysqld2]
socket = /tmp/mysql_3307.sock
port = 3307
pid-file = /var/lib/mysql3306/mysql3307.pid
datadir = /var/lib/mysql3307
log = /var/lib/mysql3306/mysql3307.log
user = mysql
#----------------parameter optimization---------- 参数优化部分可忽略
symbolic-links=0
max_allowed_packet=50M
character-set-server = utf8
#lower_case_table_names=1
general_log=/var/log/codfei.log
skip-external-locking
#skip-name-resolve
tmp_table_size = 256M
sql-mode= STRICT_TRANS_TABLES
default-storage-engine=INNODB
#query_cache_size = 16M
sort_buffer_size = 16M
read_buffer_size = 16M
table_cache = 512
ignore_builtin_innodb
plugin_dir=/usr/lib64/mysql/plugin
plugin_load=innodb=ha_innodb_plugin.so
;innodb_trx=ha_innodb_plugin.so
;innodb_locks=ha_innodb_plugin.so
;innodb_lock_waits=ha_innodb_plugin.so
;innodb_cmp=ha_innodb_plugin.so
;innodb_cmp_reset=ha_innodb_plugin.so
;innodb_cmpmem=ha_innodb_plugin.so
;innodb_cmpmem_reset=ha_innodb_plugin.so
max_connections = 1024
slow_query_log = /var/log/mysqlslowqueries.log
long_query_time = 4
skip-host-cache
max_allowed_packet = 160M
[root@dev02 lib]#chown mysql.mysql /var/lib/my_multi.cnf -R
[root@dev02 lib]#/usr/bin/mysqld_multi --defaults-extra-file=/var/lib/my_multi.cnf start 1,2 启动多实例,建议加入到开机自启动中
[root@dev02 lib]# ps aux|grep mysqld 查看两实例已启动
root 3893 0.0 0.0 110272 1380 pts/0 S 07:05 0:00 /bin/sh /usr/bin/mysqld_safe --socket=/tmp/mysql_3306.sock --port=3306 --pid-file=/var/lib/mysql3306/mysql3306.pid --datadir=/var/lib/mysql3306 --log=/var/lib/mysql3306/mysql3306.log --user=mysql
root 3900 0.0 0.0 110272 1384 pts/0 S 07:05 0:00 /bin/sh /usr/bin/mysqld_safe --socket=/tmp/mysql_3307.sock --port=3307 --pid-file=/var/lib/mysql3306/mysql3307.pid --datadir=/var/lib/mysql3307 --log=/var/lib/mysql3306/mysql3307.log --user=mysql
mysql 4041 0.0 0.6 185856 25544 pts/0 Sl 07:05 0:01 /usr/sbin/mysqld --basedir=/ --datadir=/var/lib/mysql3306 --user=mysql --log=/var/lib/mysql3306/mysql3306.log --log-error=/var/lib/mysql3306/dev02.err --pid-file=/var/lib/mysql3306/mysql3306.pid --socket=/tmp/mysql_3306.sock --port=3306
mysql 4047 0.0 0.7 186100 30996 pts/0 Sl 07:05 0:01 /usr/sbin/mysqld --basedir=/ --datadir=/var/lib/mysql3307 --user=mysql --log=/var/lib/mysql3306/mysql3307.log --log-error=/var/lib/mysql3307/dev02.err --pid-file=/var/lib/mysql3306/mysql3307.pid --socket=/tmp/mysql_3307.sock --port=3307
root 4226 0.0 0.0 107428 896 pts/0 S+ 07:52 0:00 grep mysqld
分别连接实例更改密码:
[root@dev02 lib]#mysql -uroot -p -S /tmp/mysql_3306.sock 登录第一个mysql,空密码直接回车
mysql> set password=password("olymtech1");
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to user1@"%" Identified by "123456"; 创建普通用户并赋予权限
[root@dev02 lib]#mysql -uroot -p -S /tmp/mysql_3307.sock 登录第二个mysql,同上设置密码
停止各实例(试了下未成功,只能手动kill掉):
/usr/bin/mysqld_multi --defaults-extra-file=/var/lib/my_multi.cnf stop 1
/usr/bin/mysqld_multi --defaults-extra-file=/var/lib/my_multi.cnf stop 2
启动各实例:
/usr/bin/mysqld_multi --defaults-extra-file=/var/lib/my_multi.cnf start 1
/usr/bin/mysqld_multi --defaults-extra-file=/var/lib/my_multi.cnf start 2
补充多实例下mysql配置文件查找的顺序:
1. 首先读取/etc/my.cnf。
2. $datadir/my.cnf,在数据库目录下寻找此配置文件。
3. defaultfile=/path/my.cnf 通常写在命令行上,mysqld_safe defaultfile=/tmp/my.cnf &等执行。
4,~/my.cnf 当前用户下的配置文件。
参考文档:
http://sharkyan.blog.51cto.com/536264/271144/
http://www.cnblogs.com/acpp/archive/2010/08/09/1795466.html
http://www.linuxidc.com/Linux/2012-11/73615.htm