Mysql中的mysqld_multi命令,可用于在一台物理服务器运行多个Mysql服务
一、准备工作
1.MySQL源码安装
[root@localhost ~]# tar zxvf mysql-5.0.80.tar.gz
[root@localhost ~]# cd mysql-5.0.80
[root@localhost mysql-5.0.80]# ./configure --prefix=/usr/local/mysql
[root@localhost mysql-5.0.80]# make
[root@localhost mysql-5.0.80]# make install
[root@localhost ~]# cd mysql-5.0.80
[root@localhost mysql-5.0.80]# ./configure --prefix=/usr/local/mysql
[root@localhost mysql-5.0.80]# make
[root@localhost mysql-5.0.80]# make install
2.建立帐户
[root@localhost mysql-5.0.80]# adduser mysql
[root@localhost mysql-5.0.80]# chown -R mysql:root /usr/local/mysql
[root@localhost mysql-5.0.80]# chown -R mysql:root /usr/local/mysql
3.初始化授权表
[root@localhost mysql]# su mysql
[root@localhost mysql]# cd /usr/local/mysql
[root@localhost mysql]# ./bin/mysql_install_db
[root@localhost mysql]# cd /usr/local/mysql
[root@localhost mysql]# ./bin/mysql_install_db
二、多个MySQL服务
创建my.cnf
[root@localhost mysql]# touch /etc/my.cnf
[root@localhost mysql]#vi /etc/my.cnf
[root@localhost mysql]#vi /etc/my.cnf
my.cnf的内容如下
[mysqld_multi]
mysqld=/usr/local/mysql/bin/mysqld_safe
mysqladmin=/usr/local/mysql/bin/mysqladmin
user = mysql
password = d3.zone
[mysqld1]
port = 3306
socket = /tmp/mysql.sock1
pid-file=/usr/local/mysql/var1/db-app1.pid
log=/usr/local/mysql/var1/db-app.log
datadir = /usr/local/mysql/var1
user = mysql
[mysqld2]
port = 3307
socket = /tmp/mysql.sock2
pid-file = /usr/local/mysql/var2/db-app2.pid
datadir = /usr/local/mysql/var2
log=/usr/local/local/var2/db-app.log
user = mysql
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[isamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
mysqld=/usr/local/mysql/bin/mysqld_safe
mysqladmin=/usr/local/mysql/bin/mysqladmin
user = mysql
password = d3.zone
[mysqld1]
port = 3306
socket = /tmp/mysql.sock1
pid-file=/usr/local/mysql/var1/db-app1.pid
log=/usr/local/mysql/var1/db-app.log
datadir = /usr/local/mysql/var1
user = mysql
[mysqld2]
port = 3307
socket = /tmp/mysql.sock2
pid-file = /usr/local/mysql/var2/db-app2.pid
datadir = /usr/local/mysql/var2
log=/usr/local/local/var2/db-app.log
user = mysql
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[isamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
配置中不同mysql 服务建立各自文件夹和初始数据库等
[root@localhost mysql]# cd /usr/local/mysql
[root@localhost mysql]# mv var ./var1
[root@localhost mysql]# cp var1 ./var2 -R
[root@localhost mysql]# mv var ./var1
[root@localhost mysql]# cp var1 ./var2 -R
现在,可以通过mysqld_multi的参数来启动mysqld_multi
[root@localhost mysql]# /usr/local/mysql/bin/mysqld_multi --config-file=/etc/my.cnf start 1-2
用report参数查看运行状态
至此,我们的两个mysql服务就启动了,可以根据需要添加更多的mysql服务,这里我就不在啰嗦了。
在客户端访问通过mysqld_multi启动的mysql服务时,需要指定相应的mysql服务端口
在服务器本机上,还可以通过mysql的socket来指定相应mysql服务
[root@localhost mysql]# mysql --socket=/tmp/mysql.sock1 -uroot -p
转载于:https://blog.51cto.com/fdsazi/362561