需求:
在一台服务器上通过源码编译安装一个版本为5.1.63版本MySQL数据库;
方案:
将所有配置文件与数据等均存放在/home/zhaoshuangshuang下。
在同一个MySQL中运行两个实例,一个绑定在端口3306,另一个绑定在端口3307。
其中,绑定3306端口的实例,数据存放在/home/zhaoshuangshuang/mysql/3306下;
绑定端口3307的实例,数据存放在/home/zhaoshuangshuang/mysql/3307下。
两个实例的存储引擎及字符集都按默认;
两个实例均采用相同的性能优化配置参数;
在编译安装时,将数据库的配置文件my.cnf以及数据目录等均指向到/home/zhaoshuangshuang/mysql目录;
通过mysqld_multi的方式来管理两个不同的实例,采用相同的配置文件共享性能优化配置参数;
在同一个配置文件中,利用[mysqld1]与[mysqld2]标签实现不同实例的差异化配置;
实施步骤:
1、编译安装MySQL数据库
我测试用的服务器已经安装过MySQL用户和用户组,分别是mysql。如果没有,请先创建用户和用户组。
$ tar -zxvf mysql-5.1.63.tar.gz
$ cd mysql-5.1.63
$ make
$ make install
这步执行完毕,就在/home/zhaoshuangshuang/mysql下,MySQL已经编译安装完成。
2、配置my.cnf以支持多实例
创建几个需要的目录: mkdir etc tmp run log binlogs 3306 3307
创建my.cnf配置文件:
$ vi my.cnf
[mysqld_multi]
mysqld = /home/zhaoshuangshuang/mysql/bin/mysqld_safe
mysqladmin = /home/zhaoshuangshuang/mysql/bin/mysqladmin
log = /home/zhaoshuangshuang/mysql/log/mysqld_multi.log
user = root
[mysqld1]
socket = /home/zhaoshuangshuang/mysql/run/mysqld.sock
port = 3306
pid-file = /home/zhaoshuangshuang/mysql/run/mysqld.pid
datadir = /home/zhaoshuangshuang/mysql/3306
[mysqld2]
socket = /home/zhaoshuangshuang/mysql/run/mysqld.sock2
port = 3307
pid-file = /home/zhaoshuangshuang/mysql/run/mysqld.pid2
datadir = /home/zhaoshuangshuang/mysql/3307
log-bin = /home/zhaoshuangshuang/mysql/binlogs/bin-log-mysqld2
log-bin-index = /home/zhaoshuangshuang/mysql/binlogs/bin-log-mysqld2.index
max_binlog_size = 1024m
[mysqld]
basedir = /home/zhaoshuangshuang/mysql
tmpdir = /home/zhaoshuangshuang/mysql/tmp
socket = /home/zhaoshuangshuang/mysql/run/mysqld.sock
port = 3306
pid-file = /home/zhaoshuangshuang/mysql/run/mysqld.pid
datadir = /home/zhaoshuangshuang/mysql/3306
max_connections = 200
3、初始化数据库
$ cd /home/zhaoshuangshuang/mysql-5.1.63
$ script/mysql_install_db --basedir=/home/zhaoshuangshuang/mysql --user=root --datadir=/home/zhaoshuangshuang/mysql/3306
$ script/mysql_install_db --basedir=/home/zhaoshuangshuang/mysql --user=root --datadir=/home/zhaoshuangshuang/mysql/3307
4、创建mysqld_multi脚本
$ cd /home/zhaoshuangshuang/mysql/
$ cp ../mysql-5.1.63/support-files/mysqld_multi.server ./init.d
$ vi init.d/mysqld_multi.server
#!/bin/sh
#
# A simple startup script for mysqld_multi by Tim Smith and Jani Tolonen.
# This script assumes that my.cnf file exists either in /etc/my.cnf or
# /root/.my.cnf and has groups [mysqld_multi] and [mysqldN]. See the
# mysqld_multi documentation for detailed instructions.
#
# This script can be used as /etc/init.d/mysql.server
#
# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 64 36
# description: A very fast and reliable SQL database engine.
#
# Version 1.0
#
basedir=/home/zhaoshuangshuang/mysql
bindir=/home/zhaoshuangshuang/mysql/bin
if test -x $bindir/mysqld_multi
then
mysqld_multi="$bindir/mysqld_multi";
else
echo "Can't execute $bindir/mysqld_multi from dir $basedir";
exit;
fi
case "$1" in
'start' )
"$mysqld_multi" start $2
;;
'stop' )
"$mysqld_multi" stop $2
;;
'report' )
"$mysqld_multi" report $2
;;
'restart' )
"$mysqld_multi" stop $2
"$mysqld_multi" start $2
;;
*)
echo "Usage: $0 {start|stop|report|restart}" >&2
;;
esac
设置mysqld_multi脚本的可执行性,chmod u+x mysqld_multi
5、管理数据库实例
启动:
$ /home/zhaoshuangshuang/mysql/init.d/mysqld_multi start 1,2
观看启动后:
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 31877/mysqld
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 31916/mysqld
关闭:
$ /home/zhaoshuangshuang/mysql/init.d/mysqld_multi stop 1,2
6、登陆数据库实例
登陆3306端口:
bin/mysql -P 3306 -u root -p -S /home/zhaoshuangshuang/mysql/run/mysqld.sock
登陆3307端口:
bin/mysql -P 3307 -u root -p -S /home/zhaoshuangshuang/mysql/run/mysqld.sock2
其中-S是选择的socket路径,在步骤2的my.cnf中设置。