CentOS7下安装指定版本MySQL

一、安装之前检查系统是否存在自带的mysql rpm源

[root@localhost ~]# rpm -qa |grep mysql         # 查看是否存在mysql rpm源
[root@localhost ~]# 
[root@localhost ~]# rpm -qa |grep mariadb       # 查看是否存在mariadb rpm源
mariadb-libs-5.5.68-1.el7.x86_64
[root@localhost ~]# 
[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
[root@localhost ~]# 
[root@localhost ~]# rpm -qa |grep mariadb
[root@localhost ~]# 

二、安装过程

1、下载mysql的rpm源
https://repo.mysql.com      # 在这个地址中找到自己想要安装的版本,复制为如下格式的连接
https://repo.mysql.com/mysql80-community-release-el7-7.noarch.rpm
[root@localhost ~]# curl -O https://repo.mysql.com/mysql80-community-release-el7-7.noarch.rpm
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 11196  100 11196    0     0   9777      0  0:00:01  0:00:01 --:--:--  9786
[root@localhost ~]# ls
anaconda-ks.cfg  mysql80-community-release-el7-7.noarch.rpm
[root@localhost ~]# 
2、安装mysql源
[root@localhost ~]# yum localinstall ./mysql80-community-release-el7-7.noarch.rpm       # 安装上面下载好的rpm源
Loaded plugins: fastestmirror
Examining ./mysql80-community-release-el7-7.noarch.rpm: mysql80-community-release-el7-7.noarch
Marking ./mysql80-community-release-el7-7.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql80-community-release.noarch 0:el7-7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================
 Package                               Arch               Version            Repository                                           Size
================================================================================================================
Installing:
 mysql80-community-release             noarch             el7-7              /mysql80-community-release-el7-7.noarch              10 k

Transaction Summary
=======================================================================================================================================
Install  1 Package

Total size: 10 k
Installed size: 10 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
** Found 2 pre-existing rpmdb problem(s), 'yum check' output follows:
2:postfix-2.10.1-9.el7.x86_64 has missing requires of libmysqlclient.so.18()(64bit)
2:postfix-2.10.1-9.el7.x86_64 has missing requires of libmysqlclient.so.18(libmysqlclient_18)(64bit)
  Installing : mysql80-community-release-el7-7.noarch                                                                              1/1 
  Verifying  : mysql80-community-release-el7-7.noarch                                                                              1/1 

Installed:
  mysql80-community-release.noarch 0:el7-7                                                                                             

Complete!

# 到此rpm源安装成功
[root@localhost ~]# 
[root@localhost ~]# rpm -qa |grep mysql     # 查看rpm源是否安装成功
mysql80-community-release-el7-7.noarch
[root@localhost ~]# 
[root@localhost ~]# yum repolist enabled |grep mysql        # 查看rpm源是否安装成功
mysql-connectors-community/x86_64       MySQL Connectors Community           206
mysql-tools-community/x86_64            MySQL Tools Community                 94
mysql80-community/x86_64                MySQL 8.0 Community Server           367
[root@localhost ~]# 
3、安装
[root@localhost ~]# yum install mysql-community-server      # 安装mysql-community-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.ustc.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:8.0.31-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 8.0.31-1.el7 for package: mysql-community-server-8.0.31-1.el7.x86_64
--> Processing Dependency: mysql-community-icu-data-files = 8.0.31-1.el7 for package: mysql-community-server-8.0.31-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 8.0.11 for package: mysql-community-server-8.0.31-1.el7.x86_64
--> Processing Dependency: libaio.so.1(LIBAIO_0.1)(64bit) for package: mysql-community-server-8.0.31-1.el7.x86_64
--> Processing Dependency: libaio.so.1(LIBAIO_0.4)(64bit) for package: mysql-community-server-8.0.31-1.el7.x86_64
--> Processing Dependency: libaio.so.1()(64bit) for package: mysql-community-server-8.0.31-1.el7.x86_64
--> Running transaction check
---> Package libaio.x86_64 0:0.3.109-13.el7 will be installed
---> Package mysql-community-client.x86_64 0:8.0.31-1.el7 will be installed
--> Processing Dependency: mysql-community-client-plugins = 8.0.31-1.el7 for package: mysql-community-client-8.0.31-1.el7.x86_64
--> Processing Dependency: mysql-community-libs(x86-64) >= 8.0.11 for package: mysql-community-client-8.0.31-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:8.0.31-1.el7 will be installed
---> Package mysql-community-icu-data-files.x86_64 0:8.0.31-1.el7 will be installed
--> Running transaction check
---> Package mysql-community-client-plugins.x86_64 0:8.0.31-1.el7 will be installed
---> Package mysql-community-libs.x86_64 0:8.0.31-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================
 Package                                      Arch                 Version                       Repository                       Size
=======================================================================================================================================
Installing:
 mysql-community-server                       x86_64               8.0.31-1.el7                  mysql80-community                64 M
Installing for dependencies:
 libaio                                       x86_64               0.3.109-13.el7                base                             24 k
 mysql-community-client                       x86_64               8.0.31-1.el7                  mysql80-community                16 M
 mysql-community-client-plugins               x86_64               8.0.31-1.el7                  mysql80-community               2.5 M
 mysql-community-common                       x86_64               8.0.31-1.el7                  mysql80-community               647 k
 mysql-community-icu-data-files               x86_64               8.0.31-1.el7                  mysql80-community               2.1 M
 mysql-community-libs                         x86_64               8.0.31-1.el7                  mysql80-community               1.5 M

Transaction Summary
=======================================================================================================================================
Install  1 Package (+6 Dependent packages)

Total download size: 87 M
Installed size: 409 M
Is this ok [y/d/N]: y
Downloading packages:
(1/7): libaio-0.3.109-13.el7.x86_64.rpm                                                                         |  24 kB  00:00:01     
warning: /var/cache/yum/x86_64/7/mysql80-community/packages/mysql-community-client-plugins-8.0.31-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
Public key for mysql-community-client-plugins-8.0.31-1.el7.x86_64.rpm is not installed
(2/7): mysql-community-client-plugins-8.0.31-1.el7.x86_64.rpm                                                   | 2.5 MB  00:00:04     
(3/7): mysql-community-common-8.0.31-1.el7.x86_64.rpm                                                           | 647 kB  00:00:00     
(4/7): mysql-community-icu-data-files-8.0.31-1.el7.x86_64.rpm                                                   | 2.1 MB  00:00:01     
(5/7): mysql-community-libs-8.0.31-1.el7.x86_64.rpm                                                             | 1.5 MB  00:00:03     
(6/7): mysql-community-client-8.0.31-1.el7.x86_64.rpm                                                           |  16 MB  00:00:19     
(7/7): mysql-community-server-8.0.31-1.el7.x86_64.rpm                                                           |  64 MB  00:00:40     
---------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                  1.7 MB/s |  87 MB  00:00:50     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
Importing GPG key 0x3A79BD29:
 Userid     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 Fingerprint: 859b e8d7 c586 f538 430b 19c2 467b 942d 3a79 bd29
 Package    : mysql80-community-release-el7-7.noarch (@/mysql80-community-release-el7-7.noarch)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
Is this ok [y/N]: y
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
 Userid     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 Package    : mysql80-community-release-el7-7.noarch (@/mysql80-community-release-el7-7.noarch)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql-community-client-plugins-8.0.31-1.el7.x86_64                                                                  1/7 
  Installing : mysql-community-common-8.0.31-1.el7.x86_64                                                                          2/7 
  Installing : mysql-community-libs-8.0.31-1.el7.x86_64                                                                            3/7 
  Installing : mysql-community-client-8.0.31-1.el7.x86_64                                                                          4/7 
  Installing : libaio-0.3.109-13.el7.x86_64                                                                                        5/7 
  Installing : mysql-community-icu-data-files-8.0.31-1.el7.x86_64                                                                  6/7 
  Installing : mysql-community-server-8.0.31-1.el7.x86_64                                                                          7/7 
  Verifying  : mysql-community-server-8.0.31-1.el7.x86_64                                                                          1/7 
  Verifying  : mysql-community-common-8.0.31-1.el7.x86_64                                                                          2/7 
  Verifying  : mysql-community-client-plugins-8.0.31-1.el7.x86_64                                                                  3/7 
  Verifying  : mysql-community-icu-data-files-8.0.31-1.el7.x86_64                                                                  4/7 
  Verifying  : mysql-community-client-8.0.31-1.el7.x86_64                                                                          5/7 
  Verifying  : libaio-0.3.109-13.el7.x86_64                                                                                        6/7 
  Verifying  : mysql-community-libs-8.0.31-1.el7.x86_64                                                                            7/7 

Installed:
  mysql-community-server.x86_64 0:8.0.31-1.el7                                                                                         

Dependency Installed:
  libaio.x86_64 0:0.3.109-13.el7                                         mysql-community-client.x86_64 0:8.0.31-1.el7                  
  mysql-community-client-plugins.x86_64 0:8.0.31-1.el7                   mysql-community-common.x86_64 0:8.0.31-1.el7                  
  mysql-community-icu-data-files.x86_64 0:8.0.31-1.el7                   mysql-community-libs.x86_64 0:8.0.31-1.el7                    

Complete!
[root@localhost ~]# 

# 到此安装成功
4、查看是否安装成功
[root@localhost ~]# yum list installed mysql*       # 查看已安装的mysql相关的软件包
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.ustc.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Installed Packages
mysql-community-client.x86_64                               8.0.31-1.el7                       @mysql80-community                      
mysql-community-client-plugins.x86_64                       8.0.31-1.el7                       @mysql80-community                      
mysql-community-common.x86_64                               8.0.31-1.el7                       @mysql80-community                      
mysql-community-icu-data-files.x86_64                       8.0.31-1.el7                       @mysql80-community                      
mysql-community-libs.x86_64                                 8.0.31-1.el7                       @mysql80-community                      
mysql-community-server.x86_64                               8.0.31-1.el7                       @mysql80-community                      
mysql80-community-release.noarch                            el7-7                              @/mysql80-community-release-el7-7.noarch
[root@localhost ~]# 
[root@localhost ~]# systemctl status mysqld     # 查看mysql服务状态
● 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
[root@localhost ~]# 

[root@localhost ~]# systemctl start mysqld      # 启动mysql服务
[root@localhost ~]# systemctl status mysqld     # 查看mysql服务状态
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2022-12-28 13:46:21 CST; 2s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 64032 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 64110 (mysqld)
   Status: "Server is operational"
   CGroup: /system.slice/mysqld.service
           └─64110 /usr/sbin/mysqld

Dec 28 13:46:19 localhost.localdomain systemd[1]: Starting MySQL Server...
Dec 28 13:46:21 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost ~]# 

