CentOS自带的yum仓库文件中,默认是没有mysql记录的,所以我们去mysql的官网下载mysql的yum仓库文件,注意对应版本。地址:https://dev.mysql.com/downloads/repo/yum/
感谢此篇博客,我就是看过此文章后,按需操作安装的。特此记录下来
参考博文:https://www.cnblogs.com/skyblue123/p/13287028.html
1.添加mysql的yum仓库文件
yum localinstall mysql80-community-release-el7-3.noarch
安装后会在yum的仓库列表中多出mysql相关的仓库文件
[root@VM-0-9-centos yum.repos.d]# ll
total 16
-rw-r--r-- 1 root root 614 Jun 30 10:40 CentOS-Base.repo
-rw-r--r-- 1 root root 230 Jun 30 10:40 CentOS-Epel.repo
-rw-r--r-- 1 root root 2076 Apr 25 2019 mysql-community.repo
-rw-r--r-- 1 root root 2108 Apr 25 2019 mysql-community-source.repo
每个
.repo
文件中都有多条记录,记录着相关软件的版本和安装地址等信息,我们把所有的.repo文件整体叫做yum的仓库
,即整个yum.repos.d目录下的内容。
2.更新yum仓库
yum makecache
3.查看yum仓库,可以看到有了mysql的相关记录
[root@VM-0-9-centos log]# yum repolist all |grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled
mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community disabled
mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community - disabled
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 212
mysql-connectors-community-source MySQL Connectors Community - disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 132
mysql-tools-community-source MySQL Tools Community - Sourc disabled
mysql-tools-preview/x86_64 MySQL Tools Preview disabled
mysql-tools-preview-source MySQL Tools Preview - Source disabled
mysql55-community/x86_64 MySQL 5.5 Community Server disabled
mysql55-community-source MySQL 5.5 Community Server - disabled
mysql56-community/x86_64 MySQL 5.6 Community Server disabled
mysql56-community-source MySQL 5.6 Community Server - disabled
mysql57-community/x86_64 MySQL 5.7 Community Server disabled
mysql57-community-source MySQL 5.7 Community Server - disabled
mysql80-community/x86_64 MySQL 8.0 Community Server enabled: 283
mysql80-community-source MySQL 8.0 Community Server - disabled
4.选择一个我们需要的版本,开始在线安装mysql
yum install mysql-community-server
在线等待yum下载安装完成,这个版本共需要490m左右。
安装成功会自动创建一个’root’@'localhost’的本地用户,临时密码在/var/log/mysql.log文件中(hn?oW<U6tFy7)
[root@VM-0-9-centos ~]# cat /var/log/mysqld.log |grep password
2021-09-22T11:35:49.670720Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: hn?oW<U6tFy7
5.修改临时密码
临时密码必须修改,不然mysql无法使用。新密码要符合mysql8的密码策略,或者可以修改密码策略。也可以把临时密码改一改作为新密码用
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '符合密码策略的新密码';
登录msql时,不要忘记 带上-u参数,不然会报Ignoring query to other database错误。带上-u参数重新登陆!
7.启动mysql服务,设置开机启动
启动:
systemctl start mysqld.service
开机启动:
systemctl enable mysqld.service
查看状态:
systemctl status mysqld.service
[root@VM-0-9-centos ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2021-09-22 19:35:55 CST; 2h 42min ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 17335 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─17335 /usr/sbin/mysqld
Sep 22 19:35:45 VM-0-9-centos systemd[1]: Starting MySQL Server...
Sep 22 19:35:55 VM-0-9-centos systemd[1]: Started MySQL Server.
8.开启数据库的远程访问功能
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> use mysql;
mysql> select user,host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
mysql> update user set host='%' where user='root';
mysql> select user,host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| root | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
刷新配置,使之生效!!!
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
测试远程链接数据库: