centos安装mysql5.7及允许远程连接mysql

1、下载 MySQL 官方的 Yum Repository

从 MySQL 官网选取合适的 MySQL 版本,获取下载地址。

然后使用 wget 下载:

[root@localhost opt]# wget http://repo.mysql.com//mysql57-community-release-el7-8.noarch.rpm
--2019-09-18 14:47:40--  http://repo.mysql.com//mysql57-community-release-el7-8.noarch.rpm
正在解析主机 repo.mysql.com (repo.mysql.com)... 23.195.78.58
正在连接 repo.mysql.com (repo.mysql.com)|23.195.78.58|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:9116 (8.9K) [application/x-redhat-package-manager]
正在保存至: “mysql57-community-release-el7-8.noarch.rpm”

100%[=========================================================================================================================================================================>] 9,116       --.-K/s 用时 0s      

2019-09-18 14:47:40 (210 MB/s) - 已保存 “mysql57-community-release-el7-8.noarch.rpm” [9116/9116])

2、安装 yum Repository

[root@localhost opt]#  yum -y install mysql57-community-release-el7-8.noarch.rpm
已加载插件:fastestmirror
正在检查 mysql57-community-release-el7-8.noarch.rpm: mysql57-community-release-el7-8.noarch
mysql57-community-release-el7-8.noarch.rpm 将被安装
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql57-community-release.noarch.0.el7-8 将被 安装
--> 解决依赖关系完成

依赖关系解决

===================================================================================================================================================================================================================
 Package                                                  架构                                  版本                                  源                                                                      大小
===================================================================================================================================================================================================================
正在安装:
 mysql57-community-release                                noarch                                el7-8                                 /mysql57-community-release-el7-8.noarch                                8.2 k

事务概要
===================================================================================================================================================================================================================
安装  1 软件包

总计:8.2 k
安装大小:8.2 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : mysql57-community-release-el7-8.noarch                                                                                                                                                         1/1 
  验证中      : mysql57-community-release-el7-8.noarch                                                                                                                                                         1/1 

已安装:
  mysql57-community-release.noarch 0:el7-8 

3、安装 MySQL 服务端

  1. 查看mysql server
[root@localhost opt]# yum search mysql-com
已加载插件:fastestmirror
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                                                                                                        | 3.6 kB  00:00:00     
docker-ce-stable                                                                                                                                                                            | 3.5 kB  00:00:00     
extras                                                                                                                                                                                      | 2.9 kB  00:00:00     
mysql-connectors-community                                                                                                                                                                  | 2.5 kB  00:00:00     
mysql-tools-community                                                                                                                                                                       | 2.5 kB  00:00:00     
mysql57-community                                                                                                                                                                           | 2.5 kB  00:00:00     
nodesource                                                                                                                                                                                  | 2.5 kB  00:00:00     
openresty                                                                                                                                                                                   | 2.9 kB  00:00:00     
updates                                                                                                                                                                                     | 2.9 kB  00:00:00     
(1/8): extras/7/x86_64/primary_db                                                                                                                                                           | 152 kB  00:00:00     
(2/8): base/7/x86_64/group_gz                                                                                                                                                               | 165 kB  00:00:00     
(3/8): mysql-tools-community/x86_64/primary_db                                                                                                                                              |  61 kB  00:00:00     
(4/8): mysql-connectors-community/x86_64/primary_db                                                                                                                                         |  44 kB  00:00:00     
(5/8): updates/7/x86_64/primary_db                                                                                                                                                          | 1.1 MB  00:00:00     
(6/8): base/7/x86_64/primary_db                                                                                                                                                             | 6.0 MB  00:00:01     
(7/8): openresty/7/x86_64/primary_db                                                                                                                                                        |  33 kB  00:00:06     
(8/8): mysql57-community/x86_64/primary_db                                                                                                                                                  | 184 kB  00:00:07     
============================================================================================= N/S matched: mysql-com ==============================================================================================
mysql-community-client.i686 : MySQL database client applications and tools
mysql-community-client.x86_64 : MySQL database client applications and tools
mysql-community-common.i686 : MySQL database common files for server and client libs
mysql-community-common.x86_64 : MySQL database common files for server and client libs
mysql-community-devel.i686 : Development header files and libraries for MySQL database client applications
mysql-community-devel.x86_64 : Development header files and libraries for MySQL database client applications
mysql-community-embedded.i686 : MySQL embedded library
mysql-community-embedded.x86_64 : MySQL embedded library
mysql-community-embedded-compat.i686 : MySQL embedded compat library
mysql-community-embedded-compat.x86_64 : MySQL embedded compat library
mysql-community-embedded-devel.i686 : Development header files and libraries for MySQL as an embeddable library
mysql-community-embedded-devel.x86_64 : Development header files and libraries for MySQL as an embeddable library
mysql-community-libs.i686 : Shared libraries for MySQL database client applications
mysql-community-libs.x86_64 : Shared libraries for MySQL database client applications
mysql-community-libs-compat.i686 : Shared compat libraries for MySQL 5.6.25 database client applications
mysql-community-libs-compat.x86_64 : Shared compat libraries for MySQL 5.6.37 database client applications
mysql-community-release.noarch : MySQL repository configuration for yum
mysql-community-server.x86_64 : A very fast and reliable SQL database server
mysql-community-test.x86_64 : Test suite for the MySQL database server

  名称和简介匹配 only,使用“search all”试试。

  1. 安装
