目标:一台服务器开多个mysql实例

1、编译安装MySQL服务

到官网下周mysql源码包,然后通过脚本安装

#!/bin/bash
yum install -y ncurses-devel cmake gcc perl-Data-Dumper gcc-c++
useradd -s /sbin/nologin -r -M mysql
tar -xzf mysql-boost-5.7.13.tar.gz
cd mysql-5.7.13
cp support-files/my-default.cnf /etc/my.cnf
make clean
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock -DMYSQL_TCP_PORT=3306 -DWITH_DEBUG=0 -DENABLED_LOCAL_INFILE=1 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=./boost/
make -j 2
make install
mkdir /data/mysql -p
chown mysql:mysql /data/mysql/
chown mysql:mysql -R /usr/local/mysql/
/usr/local/mysql/bin/mysqld --initialize --user=mysql

初始化完成之后,/data/mysql这里是一个实例
注意:初始化完成会提供一个初始root密码给你的,要记得改

添加mysql环境变量

[root@k8s mysql-5.7.13]# echo 'PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
[root@k8s mysql-5.7.13]# source /etc/profile
安装完成

2、修改my.cnf配置

[mysqld_multi] 
mysqld    = /usr/local/mysql/bin/mysqld  
mysqladmin = /usr/local/mysql/bin/mysqladmin
log        = /tmp/mysql_multi.log 

#加入以上配置

#第一个实例,也就是刚安装完初始化的那个实例
[mysqld1]
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
server_id = 101
socket = /tmp/mysqld.sock
user = mysql 

#gtid配置,可选
gtid_mode=on
enforce_gtid_consistency=on
log-slave-updates=on
log-bin=mysql-bin
binlog_format=mixed

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

#第二个实例
[mysqld2]

basedir = /usr/local/mysql
datadir = /data/3307
port = 3307
server_id = 102
socket = /tmp/mysqld2.sock
user = mysql

#gtid配置,可选
gtid_mode=on
enforce_gtid_consistency=on
log-slave-updates=on
log-bin=mysql-bin
binlog_format=mixed

#从库设置只读
read-only

3、初始化另一个实例

指定data目录,执行命令初始化

/usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data/3307
初始化完成会提供一个初始root密码给你的,要记得改

4、启动实例

启动实例,通过mysqld_multi 启动
[root@k8s mysql-5.7.13]# mysqld_multi start

查看多实例状态

[root@k8s mysql-5.7.13]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running

启动完成