一、MySQL多实例简介
MySQL多实例,简单地说,就是在一台服务器上同时开启多个不同的服务端口(如:3306、3307),同时运行多个MySQL服务进程,这些服务进程通过不同的socket监听来自不同的端口来提供服务;多实例不仅节省物理主机成本,还有效提升了单台物理主机的CPU、磁盘I/O使用效率,而且还可以在多实例之间做部署数据库HA方案。
随着实例数量的增加,就面临统一管理问题,这样我们就需要用MySQL自带的管理程序 mysqld_multi 来进行管理...
二、MySQL启动流程
mysqld_multi #多实例管理程序
mysqld #以前老版本的MySQL最主要的启动方式,里面有很多参数;现在使用多实例就需要用新的 mysql_safe 来启动mysql
mysql_safe #实则还是调用mysqld,并且会读取mysqld中的my.cnf配置参数来启动mysql,mysql_safe本身也有很多参数,但是这些参数会优先于my.cnf
my.cnf #mysql的配置文件
my.sock #mysql创建的sock文件,开启、停止、登陆和管理mysql都是通过这个接口文件
三、接下来,利用 mysqld_multi 配置一个多实例...
安装环境:CentOS 6.7 _64
下载mysql-5.5.32.tar.gz编译安装包
yum install -y ncurses-devel libaio-devel
yum install -y cmake
tar zxf mysql-5.5.32.tar.gz
cd mysql-5.5.32
cmake .-DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.32 \
-DMYSQL_DATADIR=/application/mysql-5.5.32/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.5.32/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FAST_MUTEXES=1 \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0
make && make install
ln -s /application/mysql-5.5.32/ /application/mysql
mkdir -p /data/{3306,3307,3308}/data #创建单独实例数据文件夹
useradd mysql -s /sbin/nologin -M
chown -R mysql.mysql /data
cp /application/mysql/bin/* /usr/local/sbin/ #添加环境变量
cd /application/mysql/scripts/ #初始化数据库
./mysql_install_db --basedir=/application/mysql/ --datadir=/data/3306/data/ --user=mysql
./mysql_install_db --basedir=/application/mysql/ --datadir=/data/3307/data/ --user=mysql
./mysql_install_db --basedir=/application/mysql/ --datadir=/data/3308/data/ --user=mysql
/bin/cp /application/mysql/support-files/my-medium.cnf /etc/my.cnf #拷贝my.cnf配置文件
cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld.server #拷贝启动文件
cp /application/mysql/support-files/mysqld_multi.server /etc/init.d/mysqld_multi.server
#拷贝mysqld_multi程序文件
#修改mysqld_multi.server路径配置
sed -i 's#basedir=/usr/local/mysql#basedir=/application/mysql#g' /etc/init.d/mysqld_multi.server
sed -i 's#bindir=/usr/local/mysql/bin#bindir=/application/mysql/bin#g' /etc/init.d/mysqld_multi.server
#添加mysqld_multi用到的/etc/mysqld_multi.cnf配置文件
#这个模板文件可以用命令mysqld_multi --example导出来
[root@lb01 scripts]# vim /etc/mysqld_multi.cnf
[mysqld_multi]
mysqld = /application/mysql-5.5.32/bin/mysqld_safe
mysqladmin = /application/mysql-5.5.32/bin/mysqladmin
#user = multi_admin
#password = my_password
[mysqld2]
socket = /data/3306/mysql.sock
port = 3306
pid-file = /data/3306/mysql.pid
datadir = /data/3306/data
#language = /application/mysql-5.5.32/share/mysql/english
user = mysql
[mysqld3]
socket = /data/3307/mysql.sock
port = 3307
pid-file = /data/3307/mysql.pid
datadir = /data/3307/data
#language = /application/mysql-5.5.32/share/mysql/english
user = mysql
[mysqld4]
socket = /data/3308/mysql.sock
port = 3308
pid-file = /data/3308/mysql.pid
datadir = /data/3308/data
#language = /application/mysql-5.5.32/share/mysql/english
user = mysql