Centos7 安装mysql5.7

1.在Centos7中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,通过yum安装完成之后可以自动覆盖掉MariaDB

可通过命令 rpm -qa|grep mariadb 查看

[root@build ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64

2.下载并安装MySQL官方的 Yum Repository

wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

[root@build ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
--2022-08-25 08:05:15--  http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 23.10.6.175, 2600:140b:400:29b::2e31, 2600:140b:400:2b8::2e31
Connecting to dev.mysql.com (dev.mysql.com)|23.10.6.175|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm [following]
--2022-08-25 08:05:16--  https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
Connecting to dev.mysql.com (dev.mysql.com)|23.10.6.175|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm [following]
--2022-08-25 08:05:17--  https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 184.30.152.230
Connecting to repo.mysql.com (repo.mysql.com)|184.30.152.230|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 25548 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql57-community-release-el7-10.noarch.rpm’

100%[==================================================================================================================================>] 25,548       146KB/s   in 0.2s   

2022-08-25 08:05:17 (146 KB/s) - ‘mysql57-community-release-el7-10.noarch.rpm’ saved [25548/25548]

-c: No such file or directory
No URLs found in -c.
FINISHED --2022-08-25 08:05:17--
Total wall clock time: 2.1s
Downloaded: 1 files, 25K in 0.2s (146 KB/s)

3.rpm 安装:

# rpm -ivh https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm

[root@build ~]#  rpm -ivh https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
Retrieving https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
warning: /var/tmp/rpm-tmp.FntHEf: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql57-community-release-el7-11 ################################# [100%]

4.开始安装MySQL服务器

# yum -y install mysql-community-server

安装时有以下报错:

Total size: 210 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-client-5.7.39-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql


The GPG keys listed for the "MySQL 5.7 Community Server" repository are already installed but they are not correct for this package.
Check that the correct key URLs are configured for this repository.


 Failing package is: mysql-community-client-5.7.39-1.el7.x86_64
 GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

报错原因:MySQL GPG 密钥已过期导致

解决办法:执行以下下命令解决

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

5.重新执行yum -y install mysql-community-server, 看到以下打印则安装完成

Total size: 210 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : mysql-community-common-5.7.39-1.el7.x86_64                                                                                                               1/7 
  Installing : mysql-community-libs-5.7.39-1.el7.x86_64                                                                                                                 2/7 
  Installing : mysql-community-client-5.7.39-1.el7.x86_64                                                                                                               3/7 
  Installing : net-tools-2.0-0.25.20131004git.el7.x86_64                                                                                                                4/7 
  Installing : mysql-community-server-5.7.39-1.el7.x86_64                                                                                                               5/7 
  Installing : mysql-community-libs-compat-5.7.39-1.el7.x86_64                                                                                                          6/7 
  Erasing    : 1:mariadb-libs-5.5.68-1.el7.x86_64                                                                                                                       7/7 
  Verifying  : mysql-community-client-5.7.39-1.el7.x86_64                                                                                                               1/7 
  Verifying  : mysql-community-server-5.7.39-1.el7.x86_64                                                                                                               2/7 
  Verifying  : mysql-community-common-5.7.39-1.el7.x86_64                                                                                                               3/7 
  Verifying  : net-tools-2.0-0.25.20131004git.el7.x86_64                                                                                                                4/7 
  Verifying  : mysql-community-libs-5.7.39-1.el7.x86_64                                                                                                                 5/7 
  Verifying  : mysql-community-libs-compat-5.7.39-1.el7.x86_64                                                                                                          6/7 
  Verifying  : 1:mariadb-libs-5.5.68-1.el7.x86_64                                                                                                                       7/7 

Installed:
  mysql-community-libs.x86_64 0:5.7.39-1.el7            mysql-community-libs-compat.x86_64 0:5.7.39-1.el7            mysql-community-server.x86_64 0:5.7.39-1.el7           

Dependency Installed:
  mysql-community-client.x86_64 0:5.7.39-1.el7             mysql-community-common.x86_64 0:5.7.39-1.el7             net-tools.x86_64 0:2.0-0.25.20131004git.el7            

Replaced:
  mariadb-libs.x86_64 1:5.5.68-1.el7                                                                                                                                        

Complete!

6.启动MySQL

# systemctl start  mysqld.service

7.查看MySQL运行状态

# systemctl status mysqld.service

mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-08-25 08:45:25 CST; 7s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 2213 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 2160 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 2216 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─2216 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

8.更改mysql密码,查看msql临时密码

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

root@build ~]# cat /var/log/mysqld.log |grep 'password'
2022-08-25T00:45:22.149109Z 1 [Note] A temporary password is generated for root@localhost: Yo#gWGS<K0z:

9. 登陆mysql

# mysql -uroot -p回车,复制密码Yo#gWGS<K0z:

10.MySQL默认必须修改密码之后才能操作数据库:

MySQL有密码设置的规范,具体是与validate_password_policy的值有关

通过如下命令修改,可设置简单密码

# set global validate_password_policy=0;

# set global validate_password_length=1;

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'GIZZE.cn';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

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

11.设置root账户可远程连接, 设置root用户Host为%

Grant all on *.* to 'root'@'%' identified by 'root' with grant option;
 
flush privileges;

mysql> select Host, User from user;
+-----------+---------------+
| Host      | User          |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
3 rows in set (0.00 sec)

mysql> Grant all on *.* to 'root'@'%' identified by 'root' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

重新查询Host, User信息, user表Host已修改为%, plugin需为mysql_native_password

mysql> select Host, User, plugin from user;
+-----------+---------------+-----------------------+
| Host      | User          | plugin                |
+-----------+---------------+-----------------------+
| localhost | root          | mysql_native_password |
| localhost | mysql.session | mysql_native_password |
| localhost | mysql.sys     | mysql_native_password |
| %         | root          | mysql_native_password |
+-----------+---------------+-----------------------+
4 rows in set (0.00 sec)

通过navicat连接时失败

 此时需查询防火墙状态, 

# systemctl status firewalld

[root@build etc]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2022-08-25 10:55:09 CST; 7s ago
     Docs: man:firewalld(1)
 Main PID: 4062 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─4062 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

Aug 25 10:55:09 build systemd[1]: Starting firewalld - dynamic firewall daemon...
Aug 25 10:55:09 build systemd[1]: Started firewalld - dynamic firewall daemon.
Aug 25 10:55:09 build firewalld[4062]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a fut...ing it now.
Hint: Some lines were ellipsized, use -l to show in full.

关闭防火墙后重新连接OK

# systemctl stop firewalld

[root@build etc]# systemctl stop firewalld
[root@build etc]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Thu 2022-08-25 10:59:03 CST; 7s ago
     Docs: man:firewalld(1)
  Process: 4062 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 4062 (code=exited, status=0/SUCCESS)

Aug 25 10:55:09 build systemd[1]: Starting firewalld - dynamic firewall daemon...
Aug 25 10:55:09 build systemd[1]: Started firewalld - dynamic firewall daemon.
Aug 25 10:55:09 build firewalld[4062]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a fut...ing it now.
Aug 25 10:59:02 build systemd[1]: Stopping firewalld - dynamic firewall daemon...
Aug 25 10:59:03 build systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.

 12.修改mysql的编码

vi /etc/my.conf  增加以下配置,保存退出

systemctl restart mysqld.service 重启mysql

[mysqld]
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8

查询mysql编码

# show variables like 'character%';

mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

13.此时还有一个问题,因为安装了Yum Repository,以后每次yum操作都会自动更新,需要把这个卸载掉:

# yum -y remove mysql57-community-release-el7-10.noarch
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

z60015260

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值