[root@localhost opt]# yum -y install mysql-community-server.x86_64
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql-community-server.x86_64.0.5.7.27-1.el7 将被 安装
--> 正在处理依赖关系 mysql-community-common(x86-64) = 5.7.27-1.el7,它被软件包 mysql-community-server-5.7.27-1.el7.x86_64 需要
--> 正在处理依赖关系 mysql-community-client(x86-64) >= 5.7.9,它被软件包 mysql-community-server-5.7.27-1.el7.x86_64 需要
--> 正在检查事务
---> 软件包 mysql-community-client.x86_64.0.5.7.27-1.el7 将被 安装
--> 正在处理依赖关系 mysql-community-libs(x86-64) >= 5.7.9,它被软件包 mysql-community-client-5.7.27-1.el7.x86_64 需要
---> 软件包 mysql-community-common.x86_64.0.5.7.27-1.el7 将被 安装
--> 正在检查事务
---> 软件包 mariadb-libs.x86_64.1.5.5.56-2.el7 将被 取代
--> 正在处理依赖关系 libmysqlclient.so.18()(64bit),它被软件包 perl-DBD-MySQL-4.023-6.el7.x86_64 需要
--> 正在处理依赖关系 libmysqlclient.so.18()(64bit),它被软件包 2:postfix-2.10.1-6.el7.x86_64 需要
--> 正在处理依赖关系 libmysqlclient.so.18(libmysqlclient_18)(64bit),它被软件包 perl-DBD-MySQL-4.023-6.el7.x86_64 需要
--> 正在处理依赖关系 libmysqlclient.so.18(libmysqlclient_18)(64bit),它被软件包 2:postfix-2.10.1-6.el7.x86_64 需要
---> 软件包 mysql-community-libs.x86_64.0.5.7.27-1.el7 将被 舍弃
--> 正在检查事务
---> 软件包 mysql-community-libs-compat.x86_64.0.5.7.27-1.el7 将被 舍弃
---> 软件包 postfix.x86_64.2.2.10.1-6.el7 将被 升级
---> 软件包 postfix.x86_64.2.2.10.1-7.el7 将被 更新
--> 解决依赖关系完成

依赖关系解决

===================================================================================================================================================================================================================
 Package                                                       架构                                     版本                                             源                                                   大小
===================================================================================================================================================================================================================
正在安装:
 mysql-community-libs                                          x86_64                                   5.7.27-1.el7                                     mysql57-community                                   2.2 M
      替换  mariadb-libs.x86_64 1:5.5.56-2.el7
 mysql-community-libs-compat                                   x86_64                                   5.7.27-1.el7                                     mysql57-community                                   2.0 M
      替换  mariadb-libs.x86_64 1:5.5.56-2.el7
 mysql-community-server                                        x86_64                                   5.7.27-1.el7                                     mysql57-community                                   165 M
为依赖而安装:
 mysql-community-client                                        x86_64                                   5.7.27-1.el7                                     mysql57-community                                    24 M
 mysql-community-common                                        x86_64                                   5.7.27-1.el7                                     mysql57-community                                   275 k
为依赖而更新:
 postfix                                                       x86_64                                   2:2.10.1-7.el7                                   base                                                2.4 M

事务概要
===================================================================================================================================================================================================================
安装  3 软件包 (+2 依赖软件包)
升级           ( 1 依赖软件包)

总下载量:197 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
警告:/var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.27-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY                        ] 243 kB/s | 261 kB  00:13:46 ETA 
mysql-community-common-5.7.27-1.el7.x86_64.rpm 的公钥尚未安装
(1/6): mysql-community-common-5.7.27-1.el7.x86_64.rpm                                                                                                                                       | 275 kB  00:00:01     
(2/6): mysql-community-libs-5.7.27-1.el7.x86_64.rpm                                                                                                                                         | 2.2 MB  00:00:03     
(3/6): mysql-community-libs-compat-5.7.27-1.el7.x86_64.rpm                                                                                                                                  | 2.0 MB  00:00:03     
(4/6): mysql-community-client-5.7.27-1.el7.x86_64.rpm                                                                                                                                       |  24 MB  00:00:13     
(5/6): postfix-2.10.1-7.el7.x86_64.rpm                                                                                                                                                      | 2.4 MB  00:00:10     
(6/6): mysql-community-server-5.7.27-1.el7.x86_64.rpm                                                                                                                                       | 165 MB  00:02:11     
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                                                                               1.4 MB/s | 197 MB  00:02:20     
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥
导入 GPG key 0x5072E1F5:
 用户ID     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 指纹       : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 软件包     : mysql57-community-release-el7-8.noarch (installed)
 来自       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : mysql-community-common-5.7.27-1.el7.x86_64                                                                                                                                                     1/8 
  正在安装    : mysql-community-libs-5.7.27-1.el7.x86_64                                                                                                                                                       2/8 
  正在安装    : mysql-community-libs-compat-5.7.27-1.el7.x86_64                                                                                                                                                3/8 
  正在安装    : mysql-community-client-5.7.27-1.el7.x86_64                                                                                                                                                     4/8 
  正在安装    : mysql-community-server-5.7.27-1.el7.x86_64                                                                                                                                                     5/8 
  正在更新    : 2:postfix-2.10.1-7.el7.x86_64                                                                                                                                                                  6/8 
  清理        : 2:postfix-2.10.1-6.el7.x86_64                                                                                                                                                                  7/8 
  正在删除    : 1:mariadb-libs-5.5.56-2.el7.x86_64                                                                                                                                                             8/8 
  验证中      : mysql-community-libs-compat-5.7.27-1.el7.x86_64                                                                                                                                                1/8 
  验证中      : mysql-community-common-5.7.27-1.el7.x86_64                                                                                                                                                     2/8 
  验证中      : 2:postfix-2.10.1-7.el7.x86_64                                                                                                                                                                  3/8 
  验证中      : mysql-community-server-5.7.27-1.el7.x86_64                                                                                                                                                     4/8 
  验证中      : mysql-community-client-5.7.27-1.el7.x86_64                                                                                                                                                     5/8 
  验证中      : mysql-community-libs-5.7.27-1.el7.x86_64                                                                                                                                                       6/8 
  验证中      : 1:mariadb-libs-5.5.56-2.el7.x86_64                                                                                                                                                             7/8 
  验证中      : 2:postfix-2.10.1-6.el7.x86_64                                                                                                                                                                  8/8 

已安装:
  mysql-community-libs.x86_64 0:5.7.27-1.el7                         mysql-community-libs-compat.x86_64 0:5.7.27-1.el7                         mysql-community-server.x86_64 0:5.7.27-1.el7                        

作为依赖被安装:
  mysql-community-client.x86_64 0:5.7.27-1.el7                                                             mysql-community-common.x86_64 0:5.7.27-1.el7                                                            

作为依赖被升级:
  postfix.x86_64 2:2.10.1-7.el7                                                                                                                                                                                    

替代:
  mariadb-libs.x86_64 1:5.5.56-2.el7                                                                                                                                                                               

完毕!

4. 启动 mysql 服务

[root@localhost opt]# systemctl start mysqld.service

5.查看启动结果

[root@localhost opt]# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2019-09-18 14:52:47 CST; 6s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 29507 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 29429 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 29510 (mysqld)
    Tasks: 27
   Memory: 324.4M
   CGroup: /system.slice/mysqld.service
           └─29510 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

9月 18 14:52:40 localhost.localdomain systemd[1]: Starting MySQL Server...
9月 18 14:52:47 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost opt]#  cat /var/log/mysqld.log | grep 'password'
2019-09-18T06:52:43.864287Z 1 [Note] A temporary password is generated for root@localhost: MZssaq?zI2sD

6.登录数据库

这里注意一点,MySQL5.7.6 之后会在启动 mysql 进程的时候生成一个用户密码,首次登陆需要这个密码才行。密码保存在 mysql 进程的日志里,即(/var/log/mysqld.log)

查看登陆密码

[root@localhost opt]#  cat /var/log/mysqld.log | grep 'password'
2019-09-18T06:52:43.864287Z 1 [Note] A temporary password is generated for root@localhost: MZssaq?zI2sD

登录操作

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

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> 

7.修改root密码

但是如果想正常使用还需要修改密码,这里我们注意设置密码 长度要大于8,同时要有数字,大小写,特殊字符。

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '!@#Yanbo19961996';
Query OK, 0 rows affected (0.01 sec)

以上安装步骤借鉴了 https://my.oschina.net/Laily/blog/713023 这篇文章。谢谢该作者。

8.允许远程连接mysql

安装完成后,我们通过本地(windows)使用navicat连接mysql,报错:

远程连接MYSQL提示Host is not allowed to connect to this MySQL server

解决方法: https://blog.csdn.net/ei__nino/article/details/25069391

1。 改表法。可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从"localhost"改称"%"

mysql -u root -pvmwaremysql>use mysql;
mysql>update user set host = ‘%’ where user = ‘root’;
mysql>select host, user from user;

  1. 授权法。例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话。

GRANT ALL PRIVILEGES ON . TO ‘myuser’@’%’ IDENTIFIED BY ‘mypassword’ WITH GRANT OPTION;
如果你想允许用户myuser从ip为192.168.1.3的主机连接到mysql服务器,并使用mypassword作为密码

GRANT ALL PRIVILEGES ON . TO ‘root’@‘192.168.1.3’ IDENTIFIED BY ‘mypassword’ WITH GRANT OPTION;

GRANT ALL PRIVILEGES ON . TO ‘root’@‘192.168.1.3’ IDENTIFIED BY ‘1235’ WITH GRANT OPTION;

mysql>flush privileges; 这句一定要加上!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值