安装并配置多实例mysql数据库

一、下载介质
http://mirrors.sohu.com/mysql/
http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.40.tar.gz
http://dev.mysql.com/downloads/mysql/
http://ftp.ntu.edu.tw/MySQL/Downloads/

二、安装mysql需要的依赖包和编译软件

[ ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[ ~]# uname -r
3.10.0-957.el7.x86_64
[ ~]# uname -m
x86_64

安装依赖包
[ yum.repos.d]# yum install ncurses-devel libaio-devel cmake gcc gcc-c++ glibc openssl-devel bison-devel autoconf -y
[ yum.repos.d]# rpm -qa curses-devel libaio-devel

安装编译mysql需要的软件
[ ~]# yum install cmake -y
[ ~]# rpm -qa cmake

三、安装mysql

1、建立mysql用户账号
[ ~]# useradd -s /sbin/nologin -M mysql
[ ~]# id mysql
uid=1004(mysql) gid=1007(mysql) 组=1007(mysql)

2、获取mysql软件包
http://mirrors.sohu.com/mysql/

mysql-5.6.40.tar.gz
mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz

3、采用编译方式安装mysql
第一步,下载mysql软件包,命令及操作如下:
[ ~]# mkdir -p /home/oldboy/tools
[ ~]# mkdir /application
[ ~]# cd /home/oldboy/tools
[ ~]# wget -q http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.40.tar.gz

[ tools]# ll -h
总用量 344M
-rw-r--r--. 1 root root 314M 4月 13 14:52 mysql-5.6.40-linux-glibc2.12-x86_64.tar.gz
-rw-r--r--. 1 root root 31M 4月 13 14:41 mysql-5.6.40.tar.gz

第二步,解压并配置mysql,命令及操作如下:
[ tools]# tar xf mysql-5.6.40.tar.gz
[ tools]# cd mysql-5.6.40/
[ mysql-5.6.40]# cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.40 \
-DMYSQL_DATADIR=/application/mysql-5.6.40/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.40/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_SSL=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0

参数说明:
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.40 \ #指定安装目录
-DMYSQL_DATADIR=/application/mysql-5.6.40/data \ #指定数据data目录
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.40/mysql.sock \ #指定sock路径
-DDEFAULT_CHARSET=utf8 \ #指定默认字符集
-DDEFAULT_COLLATION=utf8_general_ci \ #指定默认校验规则
-DWITH_EXTRA_CHARSETS=all \ #指定扩展支持的字符集
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ #启用innodb存储引擎
-DWITH_FEDERATED_STORAGE_ENGINE=1 \ #启用Federated存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ #启用BLACKHOLE引擎
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \ #禁用EXAMPLE_STORAGE_ENGINE
-DWITH_ZLIB=bundled \ #启用zlib支持
-DWITH_SSL=bundled \ #启用ssl库支持
-DENABLED_LOCAL_INFILE=1 \ #启用加载本地数据
-DWITH_EMBEDDED_SERVER=1 \ #嵌入式服务器
-DENABLE_DOWNLOADS=1 \ #编译时允许自主下载相关文件
-DWITH_DEBUG=0 #禁用调试模式
(参考:https://www.cnblogs.com/ssgeek/p/10489078.html)

第三步,编译并安装mysql,命令及操作如下:
[ mysql-5.6.40]# make && make install

第四步,为mysql安装路径设置不带版本号的软链接/application/mysql,命令及操作如下:
[ mysql-5.6.40]# ln -s /application/mysql-5.6.40/ /application/mysql

四、创建mysql多实例的数据文件目录
[ mysql-5.6.40]# mkdir -p /data/{3306,3307}/data

五、创建mysql多实例配置文件

[ mysql-5.6.40]# ll support-files/*.cnf
-rw-r--r--. 1 root root 1126 4月 13 15:14 support-files/my-default.cnf
[ mysql-5.6.40]# mv /etc/my.cnf /etc/my.cnf.bak_0

[ mysql-5.6.40]# vi /data/3306/my.cnf
[client]
port = 3306
socket = /data/3306/mysql.sock

[mysqld]
user = mysql
port = 3306
socket = /data/3306/mysql.sock
basedir = /application/mysql
datadir = /data/3306/data
log_bin = /data/3306/mysql-bin
server-id = 6

[mysqld_safe]
log_error = /data/3306/oldboy_3306.err
pid_file = /data/3306/mysqld.pid

[ 3306]# cd /data/3306/
[ 3306]# touch oldboy_3306.err
[ 3306]# chown mysql.mysql oldboy_3306.err
[ 3306]# chmod u+x oldboy_3306.err

[ mysql-5.6.40]# vi /data/3307/my.cnf
[client]
port = 3307
socket = /data/3307/mysql.sock

[mysqld]
user = mysql
port = 3307
socket = /data/3307/mysql.sock
basedir = /application/mysql
datadir = /data/3307/data
log_bin = /data/3307/mysql-bin
server-id = 7

[mysqld_safe]
log_error = /data/3307/oldboy_3307.err
pid_file = /data/3307/mysqld.pid

[ 3307]# cd /data/3307/
[ 3307]# touch oldboy_3307.err
[ 3307]# chown mysql.mysql oldboy_3307.err
[ 3307]# chmod u+x oldboy_3307.err

六、创建mysql多实例的启动文件
[ mysql-5.6.40]# vim /data/3306/mysql

#!/bin/sh
##############################################################

this scripts is created by oldboy at 2017-03-09

site: http://www.oldboyedu.com

blog: http://oldboy.blog.51cto.com

##############################################################
#init
port=3306
mysql_user="root"
Cmdpath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
mysqld_pid_file_path=/data/3306/3306.pid

start(){
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
/bin/sh ${Cmdpath}/mysqld_safe --defaults-file=/data/${port}/my.cnf --pid-file=$mysqld_pid_file_path 2>&1 > /dev/null &
sleep 3
else
printf "MySQL is running...\n"
exit 1
fi
}
stop(){
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit 1
else
printf "Stoping MySQL...\n"
mysqld_pid=cat "$mysqld_pid_file_path"
if (kill -0 $mysqld_pid 2>/dev/null)
then
kill $mysqld_pid
sleep 2
fi
fi
}

restart(){
printf "Restarting MySQL...\n"
stop
sleep 2
start
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
printf "Usage: /data/${port}/mysql{start|stop|restart}\n"
esac

[ mysql-5.6.40]# vim /data/3307/mysql

#!/bin/sh
##############################################################

this scripts is created by oldboy at 2017-03-09

site: http://www.oldboyedu.com

blog: http://oldboy.blog.51cto.com

##############################################################
#init
port=3307
mysql_user="root"
Cmdpath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
mysqld_pid_file_path=/data/3307/3307.pid

start(){
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
/bin/sh ${Cmdpath}/mysqld_safe --defaults-file=/data/${port}/my.cnf --pid-file=$mysqld_pid_file_path 2>&1 > /dev/null &
sleep 3
else
printf "MySQL is running...\n"
exit 1
fi
}
stop(){
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit 1
else
printf "Stoping MySQL...\n"
mysqld_pid=cat "$mysqld_pid_file_path"
if (kill -0 $mysqld_pid 2>/dev/null)
then
kill $mysqld_pid
sleep 2
fi
fi
}

restart(){
printf "Restarting MySQL...\n"
stop
sleep 2
start
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
printf "Usage: /data/${port}/mysql{start|stop|restart}\n"
esac

mysqld_safe --defaults-file=/data/3306/my.cnf > /dev/null 2>&1 &
mysqladmin -u root -poldboy123 -S /data/3306/mysql.sock shutdown
mysqld_safe --defaults-file=/data/3307/my.cnf > /dev/null 2>&1 &
mysqladmin -u root -poldboy123 -S /data/3307/mysql.sock shutdown

七、配置mysql多实例的文件权限
[ ~]# chown -R mysql.mysql /data
[ ~]# find /data -name mysql|xargs ls -l
-rw-r--r--. 1 mysql mysql 1362 4月 14 11:27 /data/3306/mysql
-rw-r--r--. 1 mysql mysql 1362 4月 14 11:28 /data/3307/mysql

[ ~]# find /data -name mysql|xargs chmod 700
[ ~]# find /data -name mysql -exec ls -l {} \;

八、将mysql相关命令加入全局路径
1、确认mysql命令所在路径:
[ ~]# ll /application/mysql/bin/mysql
-rwxr-xr-x. 1 mysql mysql 10250744 4月 13 15:27 /application/mysql/bin/mysql

2、在path变量前增加/application/mysql/bin路径,并追加到/etc/profile文件中
[ ~]# echo ‘export PATH=/application/mysql/bin/:$PATH‘ >>/etc/profile
[ ~]# tail -l /etc/profile
[ ~]# source /etc/profile
[ ~]# echo $PATH
/application/mysql/bin/:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin ---/application/mysql/bin/ 务必放在PATH其他路径最前面
或者
ln -s /application/mysql/bin/* /usr/local/sbin/

九、初始化mysql多实例的数据库文件
[ ~]# cd /application/mysql/scripts
[ scripts]# ./mysql_install_db --defaults-file=/data/3306/my.cnf --basedir=/application/mysql/ --datadir=/data/3306/data --user=mysql
[ scripts]# ./mysql_install_db --defaults-file=/data/3307/my.cnf --basedir=/application/mysql/ --datadir=/data/3307/data --user=mysql
[ scripts]# tree /data

十、启动mysql多实例数据库
[ scripts]# mkdir -p /application/mysql-5.6.40/tmp
[ scripts]# chown -R mysql.mysql /application/mysql/
如果第三章安装了单实例,则需要先停止之前启动的数据库
[ ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!
[ ~]# chkconfig mysqld off
[ ~]# chkconfig --list mysqld

[ ~]# /data/3306/mysql start
[ ~]# /data/3307/mysql start
[ ~]# netstat -lntup| grep mysql

十一、配置及管理mysql多实例数据库
1、配置mysql多实例开机自动启动
[ ~]# echo "#mysql multi instances" >>/etc/rc.local
[ ~]# echo "/data/3306/mysql start" >>/etc/rc.local
[ ~]# echo "/data/3307/mysql start" >>/etc/rc.local
[ ~]# tail -3 /etc/rc.local
#mysql multi instances
/data/3306/mysql start
/data/3307/mysql start
[ ~]#

2、登录mysql测试
[ ~]# mysql -S /data/3306/mysql.sock
[ ~]# mysql -S /data/3307/mysql.sock

[ ~]# /data/3306/mysql stop
[ ~]# /data/3306/mysql start
[ ~]# /data/3307/mysql stop
[ ~]# /data/3307/mysql start

3、mysql安全配置
[ ~]# mysqladmin -u root -S /data/3306/mysql.sock password ‘oldboy123‘
[ ~]# mysqladmin -u root -S /data/3307/mysql.sock password ‘oldboy123‘

[ ~]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock
[ ~]# mysql -uroot -poldboy123 -S /data/3307/mysql.sock

修改密码:
[ ~]# mysqladmin -uroot -poldboy123 -S /data/3307/mysql.sock password oldboy456

4、如何在增加一个mysql的实例:
mkdir -p /data/3308/data
\cp /data/3306/my.cnf /data/3308
\cp /data/3306/mysql /data/3308
sed -i ‘s/3306/3308/g‘ /data/3308/mysql
chown -R mysql:mysql /data/3308
chmod 700 /data/3308/mysql
cd /application/mysql/scripts
./mysql_install_db --defaults-file=/data/3308/my.cnf --datadir=/data/3308/data --basedir=/application/mysql --user=mysql
chown -R mysql:mysql /data/3308
egrep "server-id|log-bin" /data/3308/my.cnf
/data/3308/mysql start
sleep 5
netstat -lnt|grep 3308

相关推荐