mysqld_multi 配置MySQL多实例

1、下载MySQL源码安装版本

到MySQL官网查找到相应版本下载,本文下载的是5.1版本

wget -c ftp://ftp.mirrorservice.org/sites/ftp.mysql.com/Downloads/MySQL-5.1/mysql-5.1.60.tar.gz

2、解压安装

tar -zxv -f mysql-5.1.60.tar.gz

./configure \
--prefix=/usr/local/m \
--enable-assembler \
--enable-local-infile \
--with-charset=utf8 \
--with-collation=utf8_general_ci \
--with-extra-charsets=none \
--with-openssl \
--with-pthread \
--with-unix-socket-path=/var/lib/mysql/mysql.sock \
--with-mysqld-user=mysql \
--with-mysqld-ldflags \
--with-client-ldflags \
--with-comment \
--with-big-tables \
--without-ndb-debug \
--without-docs \
--without-debug \
--without-bench

make && make install

3、添加用户和用户组

shell> groupadd mysql
shell> useradd -r -g mysql mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-5.1.60.tar.gz
shell> ln -s full-path-to-mysql-5.1.60 mysql
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
# Next command is optional
shell> cp support-files/my-medium.cnf /etc/my.cnf
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server

 

4、初始化数据目录

#把用到的工具添加到/usr/bin目录
ln -s /usr/local/mysql/bin/mysqld_multi /usr/bin/mysqld_multi
ln -s /usr/local/mysql/bin/mysql_install_db /usr/bin/mysql_install_db

#初始化四个数据目录
mysql_install_db --datadir=/usr/local/var/mysql1 --user=mysql
mysql_install_db --datadir=/usr/local/var/mysql2 --user=mysql
mysql_install_db --datadir=/usr/local/var/mysql3 --user=mysql
mysql_install_db --datadir=/usr/local/var/mysql4 --user=mysql

#修改属性
chown -R mysql /usr/local/var/mysql1 
chown -R mysql /usr/local/var/mysql2
chown -R mysql /usr/local/var/mysql3 
chown -R mysql /usr/local/var/mysql4

 

5、配置多实例启动脚本

从MySQL的源码中把复制到/etc/init.d/目录下

cp /usr/local/src/mysql-5.1.60/support-files/mysqld_multi.server /etc/init.d/mysqld_multi.server

#修改basedir和bindir为安装路径
basedir=/usr/local/mysql
bindir=/usr/local/mysql/bin

 

6、配置多实例数据库配置文件

用mysqld_multi工具查看该配置文件的模板方法,命令为:mysqld_multi –example

在/etc/目录下创建创建文件/etc/mysqld_multi.cnf,把mysqld_multi –example产生的文件粘进去,修改相应属性,如:mysqld,mysqladmin,socket,port,pid-file,datadir,user等。

[mysqld_multi]
mysqld     = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
#user       = mysql
#password   = my_password

[mysqld1]
socket     = /usr/local/var/mysql1/mysql1.sock
port       = 3306
pid-file   = /usr/local/var/mysql1/mysql1.pid
datadir    = /usr/local/var/mysql1
#language   = /usr/local/mysql/share/mysql/english
user       = mysql

[mysqld2]
socket     = /usr/local/var/mysql2/mysql2.sock
port       = 3307
pid-file   = /usr/local/var/mysql2/mysql2.pid
datadir    = /usr/local/var/mysql2
#language   = /usr/local/mysql/share/mysql/english
user       = mysql

[mysqld3]
socket     = /usr/local/var/mysql3/mysql3.sock
port       = 3308
pid-file   = /usr/local/var/mysql3/mysql3.pid
datadir    = /usr/local/var/mysql3
#language   = /usr/local/mysql/share/mysql/english
user       = mysql

[mysqld4]
socket     = /usr/local/var/mysql4/mysql4.sock
port       = 3309
pid-file   = /usr/local/var/mysql4/mysql4.pid
datadir    = /usr/local/var/mysql4
#language   = /usr/local/mysql/share/mysql/english
user       = mysql

注意:除了以上配置方法,还可以直接写到主配置文件中。如下:


[root @localhost ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#default-character-set=utf8
#init_connect='SET NAMES utf8'
server-id=1
log-bin
binlog-do-db=backup

[mysqld_multi]

[mysqld1]
socket = /var/lib/mysql1/mysql1.sock
port = 3307
pid-file = /var/lib/mysql1/mysql1.pid
datadir = /var/lib/mysql1
user = mysql

[mysqld2]
socket = /var/lib/mysql2/mysql2.sock
port = 3308
pid-file = /var/lib/mysql2/mysql2.pid
datadir = /var/lib/mysql2
user = mysql

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

#[client]
#default-character-set=utf8

7、启动多实例数据库

将/usr/local/mysql/bin加到$PATH环境变量里
export PATH=/usr/local/mysql/bin:$PATH

#查看数据库状态
mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf report
#结果都为没有运行
Reporting MySQL servers
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is not running
MySQL server from group: mysqld3 is not running
MySQL server from group: mysqld4 is not running

#启动
mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf start
#结果为
Reporting MySQL servers
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is not running
MySQL server from group: mysqld3 is not running
MySQL server from group: mysqld4 is not running

#启动具体某一个实例可在start、stop后面加上具体数据1,2,3

8、查看运行结果

#查看相应端口是否已经被监听
netstat -tunlp

#查看是否有活动进程
ps -aux|grep mysql

9、登录相应数据库

#进入端口为3306的数据库
mysql -uroot -p -h127.0.0.1 -P3306

#通过sock文件登录
mysql -uroot -p -S /usr/local/var/mysql1/mysql1.sock

 #查看socket文件
mysql> SHOW VARIABLES LIKE 'socket';

#查看pid文件
mysql> SHOW VARIABLES LIKE '%pid%';

mysqld_multi — Manage Multiple MySQL Servers

发表评论