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

。_饼干妹妹 提交于 2020-04-18 17:50:38

一、下载介质
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需要的依赖包和编译软件

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




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

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

三、安装mysql

1、建立mysql用户账号
[root@monitor01 ~]# useradd -s /sbin/nologin -M mysql
[root@monitor01 ~]# 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软件包,命令及操作如下:
[root@monitor01 ~]# mkdir -p /home/oldboy/tools
[root@monitor01 ~]# mkdir /application
[root@monitor01 ~]# cd /home/oldboy/tools
[root@monitor01 ~]# wget -q http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.40.tar.gz




[root@monitor01 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,命令及操作如下:
[root@monitor01 tools]# tar xf mysql-5.6.40.tar.gz
[root@monitor01 tools]# cd mysql-5.6.40/
[root@monitor01 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,命令及操作如下:
[root@monitor01 mysql-5.6.40]# make && make install

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

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

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

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

[root@monitor01 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

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


[root@monitor01 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

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


六、创建mysql多实例的启动文件
[root@monitor01 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











[root@monitor01 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多实例的文件权限
[root@monitor01 ~]# chown -R mysql.mysql /data
[root@monitor01 ~]# 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



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

八、将mysql相关命令加入全局路径
1、确认mysql命令所在路径:
[root@monitor01 ~]# 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文件中
[root@monitor01 ~]# echo 'export PATH=/application/mysql/bin/:$PATH' >>/etc/profile
[root@monitor01 ~]# tail -l /etc/profile
[root@monitor01 ~]# source /etc/profile
[root@monitor01 ~]# 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多实例的数据库文件
[root@monitor01 ~]# cd /application/mysql/scripts
[root@monitor01 scripts]# ./mysql_install_db --defaults-file=/data/3306/my.cnf --basedir=/application/mysql/ --datadir=/data/3306/data --user=mysql
[root@monitor01 scripts]# ./mysql_install_db --defaults-file=/data/3307/my.cnf --basedir=/application/mysql/ --datadir=/data/3307/data --user=mysql
[root@monitor01 scripts]# tree /data



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






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

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








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

[root@monitor01 ~]# /data/3306/mysql stop
[root@monitor01 ~]# /data/3306/mysql start
[root@monitor01 ~]# /data/3307/mysql stop
[root@monitor01 ~]# /data/3307/mysql start


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

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

修改密码:
[root@monitor01 ~]# 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












易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!