MySQL5.7多实例安装及开机启动配置(亲测)

database

安装环境:

CentOS版本:CentOS7.6.1810

MySQL版本:5.7.9

以前一些很low的方法是:

解压两个mysql,分别放到不同文件夹。

其实在mysql中已经考虑到了多实例安装的情况。也有相应的脚本命令的支持。

现在安装两个mysql 一个3307,3308

新建 /etc/my.cnf 配置如下:

[mysqld_multi]

mysqld = /usr/local/mysql/bin/mysqld_safe

mysqladmin = /usr/local/mysql/bin/mysqladmin

user = root #登陆数据库用户

password = root1234% #登陆数据库密码,用于关闭数据库,两台数据库密码设置一样

[mysqld1]

user = mysql

port = 3307

socket = /tmp/mysql3307.sock

pid-file = /usr/local/mysql/data3307/mysql.pid

datadir = /usr/local/mysql/data3307

language = /usr/local/mysql/share/mysql/english

log_error = 3307_error.log

[mysqld2]

user = mysql

port = 3308

socket = /tmp/mysql3308.sock

pid-file = /usr/local/mysql/data3308/mysql.pid

datadir = /usr/local/mysql/data3308

language = /usr/local/mysql/share/mysql/english

log_error = 3308_error.log

创建两个数据目录:

mkdir /usr/local/mysql/data3307

mkdir /usr/local/mysql/data3308

chown mysql.mysql /data{3307…3308}

数据库初始化:

mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data3307

mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data3308

cp /usr/local/mysql/support-files/mysqld_multi.server /etc/init.d/mysqld_multid

查看状态

mysqld_multi report

这个时候发现还需要perl的环境,安装

yum -y install perl perl-devel

在运行,发现已经有实例了

mysqld_multi report

mysqld_multi start

启动,分别修改密码,允许远程连接

mysql -u root -S /tmp/mysql.sock1 -p -P3307

mysql -u root -S /tmp/mysql.sock2 -p -P3308

set password = ‘root1234%’;

GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY ‘root12345’;

flush privileges;

检查多实例运行状态:

mysqld_multi report

此时多实例安装成功。。。

开机启动配置:

一般情况下用命令:

chkconfig mysqld_multid on

但是该命令在多实例场景下是不起作用的。

Mysql多实例配置成功后,想让配置成开机自启。

首先看一下Linux启动的知识点,顺序如下:

1. 加载内核

2. 执行init程序

3. /etc/rc.d/rc.sysinit # 由init执行的第一个脚本

4. /etc/rc.d/rc $RUNLEVEL # $RUNLEVEL为缺省的运行模式,linux 共有7种运行模式,后续会介绍。

5. /etc/rc.d/rc.local # 相应级别服务启动之后、在执行该文件(其实也可以把需要执行的命令写到该文件中)

6. /sbin/mingetty # 等待用户登录

这里方法有好几种,我们选择第五条,即将执行命令加到 /etc/rc.d/rc.local 中,命令如下。

首先,先创建自动启动mysqlauto.sh文件:

#!/bin/bash

mysql_port=$2

mysql_username="root"

mysql_password="root1234%"

function_start_mysql()

{

printf "Starting MySQL...

"

# 这里建议用绝对路径,不然会找不到mysqld_multi命令

/usr/local/mysql/bin/mysqld_multi --defaults-file=/etc/my.cnf start 1,2

}

function_stop_mysql()

{

printf "Stoping MySQL...

"

# 这里建议用绝对路径,不然会找不到mysqladmin命令

/usr/local/mysql/bin/mysqladmin -u root -proot1234% -S /tmp/mysql${mysql_port}.sock shutdown

# /usr/local/mysql/bin/mysqladmin -u root -proot1234% -S /tmp/mysql3308.sock shutdown

}

function_restart_mysql()

{

printf "Restarting MySQL...

"

function_stop_mysql

function_start_mysql

}

function_kill_mysql()

{

kill -9 $(ps -ef | grep "bin/mysqld_safe" | grep ${mysql_port} | awk "{printf $2}")

kill -9 $(ps -ef | grep "libexec/mysqld" | grep ${mysql_port} | awk "{printf $2}")

}

case $1 in

start)

function_start_mysql;;

stop)

function_stop_mysql;;

kill)

function_kill_mysql;;

restart)

function_stop_mysql

function_start_mysql;;

*)

esac

将mysqlauto.sh文件放在/opt/script目录中

如果脚本报错:

sed -i ‘s/$//’ mysqlauto.sh

其次,系统在执行rc.local文件后才会加载环境变量,所以需要将rc.local的内容改为:

注:

#系统在执行rc.local文件后才会加载环境变量,故有下面配置:

MYSQL_HOME=/usr/local/mysql

PATH=MYSQLHOME/bin:MYSQL

HOME/bin:PATH

source /root/.bash_profile

#分别启动3307和3308端口

/opt/script/mysqlauto.sh start 3307

/opt/script/mysqlauto.sh start 3308

重启机器:

通过netstat命令查看端口状态如下,则配置成功。

若有错不要慌,可以通过命令systemctl list-units –type=service查看报错信息,示例如下:

[root@localhost ~]# systemctl status rc-local.service

● rc-local.service - /etc/rc.d/rc.local Compatibility

Loaded: loaded (/usr/lib/systemd/system/rc-local.service; static; vendor preset: disabled)

Active: failed (Result: exit-code) since 四 2019-06-20 13:20:45 CST; 2min 39s ago

Process: 6554 ExecStart=/etc/rc.d/rc.local start (code=exited, status=1/FAILURE)

6月 20 13:20:45 localhost.localdomain rc.local[6554]: in your path. The command is available from the latest

6月 20 13:20:45 localhost.localdomain rc.local[6554]: MySQL distribution.

6月 20 13:20:45 localhost.localdomain rc.local[6554]: ABORT: Can"t find command "my_print_defaults".

6月 20 13:20:45 localhost.localdomain rc.local[6554]: This command is available from the latest MySQL

6月 20 13:20:45 localhost.localdomain rc.local[6554]: distribution. Please make sure you have the command

6月 20 13:20:45 localhost.localdomain rc.local[6554]: in your PATH.

6月 20 13:20:45 localhost.localdomain systemd[1]: rc-local.service: control process exited, code=exited status=1

6月 20 13:20:45 localhost.localdomain systemd[1]: Failed to start /etc/rc.d/rc.local Compatibility.

6月 20 13:20:45 localhost.localdomain systemd[1]: Unit rc-local.service entered failed state.

6月 20 13:20:45 localhost.localdomain systemd[1]: rc-local.service failed.

[root@localhost ~]# export PATH=/usr/local/mysql/bin:$PATH

这是未设置mysql环境变量引发的,系统在执行rc.local文件后才会加载环境变量,所以需要将rc.local的内容改为:

完结!!!

以上是 MySQL5.7多实例安装及开机启动配置(亲测) 的全部内容, 来源链接: utcz.com/z/534995.html

回到顶部