mysql多实例三种方式之我见
【共basedir,共my.cnf -- 不常用】
配置说明:首先安装好一个mysql,然后修改配置文件my.cnf,再备份出一份数据目录data和日志目录logs,最后启动mysql,记住这里要指定配置文件(--defaults-file=$path/my.cnf),具体操作简要如下:
P1. 首先创建一个单mysql实例
groupadd mysql #添加分组
useradd -g mysql -d /usr/local/mysql -s/sbin/nologin mysql #添加用户
tar zxvf mysql.tar.gz
mv mysql /usr/local/
cd /usr/local/mysql
chown -R mysql.mysql mysql #修改权限(相当于chown -R mysql . chgrp -R mysql.)
修改my.cnf配置文件(见附录)
mkdir -p /u1/mysql/data #创建数据存放库
mkdir -p /u1/mysql/logs #创建日志库
chown -R mysql.mysql /u1/mysql/data #设置权限
chown -R mysql.mysql /u1/mysql/logs #下面是初始化数据库
./scripts/mysql_install_db--user=mysql \
--defaults-file=/usr/local/mysql/my.cnf
cp support-files/my-medium.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
/usr/local/mysql/bin/mysqld--defaults-file=/usr/local/mysql/my.cnf &
vim /etc/profile #设置环境变量,添加如下内容
export PATH=$PATH:/usr/local/gymysql/bin/
mysql #登录数据库(正确登录,表示安装完成)
在/u1/mysql目录下存在以下两个文件:
drwxrwxr-x 7 mysql mysql 4096 8月 1517:31 data
drwxrwxr-x 2 mysql mysql 4096 8月 1517:31 logs
P2.修改配置文件my.cnf,然后复制出data和logs目录
cp -rp data data2
cp -rp logs logs2
这样在/u1/mysql目录下文件如下:
drwxrwxr-x 7 mysql mysql 4096 8月 1517:31 data
drwxrwxr-x 7 mysql mysql 4096 8月 1517:35 data2
drwxrwxr-x 2 mysql mysql 4096 8月 1517:31 logs
drwxrwxr-x 2 mysql mysql 4096 8月 1517:35 logs2
该实例中的参考配置文件如下:
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld
mysqladmin =/usr/local/mysql/bin/mysqladmin
[mysqld1]
connect_timeout = 20
basedir = /usr/local/mysql
datadir = /u1/mysql/data
user = mysql
port = 3306
socket = /tmp/mysql.sock
pid-file = mysql.pid
loose-character-set-server = utf8
back_log = 500
event_scheduler = Off
skip-name-resolve
tmpdir = /usr/local/mysql/tmp
binlog_format = mixed
log-bin = /u1/mysql/logs/mysql-bin
max_binlog_size = 128M
log_error = error.log
innodb_data_home_dir = /u1/mysql/data
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_log_group_home_dir = /u1/mysql/logs
[mysqld2]
connect_timeout = 20
basedir = /usr/local/mysql
datadir = /u1/mysql/data2
user = mysql
port = 3308
socket = /tmp/mysql3308.sock
pid-file = mysql.pid2
loose-character-set-server = utf8
back_log = 500
event_scheduler = Off
skip-name-resolve
tmpdir = /usr/local/mysql/tmp
binlog_format = mixed
log-bin = /u1/mysql/logs2/mysql-bin
max_binlog_size = 128M
log_error = error.log
innodb_data_home_dir = /u1/mysql/data2
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_log_group_home_dir = /u1/mysql/logs2
[mysqldump]
quick
max_allowed_packet = 128M
[mysql]
no-auto-rehash
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 28192
P3. 启动与关闭
启动:
/usr/local/mysql/bin/mysqld_multi--defaults-file=/usr/local/mysql/my.cnf start 1-2
或者:
/usr/local/mysql/bin/mysqld_multi--defaults-file=/usr/local/mysql/my.cnf start 1,2
查看:
[root@www mysql]# netstat -anpl| grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 16485/mysqld
[root@www mysql]# netstat -anpl| grep 3308
tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 16571/mysqld
unix 2 [ ACC ] STREAM LISTENING 351801 16571/mysqld /tmp/mysql3308.sock
关闭:
mysqladmin -S /tmp/mysql.sockshutdown
mysqladmin -S /tmp/mysql3308.sockshutdown
如果只想启动端口3308的mysql实例:
/usr/local/mysql/bin/mysqld_multi--defaults-file=/usr/local/mysql/my.cnf start 2
【共basedir,不共my.cnf -- 常用】***
P4. 先创建一个目录,然后再创建一个配置文件和tmp目录
[client]
port = 3308
socket = /tmp/mysql3308.sock
character-set-server = utf8
[mysqld]
connect_timeout = 20
basedir = /usr/local/mysql
datadir = /u1/mysql3308/data
user = mysql
port = 3308
socket = /tmp/mysql3308.sock
pid-file = mysql.pid
loose-character-set-server = utf8
back_log = 500
event_scheduler = Off
skip-name-resolve
tmpdir = /usr/local/mysql3308/tmp
binlog_format = mixed
log-bin = /u1/mysql3308/logs/mysql-bin
max_binlog_size = 128M
log_error = error.log
innodb_data_home_dir = /u1/mysql3308/data
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_log_group_home_dir = /u1/mysql3308/logs
[mysqldump]
quick
max_allowed_packet = 128M
[mysql]
no-auto-rehash
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 28192
P5. 初始化mysql,这里举例说明
[root@www mysql]#/usr/local/mysql/scripts/mysql_install_db --user=mysql --defaults-file=/usr/local/mysql3308/my.cnf--basedir=/usr/local/mysql
[root@www mysql]# cpsupport-files/mysql.server /etc/init.d/mysqld
cp:是否覆盖"/etc/init.d/mysqld"? y
[root@www mysql]# cpsupport-files/my-medium.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
P6. 启动
[root@www mysql]# /usr/local/mysql/bin/mysqld--defaults-file=/usr/local/mysql3308/my.cnf &
【不共basedir,不共my.cnf -- 最简单方式】
这里省略,在P1的基础上很容易实现,只需修改少量参数就行,尤其注意端口。
综上所述:第一种方式配置较复杂,由于是共享配置文件,管理维护起来较麻烦,不推荐使用。
第二种方式配置较简单,逻辑也清晰,管理维护很方便,也是当前企业中常用的方式之一,部署需要有一定经验,不适合于新手。
第三种方式简单,就相当于double一份,管理维护也很简单,新手部署起来简单快捷,易于操作。