一、安装
1、下载MySQL离线安装包
下载地址:https://dev.mysql.com/downloads/mysql/
选择如下:
2、上传tar压缩包到服务器
这里也可以新建文件夹放也行,我就通过xftp软件上传放在opt的目录下
3、删除原有的mariadb
先通过rpm -qa|grep mariadb命令查看是否安装mariadb-libs库,如果能查的到就通过rpm -e --nodeps mariadb-libs命令删除,不删除的话等下安装MySQL会出现报错,有冲突。
[root@localhost /]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@localhost /]# rpm -e --nodeps mariadb-libs
[root@localhost /]# rpm -qa|grep mariadb
[root@localhost /]#
4、解压缩mysql离线安装包
通过 cd /opt 进入之前存放的数据库压缩包路径,然后ls 打印下文件信息,最后通过tar -xvf mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar来解压文件,记得压缩包名要核对下,每个版本的名称不一致,不然解压找不到文件。
[root@localhost /]# cd /opt
[root@localhost opt]# ls
mysql-8.2.0-1.el7.x86_64.rpm-bundle.tar rh
[root@localhost opt]# tar -xvf mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar
tar: mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar:无法 open: 没有那个文件或目录
tar: Error is not recoverable: exiting now
[root@localhost opt]# tar -xvf mysql-8.2.0-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-8.2.0-1.el7.x86_64.rpm
mysql-community-client-plugins-8.2.0-1.el7.x86_64.rpm
mysql-community-common-8.2.0-1.el7.x86_64.rpm
mysql-community-debuginfo-8.2.0-1.el7.x86_64.rpm
mysql-community-devel-8.2.0-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.2.0-1.el7.x86_64.rpm
mysql-community-icu-data-files-8.2.0-1.el7.x86_64.rpm
mysql-community-libs-8.2.0-1.el7.x86_64.rpm
mysql-community-libs-compat-8.2.0-1.el7.x86_64.rpm
mysql-community-server-8.2.0-1.el7.x86_64.rpm
mysql-community-server-debug-8.2.0-1.el7.x86_64.rpm
mysql-community-test-8.2.0-1.el7.x86_64.rpm
5、安装rmp包
逐个安装,按顺序来,如果包名不一致,记得改,命令如下:
rpm -ivh mysql-community-common-8.2.0-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.2.0-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.2.0-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.2.0-1.el7.x86_64.rpm
出现错误
错误的提示也很明细,提示我们提前安装mysql-community-client-plugins = 8.2.0-1.el7这个rpm包,之前解压的tar文件都有
[root@localhost opt]# rpm -ivh mysql-community-libs-8.2.0-1.el7.x86_64.rpm
警告:mysql-community-libs-8.2.0-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
错误:依赖检测失败:
mysql-community-client-plugins = 8.2.0-1.el7 被 mysql-community-libs-8.2.0-1.el7.x86_64 需要
解决方法
目前安装rpm -ivh mysql-community-libs-8.2.0-1.el7.x86_64.rpm出现的需要mysql-community-client-plugins = 8.2.0-1.el7这个依赖,所以找到之前我们解压的包,提前安装mysql-community-client-plugins = 8.2.0-1.el7.x86_64.rpm,后面在安装就不会报错了,剩下的rpm包报错也是这样子处理
[root@localhost opt]# rpm -ivh mysql-community-client-plugins-8.2.0-1.el7.x86_64.rpm
警告:mysql-community-client-plugins-8.2.0-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-client-plugins-8.################################# [100%]
[root@localhost opt]# rpm -ivh mysql-community-libs-8.2.0-1.el7.x86_64.rpm
警告:mysql-community-libs-8.2.0-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql-community-libs-8.2.0-1.el7 ################################# [100%]
6、查看是否安装成功
[root@localhost opt]# rpm -qa|grep mysql
mysql-community-icu-data-files-8.2.0-1.el7.x86_64
mysql-community-client-plugins-8.2.0-1.el7.x86_64
mysql-community-server-8.2.0-1.el7.x86_64
mysql-community-libs-8.2.0-1.el7.x86_64
mysql-community-client-8.2.0-1.el7.x86_64
mysql-community-common-8.2.0-1.el7.x86_64
二、启动MySQL服务
1、查看MySQL服务状态
[root@localhost opt]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
发现状态是出于enabled(启动)
2、关闭MySQL服务
[root@localhost opt]# systemctl stop mysqld
3、初始化数据库
成功了也没什么提示
[root@localhost opt]# mysqld --initialize --console
4、目录授权
在 MySQL 中,我们可以应用 chown 命令来控制数据库的读写权限。例如,下列命令的作用是将 MySQL 数据库的所有者和所属组修改为 mysql,以保证数据库的安全性:
[root@localhost opt]# chown -R mysql:mysql /var/lib/mysql/
通过 chown 命令修改 MySQL 数据库的所有者和所属组,我们可以确保 MySQL 数据库的安全性和稳定性。同时,我们也需要注意文件权限的改变是否可能会对数据库的正常运行产生影响,特别是在生产环境中,我们需要谨慎处理,避免对系统造成不必要的损失。
注意事项
- chown 命令需要以 root 权限运行,否则可能会被拒绝访问。
- 在修改文件权限之前,需要先备份重要数据,以避免不必要的损失。
- 如果文件权限发生改变,可能会影响 MySQL 数据库的正常运行,因此需要谨慎处理。
- 在修改 MySQL 数据库的所有者和所属组时,需要确保没有正在运行的 MySQL 进程,否则可能会导致服务不可用。
5、启动MySQL服务
通过systemctl start mysqld开启服务,在通过netstat -tlpn执行最后一行可以看到我们的MySQL服务的运行端口进程,说明已经成功,接下来是进去修改初始密码。
[root@localhost opt]# systemctl start mysqld
[root@localhost opt]# netstat -tlpn
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:111 0.0.0.0:* LISTEN 758/rpcbind
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1515/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1239/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1240/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1448/master
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 2681/sshd: root@pts
tcp 0 0 127.0.0.1:6011 0.0.0.0:* LISTEN 55358/sshd: root@pt
tcp6 0 0 :::111 :::* LISTEN 758/rpcbind
tcp6 0 0 :::22 :::* LISTEN 1239/sshd
tcp6 0 0 ::1:631 :::* LISTEN 1240/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 1448/master
tcp6 0 0 ::1:6010 :::* LISTEN 2681/sshd: root@pts
tcp6 0 0 ::1:6011 :::* LISTEN 55358/sshd: root@pt
tcp6 0 0 :::33060 :::* LISTEN 56532/mysqld
tcp6 0 0 :::3306 :::* LISTEN 56532/mysqld
三、数据库操作
1、查看临时密码
通过命令:cat /var/log/mysqld.log 执行,root@localhost:后面是密码,建议登录的时候复制粘贴比较好。
2、用临时密码登录数据库
命令:mysql -u root -p 回车键
然后复制粘贴临时密码(输入时不会显示出来,输入完直接回车)
[root@localhost opt]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.2.0
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.
3、修改临时密码
mysql> alter USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.03 sec)
4、授权远程连接
通过use mysql;使用MySQL库,执行select host, user, authentication_string from user;发现本地root用户是主机号localhost,所以需要将host主机改为%.
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> select host, user, authentication_string from user;
+-----------+------------------+------------------------------------------------------------------------+
| host | user | authentication_string |
+-----------+------------------+------------------------------------------------------------------------+
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | $A$005$-G(mK8y%."8%\!yMvV4bBdHE6w6YOCkQwWpTYBiKY3Jw6Ph1C61xWKrSf. |
+-----------+------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)
通过update user set host = “%” where user=‘root’;修改主机为%,在查看下内容select host, user, authentication_string from user;,然后刷新权限flush privileges;
mysql> update user set host = "%" where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select host, user, authentication_string from user;
+-----------+------------------+------------------------------------------------------------------------+
| host | user | authentication_string |
+-----------+------------------+------------------------------------------------------------------------+
| % | root | $A$005$-G(mK8y%."8%\!yMvV4bBdHE6w6YOCkQwWpTYBiKY3Jw6Ph1C61xWKrSf. |
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
+-----------+------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
5、使用navicat远程连接出现一下报错信息,这是linux防火墙处于开启状态导致的
6、远程防火墙设置
方案1:关闭防火墙
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
方案2:开放访问端口
1、开放端口:firewall-cmd --zone=public --add-port=3306/tcp --permanent
命令含义:
--zone #作用域
--add-port=3306/tcp #添加端口,格式为:端口/通讯协议
--permanent #永久生效,没有此参数重启后失效
2、重启生效:firewall-cmd --reload
本人采用的是第二种方案
[root@localhost opt]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@localhost opt]# firewall-cmd --reload
success
7、再次使用navicat连接,发现连接成功
文章来源:https://www.toymoban.com/news/detail-854161.html
四、总结
本人也是参考了一些信息,做了融合,参考文献如下。
https://www.cnblogs.com/quchunhui/p/11115339.html
https://blog.csdn.net/rao991207823/article/details/119840876
关于linux查看防火墙状态的内容可以参考这个
https://blog.csdn.net/qq_36640713/article/details/106553833文章来源地址https://www.toymoban.com/news/detail-854161.html
到了这里,关于linux离线安装mysql的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!