到此mysql安装成功。接下来配置mysql服务。

三、mysql配置

1、查看mysql安装生成的root用户密码
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
2022-12-28T05:46:19.928855Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: do,-moX%)2do
[root@localhost ~]# 
# 生成的默认密码为:do,-moX%)2do
2、使用默认密码登录mysql
[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.31

Copyright (c) 2000, 2022, 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> 
3、更改root用户默认改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';        # 如将root用户本地登陆密码改为123456
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements      # 该报错原因是新密码不满足密码策略
mysql> 
mysql> SHOW VARIABLES LIKE "%password%";        # 查看现有密码策略
+----------------------------------------------+-----------------+
| Variable_name                                | Value           |
+----------------------------------------------+-----------------+
| caching_sha2_password_auto_generate_rsa_keys | ON              |
| caching_sha2_password_digest_rounds          | 5000            |
| caching_sha2_password_private_key_path       | private_key.pem |
| caching_sha2_password_public_key_path        | public_key.pem  |
| default_password_lifetime                    | 0               |
| disconnect_on_expired_password               | ON              |
| generated_random_password_length             | 20              |
| mysql_native_password_proxy_users            | OFF             |
| password_history                             | 0               |
| password_require_current                     | OFF             |
| password_reuse_interval                      | 0               |
| report_password                              |                 |
| sha256_password_auto_generate_rsa_keys       | ON              |
| sha256_password_private_key_path             | private_key.pem |
| sha256_password_proxy_users                  | OFF             |
| sha256_password_public_key_path              | public_key.pem  |
| validate_password.check_user_name            | ON              |
| validate_password.dictionary_file            |                 |
| validate_password.length                     | 8               |
| validate_password.mixed_case_count           | 1               |
| validate_password.number_count               | 1               |
| validate_password.policy                     | MEDIUM          |
| validate_password.special_char_count         | 1               |
+----------------------------------------------+-----------------+
23 rows in set (0.01 sec)

# validate_password.length      密码长度
# validate_password.policy      含有字母大小的啥的
mysql> 

mysql> set global validate_password.policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password.length=0;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE "%password%";        # 查看更改后的密码策略
+----------------------------------------------+-----------------+
| Variable_name                                | Value           |
+----------------------------------------------+-----------------+
| caching_sha2_password_auto_generate_rsa_keys | ON              |
| caching_sha2_password_digest_rounds          | 5000            |
| caching_sha2_password_private_key_path       | private_key.pem |
| caching_sha2_password_public_key_path        | public_key.pem  |
| default_password_lifetime                    | 0               |
| disconnect_on_expired_password               | ON              |
| generated_random_password_length             | 20              |
| mysql_native_password_proxy_users            | OFF             |
| password_history                             | 0               |
| password_require_current                     | OFF             |
| password_reuse_interval                      | 0               |
| report_password                              |                 |
| sha256_password_auto_generate_rsa_keys       | ON              |
| sha256_password_private_key_path             | private_key.pem |
| sha256_password_proxy_users                  | OFF             |
| sha256_password_public_key_path              | public_key.pem  |
| validate_password.check_user_name            | ON              |
| validate_password.dictionary_file            |                 |
| validate_password.length                     | 4               |
| validate_password.mixed_case_count           | 1               |
| validate_password.number_count               | 1               |
| validate_password.policy                     | LOW             |
| validate_password.special_char_count         | 1               |
+----------------------------------------------+-----------------+
23 rows in set (0.00 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';        # 如将root用户本地登陆密码改为123456
Query OK, 0 rows affected (0.00 sec)

mysql> 
# 改密成功
4、设置远程访问数据库用户和密码

两种方式

方式一(我失败了,但参数说明可供参考)
grant all privileges on *.* to 'root' @'%' identified by '123456';     

第一个*是数据库,可以改成允许访问的数据库名称

第二个 是数据库的表名称,代表允许访问任意的表

root代表远程登录使用的用户名,可以自定义

%代表允许任意ip登录,如果你想指定特定的IP,可以把%替换掉就可以了

password代表远程登录时使用的密码,可以自定义

让权限立即生效:flush privileges

mysql> grant all privileges on *.* to 'root' @'%' identified by '123456';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by '123456'' at line 1
方式二(成功)
# 创建用户
CREATE USER 'root'@'%' IDENTIFIED BY '123456';
# 授权
grant all privileges on *.* to 'root'@'%' ;
# 刷新访问权限表
FLUSH PRIVILEGES;

四、远程连接mysql

这里使用的远程连接mysql的工具为DataGrip


error.png

异常原因为防火墙为开放3306端口,这里直接将防火墙进行关闭

# 关闭防火墙
systemctl stop firewalld.service
# 查看防火墙状态
firewall-cmd --state 或者 systemctl status firewalld.service

以下为解决上述异常碰到的小插曲以为是selinux引起的,引以为戒

[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# setenforce
usage:  setenforce [ Enforcing | Permissive | 1 | 0 ]
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# setenforce 1
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350

推荐阅读更多精彩内容