1、首先准备需要用到的MySQL压缩包

这里我给大家提供了百度云链接,我的版本是8.0.17,你也可以自己在官网下载想要的rpm版本

链接:https://pan.baidu.com/s/1GGZPNRaRL6Rl3b948tv7jQ
提取码:ggwh

 

2、检查是否有旧版本的mysql包,有的centos镜像会默认携带

rpm -qa | grep mariadb

[root@DTS mysql]#  rpm -qa | grep mariadb
mariadb-libs-5.5.65-1.el7.x86_64

有的话要删除,没有的话就可以跳过下面的步骤了。

[root@DTS mysql]# rpm -e mariadb-libs-5.5.65-1.el7.x86_64 --nodeps

再次查看是否删除成功

[root@DTS mysql]# rpm -qa | grep mariadb
[root@DTS mysql]#

3、建立mysql文件夹并上传刚刚准备好的rpm压缩文件

[root@DTS /]# cd /usr/local/
[root@DTS local]# cd mysql/
[root@DTS mysql]#

接下来是上传文件,这里有很多种上传的方式,你可以用xshell的上传,也可以用xftp或者FileZilla client,总之选择你方便的方式,这里就不做详细讲解了。
上传好后查看如下:

[root@DTS mysql]# ls
mysql-8.0.17-1.el7.x86_64.rpm-bundle.tar

4、解压文件

tar -xvf mysql-8.0.17-1.el7.x86_64.rpm-bundle.tar

[root@DTS mysql]# tar -xvf mysql-8.0.17-1.el7.x86_64.rpm-bundle.tar 
mysql-community-test-8.0.17-1.el7.x86_64.rpm
mysql-community-server-8.0.17-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.17-1.el7.x86_64.rpm
mysql-community-client-8.0.17-1.el7.x86_64.rpm
mysql-community-devel-8.0.17-1.el7.x86_64.rpm
mysql-community-common-8.0.17-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.17-1.el7.x86_64.rpm
mysql-community-libs-8.0.17-1.el7.x86_64.rpm

5、安装common

rpm -ivh mysql-community-common-8.0.17-1.el7.x86_64.rpm --nodeps --force

[root@DTS mysql]# rpm -ivh mysql-community-common-8.0.17-1.el7.x86_64.rpm --nodeps --force
警告:mysql-community-common-8.0.17-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-common-8.0.17-1.e################################# [100%]

**

6、安装libs

rpm -ivh mysql-community-libs-8.0.17-1.el7.x86_64.rpm --nodeps --force

[root@DTS mysql]# rpm -ivh mysql-community-libs-8.0.17-1.el7.x86_64.rpm --nodeps --force
警告:mysql-community-libs-8.0.17-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-libs-8.0.17-1.el7################################# [100%]

7、安装client

rpm -ivh mysql-community-client-8.0.17-1.el7.x86_64.rpm --nodeps –

[root@DTS mysql]# rpm -ivh mysql-community-client-8.0.17-1.el7.x86_64.rpm --nodeps --force
警告:mysql-community-client-8.0.17-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-client-8.0.17-1.e################################# [100%]

8、安装server

rpm -ivh mysql-community-server-8.0.17-1.el7.x86_64.rpm --nodeps --force

[root@DTS mysql]# rpm -ivh mysql-community-server-8.0.17-1.el7.x86_64.rpm --nodeps --force
警告:mysql-community-server-8.0.17-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-server-8.0.17-1.e################################# [100%]

9、查看已安装的mysql安装包

rpm -qa | grep mysql

[root@DTS mysql]# rpm -qa | grep mysql
mysql-community-libs-8.0.17-1.el7.x86_64
mysql-community-common-8.0.17-1.el7.x86_64
mysql-community-server-8.0.17-1.el7.x86_64
mysql-community-client-8.0.17-1.el7.x86_64

到这就安装完毕了,接下来就是对mysql数据库的一些配置

10、对 mysql 数据库进行初始化和相关配置

mysqld --initialize; 初始化mysql
chown mysql:mysql /var/lib/mysql -R; 对操作文件赋予权限
systemctl start mysqld.service; 启动mysql服务
systemctl enable mysqld; 设置开机自启

