MySQL 5.6版本二进制包多实例安装

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL 5.6版本二进制包多实例安装相关的知识,希望对你有一定的参考价值。

一、环境介绍

(1)系统环境介绍:
[[email protected] ~]# uname -a
Linux linux-node2 3.10.0-693.5.2.el7.x86_64 #1 SMP Fri Oct 20 20:32:50 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[[email protected] ~]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 
(2)mysql 5.6.x版本下载
下载地址:http://mirrors.sohu.com/mysql/MySQL-5.6/

二、MySQL安装

(1)解压

[[email protected] ~]# tar -zxvf mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz -C /usr/local/mysql-5.6.12

(2)创建软链接

[[email protected] ~]# ln -sv /usr/local/mysql-5.6.12 /usr/local/mysql

(3)创建mysql 用户

[[email protected] ~]# useradd -M -s /sbin/nologin mysql

(4)创建目录结构和授权

[[email protected] ~]# mkdir /data/{3306,3307}/ -p
[[email protected] ~]# chown -R mysql.mysql /data

(5)初始化数据库

[[email protected] ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/3306/data
[[email protected] ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/3307/data

(6)修改配置

[[email protected] mysql]# vim /data/3306/my.cnf 
[client]    
port        = 3306     
socket      = /data/3306/mysql3306.sock


[mysqld]    
port        = 3306     
socket      = /data/3306/mysql3306.sock     
datadir     = /data/3306/data


[[email protected] mysql]# vim /data/3307/my.cnf 
[client]    
port        = 3307     
socket      = /data/3307/mysql3307.sock


[mysqld]    
port        = 3307     
socket      = /data/3307/mysql3307.sock     
datadir     = /data/3307/data

(7)启动多实例

[[email protected] mysql]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3306/my.cnf &
[[email protected] mysql]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/3307/my.cnf &
[[email protected] mysql]# netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1865/nginx: worker  
tcp        0      0 0.0.0.0:8081            0.0.0.0:*               LISTEN      1865/nginx: worker  
tcp        0      0 0.0.0.0:8082            0.0.0.0:*               LISTEN      1865/nginx: worker  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      866/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2235/master         
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      22597/php-fpm: mast 
tcp6       0      0 :::3307                 :::*                    LISTEN      11221/mysqld        
tcp6       0      0 :::22                   :::*                    LISTEN      866/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      2235/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      10903/mysqld        

(8)修改密码

[[email protected] ~]# mysqladmin -u root password "123456" -S /data/3306/mysql3306.sock 
[[email protected] ~]# mysqladmin -u root password "654321" -S /data/3307/mysql3307.sock

(9)登陆数据库

[[email protected] ~]# mysql -uroot -p -S /data/3306/mysql3306.sock 
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.12 MySQL Community Server (GPL)


Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.


Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.


MySQL [(none)]> quit;
Bye
[[email protected] ~]# mysql -uroot -p -S /data/3307/mysql3307.sock 
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.12 MySQL Community Server (GPL)


Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.


Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.


MySQL [(none)]> quit;
Bye

三、MySQL多实例启动脚本

[[email protected] ~]# cat /data/3306/mysql
#!/bin/sh
#init
port=3306
mysql_user="root"
mysql_pwd="123456"
CmdPath="/usr/local/mysql/bin"
mysql_sock="/data/${port}/mysql3306.sock"
#startup function
function_start_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 >/dev/null &
else
printf "MySQL is running...\n"
exit
fi
}
#stop function
function_stop_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
printf "Stoping MySQL...\n"
${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql3306.sock shutdown
fi
}
#restart function
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 2
function_start_mysql
}
case $1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
esac
[[email protected] ~]# cat /data/3307/mysql
#!/bin/sh
#init
port=3307
mysql_user="root"
mysql_pwd="654321"
CmdPath="/usr/local/mysql/bin"
mysql_sock="/data/${port}/mysql3307.sock"
#startup function
function_start_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 >/dev/null &
else
printf "MySQL is running...\n"
exit
fi
}
#stop function
function_stop_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
printf "Stoping MySQL...\n"
${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql3307.sock shutdown
fi
}
#restart function
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 2
function_start_mysql
}
case $1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
esac

以上是关于MySQL 5.6版本二进制包多实例安装的主要内容,如果未能解决你的问题,请参考以下文章

五.MySQL安装:二进制离线安装脚本(5.6版本)

四.MySQL安装:二进制安装(5.6版本)

Mysql 5.6版本二进制安装

PHP搭建(windows64+apache2.4.7+mysql-5.6+php5.5)

Linux(CentOS 6.5) 下安装MySql 5.7.18 二进制版本粗浅攻略

MYSQL 二进制安装