摘要:
打工人奉旨制作数据库服务的虚拟机模板,模板中包含各种数据库,其中mysql需要具备5.7及8.0两个版本,并保证服务能正常同时使用。此文也当成一份笔记,方便后续自己查阅。
本次安装基于截止于写稿时最新的MySQL版本,mysql 5.7.41以及mysql 8.0.33,通过官方提供的编译文件压缩包(tar.gz),非rpm安装文件。
1. 下载
官网MySQL :: Download MySQL Community Server (Archived Versions)下载需要版本的mysql
mysql-5.7.41-el7-x86_64.tar.gzhttps://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.41-el7-x86_64.tar.gz
mysql-8.0.33-el7-x86_64.tar.gzhttps://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.33-el7-x86_64.tar.gz
2. 安装
2.1 准备
2.1.1 路径规划
服务器上通过LVM挂在规划 /u01 路径用于安装应用,/u01路径用于存储数据
创建文件夹:
[root@localhost ~]# mkdir -p /u01/mysql #安装包解压到此路径下
[root@localhost ~]# mkdir -p /u02/mysql/mysq57/data
[root@localhost ~]# mkdir -p /u02/mysql/mysq57/logs
[root@localhost ~]# mkdir -p /u02/mysql/mysq80/data
[root@localhost ~]# mkdir -p /u02/mysql/mysq80/logs
2.1.2 解压
将两个tar.gz压缩包上传到服务器的/u01/mysql路径下,解压后得到与压缩包名一致的文件夹,将文件夹重命名(主要为了路径简短好记)
[root@localhost mysql]# tar -zxvf mysql-5.7.41-el7-x86_64.tar.gz
[root@localhost mysql]# mv mysql-5.7.41-el7-x86_64.tar.gz mysql-5.7.41
[root@localhost mysql]# tar -zxvf mysql-8.0.33-el7-x86_64.tar.gz
[root@localhost mysql]# mv mysql-8.0.33-el7-x86_64.tar.gz mysql-5.0.33
如下:(安装完后我已将压缩包删除,并且安装过程中会变更文件所有人,故下图仅有两个文件夹,且文件夹的所有者都是mysql,基于root操作的此时的所有者应该是root)
2.2 配置
2.2.1 删除/重命名/etc/my.cnf
/etc/my.cnf是mysql默认且优先读取的配置文件,第二顺位是安装路径下的my.cnf,由于我们需要装载两个mysql且互不干扰,所以需要删掉第一顺位的配置文件。
2.2.2 为两个mysql分别创建配置文件my.cnf
创建 /u01/mysql/mysql-5.7.41/my.cnf 以及 /u01/mysql/mysql-8.0.33/my.cnf,文件内容如下:
/u01/mysql/mysql-5.7.41/my.cnf:
[mysqld]
port=3357
user=mysql
basedir=/u01/mysql/mysql-5.7.41
datadir=/u02/mysg/mysgl57/data
socket=/tmp/mysql57.sock
log-error=/u02/mysql/mysql57/logs/mysql.err
pid-file=/u02/mysql/mysql57/mysql.pid
character_set_server=utf8mb4
lower_case_table_names=1
[mysqld_safe]
log-error=/u02/mysql/mysql57/logs/mysql.err
pid-file=/u02/mysql/mysql57/mysql.pid
tmpdir=/tmp/mysql57
/u01/mysql/mysql-8.0.33/my.cnf :
[mysqld]
port=3380
user=mysql
basedir=/u01/mysql/mysql-8.0.33
datadir=/u02/mysg/mysgl80/data
socket=/tmp/mysql80.sock
log-error=/u02/mysql/mysql80/logs/mysql.err
pid-file=/u02/mysql/mysql80/mysql.pid
character_set_server=utf8mb4
lower_case_table_names=1
[mysqld_safe]
log-error=/u02/mysql/mysql80/logs/mysql.err
pid-file=/u02/mysql/mysql80/mysql.pid
tmpdir=/tmp/mysql80
2.2.3 配置service启动文件
以mysql5.7.41为例,编辑 /u01/mysql/mysql-5.7.41/support-files/mysql.server
将文件的第46、47、58、63、207行,将对应信息配置为我们安装的真实路径,如下:
/u01/mysql/mysql-8.0.33/support-files/mysql.server 如是
将编辑好的/u01/mysql/mysql-5.7.41/support-files/mysql.server、/u01/mysql/mysql-8.0.33/support-files/mysql.server复制到 /etc/init.d/ 下,并命名为 mysql57 和 mysql80,后面通过service命令启动服务会用到
[root@localhost ~]# cp /u01/mysql/mysql-5.7.33/support-files/mysql.server /etc/init.d/mysql57
[root@localhost ~]# cp /u01/mysql/mysql-8.0.33/support-files/mysql.server /etc/init.d/mysql80
2.2.4 创建mysql用户并更改路径所有者
[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -r -g mysql mysql
[root@localhost ~]# chown -R mysql.mysql /u01/mysql
[root@localhost ~]# chown -R mysql.mysql /u02/mysql
2.3 初始化数据库
进入到安装路径下的bin目录,以mysql 5.7.41为例
[root@localhost ~]# cd /u01/mysql/mysql-5.7.41/bin
[root@localhost bin]# ./mysqld --defaults-file=/u01/mysql/mysql-5.7.41/my.cnf --basedir=/u01/mysql/mysql-5.7.41 --datadir=/u02/mysql/mysql57/data --user=mysql --initialize
[root@localhost bin]# service mysql57 start
Starting MySQL.....SUCCESS!
至此,mysql5.7.41安装并启动成功,mysql8.0.33如是,最后通过service msyql80 start 进行启动
3. 登录数据库服务器
在数据库初始化过程中,会随机生成root密码,记录在我们配置的log-error文件中
[root@localhost bin]# cat /u02/mysql/mysql57/logs/mysql.err | grep password
2023-07-19T08:23:56.987382Z 1 [Note] A temporary password is generated for root@localhost: #Bdm6F?J!2
信息中显示的【#Bdm6F?J!2】就是初次登录需要用到的密码,登录mysql5.7.41数据库并修改root的密码,打开远程访问限制(依然在安装目录的bin目录下)
[root@localhost ~]# cd /u01/mysql/mysql-5.7.41/bin
[root@localhost bin]# ./mysql -uroot -p -S /tmp/mysql57.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.41
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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.
mysql> set password = password('root.123'); #修改root密码
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set host = '%' where user='root'; #开启root的远程访问权限
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
mysql> exit
Bye
[root@localhost bin]# service mysql57 restart #重启服务
mysql 8.0.33操作类似,但需要注意的是8.0版本在首次登录时修改密码的语法与5.7的不一样,下面是mysql 8.0.33首次登录及修改密码的过程:
[root@localhost ~]# cd /u01/mysql/mysql-8.0.33/bin
[root@localhost bin]# ./mysql -u root -p -S /tmp/mysql80.sock
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.33
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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.
mysql> alter user 'root'@'localhost' identified by 'root.123';
Query OK, 0 rows affected (0.08 sec)
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set host= '%' where user='root';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye
[root@localhost bin]# service mysql80 restart
Shutting down MySQL.. SUCCESS!
Starting MySQL...... SUCCESS!
4. 创建MySQL命令行的软连接
类似第3章中进入到mysql安装路径的bin目录中执行 ./mysql xxxx 来进行登录,我们需要的是在任意目录下都可以通过mysql命令进行登录,有两种解决办法:
1. 在 /usr/bin 目录下创建 /u01/mysql/mysql-8.0.33/bin/mysql 的软连接
[root@localhost ~]# ln -s /u01/mysql/mysql-8.0.33/bin/mysql /usr/bin/mysql
2. 将 mysql8.0.33的bin目录加入到PATH当中
[root@localhost ~]# export PATH=$PATH:/u01/mysql/mysql-8.0.33/bin/
我个人是将5.7和8.0下的mysql分别创建了软连接,因为我也还没搞清楚,通过8.0的客户端指令登录5.7的服务能不能兼容,反正指定了socket也能正常登录,但总觉得还是会有点区别,反正方法都在上面,大家自行甄别。
另:将bin加入到PATH还有个好处就是其他命令,比如mysqldump啥的也能用,就是需要指定不同的socket
创建完成后即可在任意目录下通过指定不同的socket,进入不同版本的mysql
[root@localhost ~]# mysql -u root -p -S /tmp/mysql80.sock
## 或者
[root@localhost ~]# mysql -u root -p -S /tmp/mysql57.sock
当然,不想每次都输入socket也是可以偷懒的,哈哈哈哈哈哈哈哈哈哈哈,方法就是通过alias别名的方式默认加上socket,方法如下:
针对所有用户,可编辑 /etc/bashrc 文件,只针对root用户,可编辑 /root/.bashrc,针对其他个别用户,可编辑 /home/[用户名]/.bashrc
[root@localhost ~]# vim .bashrc
### 添加
alias mysql80='mysql -S /tmp/mysql80.sock'
alias mysql57='mysql -S /tmp/mysql57.sock'
### Esc + wq! 保存退出
[root@localhost ~]# source .bashrc
至此,就可直接通过 mysql80 -u root -p 命令登录mysql 8.0服务,通过 mysql57 -u root -p 命令登录mysql 5.7服务
当然还有一些其他的可增加易用的方法,比如将服务添加到systemctl,或者通过mysqld_multi来管理多个服务,后续有时间再研究吧。文章来源:https://www.toymoban.com/news/detail-721698.html
完结!撒花!文章来源地址https://www.toymoban.com/news/detail-721698.html
到了这里,关于同一Linux下安装两个版本的MySQL的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!