[root@DTS mysql]# mysqld --initialize;
[root@DTS mysql]# chown mysql:mysql /var/lib/mysql -R;
[root@DTS mysql]# systemctl start mysqld.service;
[root@DTS mysql]# systemctl  enable mysqld;

11、查看生成的默认密码

cat /var/log/mysqld.log | grep password

[root@DTS mysql]# cat /var/log/mysqld.log | grep password
2020-07-02T06:44:39.579110Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: K+e9eX>=>IjQ

dbeaver离线安装的mysql离线驱动下载 mysql离线安装包_java

这里就是密码

12、使用刚才查询到的密码登陆mysql并修改密码

[root@DTS mysql]# mysql -uroot -p
这里要输入刚才查到的密码,复制粘贴就好,这里输入密码时是不显示明文的。

[root@DTS mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.17

Copyright (c) 2000, 2019, 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.

mysql>

然后输入
ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘root’;
修改密码为root,你也可以改成别的,即更换BY后面引号内的内容,之后输入exit退出登录

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root'; 
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

13、重新登录mysql,进行远程访问授权

输入mysql -uroot -p,然后输入刚才修改的密码root

[root@DTS mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.17 MySQL Community Server - GPL

Copyright (c) 2000, 2019, 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.

mysql>

通过以下命令,进行远程访问的授权

1、create user ‘root’@’%’ identified with mysql_native_password by ‘root’;
2、grant all privileges on . to ‘root’@’%’ with grant option;
3、flush privileges;

修改后退出MySQL

mysql> create user 'root'@'%' identified with mysql_native_password by 'root';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on *.* to 'root'@'%' with grant option;
Query OK, 0 rows affected (0.01 sec)

mysql> 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

到这里MySQL就安装完毕了,但是通常这时候远程访问还是访问不到的,因为系统防火墙还没有关闭,下面这个步骤可选,如果工作环境不允许关闭防火墙就需要单独为MySQL开启远程端口,将MySQL端口添加到防火墙白名单,我图省事,直接把防火墙关了

14、关闭防火墙(可选步骤)

systemctl stop firewalld.service 停止防火墙
systemctl disable firewalld.service 禁用防火墙
systemctl mask firewalld.service 禁止开机自启

[root@DTS mysql]# systemctl stop firewalld.service;
[root@DTS mysql]# systemctl disable firewalld.service;
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@DTS mysql]# systemctl mask firewalld.service;
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.

查看防火墙状态
systemctl status firewalld.service

[root@DTS mysql]# systemctl status firewalld.service;
● firewalld.service
   Loaded: masked (/dev/null; bad)
   Active: inactive (dead) since 四 2020-07-02 14:48:38 CST; 20s ago
 Main PID: 72326 (code=exited, status=0/SUCCESS)

7月 02 14:04:49 DTS firewalld[72326]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -X DOCKER' failed: iptables: No cha...at name.
7月 02 14:04:49 DTS firewalld[72326]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -F DOCKER-ISOLATION-STAGE-1' failed...at name.
7月 02 14:04:49 DTS firewalld[72326]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -X DOCKER-ISOLATION-STAGE-1' failed...at name.
7月 02 14:04:49 DTS firewalld[72326]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -F DOCKER-ISOLATION-STAGE-2' failed...at name.
7月 02 14:04:49 DTS firewalld[72326]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -X DOCKER-ISOLATION-STAGE-2' failed...at name.
7月 02 14:04:49 DTS firewalld[72326]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -F DOCKER-ISOLATION' failed: iptabl...at name.
7月 02 14:04:49 DTS firewalld[72326]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -X DOCKER-ISOLATION' failed: iptabl...at name.
7月 02 14:04:49 DTS firewalld[72326]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -D FORWARD -i docker0 -o docker0 -j DROP' fai...chain?).
7月 02 14:48:37 DTS systemd[1]: Stopping firewalld - dynamic firewall daemon...
7月 02 14:48:38 DTS systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.

OK!

这篇文章我参考了以下大神的思路,我是在这个基础之上做了简化和优化讲解,如果你是小白的话建议看下面这个,从零开始,如果有一定基础的话看我这个就够啦!