CentOS7安装MySql多实例

安装之前的环境

在安装多实例之前首先先在虚拟机中的服务器里边先安装好一个MySql,本人的MySql版本为mysql-5.7.9-linux-glibc2.5-x86_64,端口为3306
在这里插入图片描述

开始安装一个新实例

1.在local目录下,创建一个data3文件夹,

cd /usr/local
mkdir data3

请忽略我的data1和data2
CentOS7安装MySql多实例_第1张图片
etc修改my.cnf文件,如果没有就创建,

vi my.cnf

将下面的文本粘贴上去

[root@localhost etc]# vi my.cnf

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

[mysqld1]
socket = /tmp/mysql1.sock
port = 3307
pid-file = /usr/local/data1/mysql.pid
datadir = /usr/local/data1
server-id = 1

[mysqld2]
socket = /tmp/mysql2.sock
port = 3308
pid-file = /usr/local/data2/mysql.pid
datadir = /usr/local/data2
server-id = 2

[mysqld3]
socket = /tmp/mysql3.sock
port = 3309
pid-file = /usr/local/data3/mysql.pid
datadir = /usr/local/data3
server-id = 3

我之前安装过两个实例一个端口是mysqld1,一个端口是mysqld2,这次我要装是mysql3,可根据自己的实际情况做调整。

再回到刚才的文件

cd /usr/local

执行安装命令

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

安装成功会有一个初始密码,一定要记下来
在这里插入图片描述
执行命令

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

配置开机启动

chkconfig mysqld_multid on

查看状态

mysqld_multi report

结果

Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
MySQL server from group: mysqld3 is not running

启动多实例

mysqld_multi start

再次查看实例是否启动,,如果启动不来就重启机器再试试

[root@localhost local]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
MySQL server from group: mysqld3 is running

修改密码,允许远程连接,再输入密码

mysql -u root -S /tmp/mysql.sock3 -p -P3309 -h127.0.0.1

这里有bug,就是输入密码会提示错误

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

尝试重启mysql服务器依然无效

service mysql restart

然后把data3的目录删除后重新安装,还是登录不了,之后重启服务器再试就可以了。如果没有遇到这个情况可以跳过重装和重启步骤。

重启之后登录成功

[root@localhost local]# mysql -u root -S /tmp/mysql.sock3 -p -P3309 -h127.0.0.1
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.9

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

修改密码

set password = 'root1234%';

分配root权限,并刷新权限

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root1234%';
flush privileges;

此时已经成功了一大半了,再设置一下防火墙,让其他机器能连上。
1.先看一下端口

firewall-cmd --zone=public --list-ports

结果

3306/tcp 3307/tcp

2.开放一个端口

firewall-cmd --zone=public --add-port=3309/tcp --permanent

3.将防火墙重启

firewall-cmd --reload

试试物理机是否能连上虚拟机的mysql实例
CentOS7安装MySql多实例_第2张图片

你可能感兴趣的:(Linux,MySql)