CentOS安装MySQL5.7多实例步骤详解
本次安装Mysql的CentOS版本是7.7
1.下载Mysql
首先去Mysql官网下载安装包,网址https://dev.mysql.com/downloads/mysql/,下载后将Mysql安装包上传至服务器/usr/local/目录下
2.创建Mysql用户和组
groupadd mysql #创建mysql组 useradd -r -d /home/mysql -g mysql mysql #创建mysql用户 并指定组和默认路径 chown -R mysql:mysql /home/mysql #将Mysql默认路径的用户和组改成mysql
3.解压Mysql
tar -xvf mysql-5.7.29-linux-glibc2.12-x86_64 #解压安装包 ln -s mysql-5.7.29-linux-glibc2.12-x86_64 mysql #文件夹添加软链接为mysql chown -R mysql:mysql /usr/local/mysql #将MySQL程序路径的用户和组改成mysql
4.为Mysql安装配置环境
使用root用户编辑/etc/profile,将下面的内容添加到最下面,并执行source /etc/profile
使配置生效
export PATH=$PATH:/usr/local/mysql/bin
关闭防火墙,防火墙状态为inactive时表示关闭成功
service firewalld stop
关闭SELinux,编辑/etc/selinux/config,将selinux=enforce改为disable即可
5.创建Mysql数据目录
mkdir /usr/local/mysqld_multi #创建MySQL数据母目录 mkdir /usr/local/mysqld_multi/mysql1 #为每个MySQL实例创建数据目录 mkdir /usr/local/mysqld_multi/mysql2 mkdir /usr/local/mysqld_multi/mysql3 mkdir /usr/local/mysqld_multi/mysql4 chown -R mysql:mysql /usr/local/mysqld_multi #将数据目录的用户和组改成mysql chmod -R 750 /usr/local/mysqld_multi #更改数据目录权限
6.配置my.cnf文件
新建my.cnf配置文件,并添加以下内容
[client] default-character-set=utf8 [mysqld_multi] mysqld = /usr/local/mysql/bin/mysqld_safe mysqladmin = /usr/local/mysql/bin/mysqladmin log = /usr/local/mysqld_multi/mysqld_multi.log lower_case_table_names=1 [mysqld1] server-id = 11 socket = /tmp/mysql.sock1 basedir=/usr/local/mysql port = 3307 bind_address = 0.0.0.0 datadir = /usr/local/mysqld_multi/mysql1 user = mysql performance_schema = off innodb_buffer_pool_size = 32M skip_name_resolve = 1 log_error = error.log pid-file = /usr/local/mysqld_multi/mysql1/mysql.pid1 [mysqld2] server-id = 12 socket = /tmp/mysql.sock2 basedir=/usr/local/mysql port = 3308 bind_address = 0.0.0.0 datadir = /usr/local/mysqld_multi/mysql2 user = mysql performance_schema = off innodb_buffer_pool_size = 32M skip_name_resolve = 1 log_error = error.log pid-file = /usr/local/mysqld_multi/mysql1/mysql.pid2 [mysqld3] server-id = 13 socket = /tmp/mysql.sock3 basedir=/usr/local/mysql port = 3309 bind_address = 0.0.0.0 datadir = /usr/local/mysqld_multi/mysql3 user = mysql performance_schema = off innodb_buffer_pool_size = 32M skip_name_resolve = 1 log_error = error.log pid-file = /usr/local/mysqld_multi/mysql1/mysql.pid3 [mysqld4] server-id = 14 socket = /tmp/mysql.sock4 basedir=/usr/local/mysql port = 3310 bind_address = 0.0.0.0 datadir = /usr/local/mysqld_multi/mysql4 user = mysql performance_schema = off innodb_buffer_pool_size = 32M skip_name_resolve = 1 log_error = error.log pid-file = /usr/local/mysqld_multi/mysql1/mysql.pid4
7.初始化Mysql
依次执行下面的命令初始化MySQL各个实例
mysqld --initialize --user=mysql --datadir=/usr/local/mysqld_multi/mysql1 mysqld --initialize --user=mysql --datadir=/usr/local/mysqld_multi/mysql2 mysqld --initialize --user=mysql --datadir=/usr/local/mysqld_multi/mysql3 mysqld --initialize --user=mysql --datadir=/usr/local/mysqld_multi/mysql4
8.启动Mysql
执行mysqld_mulit start
启动mysql,启动后通过mysqld_mulit report
可查看各个实例的状态
也可以通过mysqld_mulit {start|stop|restart} 1
分别停启MySQL实例
9.登录Mysql
通过查看各个实例数据目录下的error.log获取Mysql的初始化密码,并登录mysql
mysql -u root -p -S /tmp/mysql.sock1
10.修改root用户密码
通过下面的语句可以修改root用户的密码
set password = ‘root123‘;
为远程登录创建用户,并赋权
CREATE USER ‘root‘@‘%‘ IDENTIFIED BY ‘root123‘; GRANT ALL ON *.* TO ‘root‘@‘%‘; FLUSH PRIVILEGES;
这样就可以通过navicat等工具进行远程连接了,按照相同的方法设置可以其他三个实例的root密码
11.配置mysql开机自启
首先将mysqld_multi.server 复制到/etc/init.d下面
cp support-files/mysqld_multi.server /etc/init.d/mysqld_multid
这时通过service mysqld_multid report
启动MySQL会报错,原因是service调用服务时的PATH只有/sbin:/usr/sbin:/bin:/usr/bin
将mysql的bin路径添加到/etc/init.d/mysqld_multid中即可
再通过chkconfig将mysql服务添加到开机启动的列表里面就可以了
chkconfig --add --level 123456 mysqld_multid
12.配置login-path登录
为了方便多实例登录,可以配置login-path
mysql_config_editor set -G mysql -S /tmp/mysql.sock1 -u root -p
以上就是MySQL多实例安装的全部内容了,欢迎大家交流指正