Centos 7 中yum安装Mysql和修改mysql配置

一. mysql安装

 

1.安装MySQL官方的Yum Repository

[root@ecs-7bec-0002 mysql]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
--2018-10-09 18:31:29--  http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm [following]
--2018-10-09 18:31:30--  https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm [following]
--2018-10-09 18:31:31--  https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.36.193.224
Connecting to repo.mysql.com (repo.mysql.com)|23.36.193.224|: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      --.-K/s   in 0.1s    

2018-10-09 18:31:32 (204 KB/s) - ‘mysql57-community-release-el7-10.noarch.rpm’ saved [25548/25548]

-c: No such file or directory
No URLs found in -c.
FINISHED --2018-10-09 18:31:32--
Total wall clock time: 3.3s
Downloaded: 1 files, 25K in 0.1s (204 KB/s)

 

 

2.下载所需要的rpm包

[root@ecs-7bec-0002 mysql]# yum -y install mysql57-community-release-el7-10.noarch.rpm
Loaded plugins: fastestmirror, langpacks
Examining mysql57-community-release-el7-10.noarch.rpm: mysql57-community-release-el7-10.noarch
Marking mysql57-community-release-el7-10.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql57-community-release.noarch 0:el7-10 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

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

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

Total size: 30 k
Installed size: 30 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql57-community-release-el7-10.noarch                                                                                                                                                                      1/1 
  Verifying  : mysql57-community-release-el7-10.noarch                                                                                                                                                                      1/1 

Installed:
  mysql57-community-release.noarch 0:el7-10                                                                                                                                                                                     

Complete!
[root@ecs-7bec-0002 mysql]#

 

 

3.安装MySQL所需要的服务,这一步执行的时间比较长

如果中间需要输入y/n,则输入y继续执行

[root@ecs-7bec-0002 mysql]# yum -y install mysql-community-server
Loaded plugins: fastestmirror, langpacks
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     
(1/3): mysql-tools-community/x86_64/primary_db                                                                                                                                                           |  45 kB  00:00:00     
(2/3): mysql-connectors-community/x86_64/primary_db                                                                                                                                                      |  26 kB  00:00:00     
(3/3): mysql57-community/x86_64/primary_db                                                                                                                                                               | 152 kB  00:00:00     
Loading mirror speeds from cached hostfile
 * base: 100.125.0.40
 * extras: 100.125.0.40
 * updates: 100.125.0.40
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.23-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.23-1.el7 for package: mysql-community-server-5.7.23-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.23-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.23-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.23-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.7.23-1.el7 will be installed
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.52-1.el7 will be obsoleted
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
---> Package mysql-community-libs.x86_64 0:5.7.23-1.el7 will be obsoleting
--> Running transaction check
---> Package mysql-community-libs-compat.x86_64 0:5.7.23-1.el7 will be obsoleting
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================================================================================================================
 Package                                                          Arch                                        Version                                              Repository                                              Size
================================================================================================================================================================================================================================
Installing:
 mysql-community-libs                                             x86_64                                      5.7.23-1.el7                                         mysql57-community                                      2.2 M
     replacing  mariadb-libs.x86_64 1:5.5.52-1.el7
 mysql-community-libs-compat                                      x86_64                                      5.7.23-1.el7                                         mysql57-community                                      2.0 M
     replacing  mariadb-libs.x86_64 1:5.5.52-1.el7
 mysql-community-server                                           x86_64                                      5.7.23-1.el7                                         mysql57-community                                      165 M
Installing for dependencies:
 mysql-community-client                                           x86_64                                      5.7.23-1.el7                                         mysql57-community                                       24 M
 mysql-community-common                                           x86_64                                      5.7.23-1.el7                                         mysql57-community                                      274 k

Transaction Summary
================================================================================================================================================================================================================================
Install  3 Packages (+2 Dependent packages)

Total download size: 194 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.23-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY                              ]  0.0 B/s |  70 kB  --:--:-- ETA 
Public key for mysql-community-common-5.7.23-1.el7.x86_64.rpm is not installed
(1/5): mysql-community-common-5.7.23-1.el7.x86_64.rpm                                                                                                                                                    | 274 kB  00:00:01     
(2/5): mysql-community-libs-5.7.23-1.el7.x86_64.rpm                                                                                                                                                      | 2.2 MB  00:00:00     
(3/5): mysql-community-libs-compat-5.7.23-1.el7.x86_64.rpm                                                                                                                                               | 2.0 MB  00:00:00     
(4/5): mysql-community-client-5.7.23-1.el7.x86_64.rpm                                                                                                                                                    |  24 MB  00:00:04     
(5/5): mysql-community-server-5.7.23-1.el7.x86_64.rpm                                                                                                                                                    | 165 MB  00:00:20     
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                           8.4 MB/s | 194 MB  00:00:22     
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    : mysql57-community-release-el7-10.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql-community-common-5.7.23-1.el7.x86_64                                                                                                                                                                   1/6 
  Installing : mysql-community-libs-5.7.23-1.el7.x86_64                                                                                                                                                                     2/6 
  Installing : mysql-community-client-5.7.23-1.el7.x86_64                                                                                                                                                                   3/6 
  Installing : mysql-community-server-5.7.23-1.el7.x86_64                                                                                                                                                                   4/6 
  Installing : mysql-community-libs-compat-5.7.23-1.el7.x86_64                                                                                                                                                              5/6 
  Erasing    : 1:mariadb-libs-5.5.52-1.el7.x86_64                                                                                                                                                                           6/6 
  Verifying  : mysql-community-server-5.7.23-1.el7.x86_64                                                                                                                                                                   1/6 
  Verifying  : mysql-community-libs-5.7.23-1.el7.x86_64                                                                                                                                                                     2/6 
  Verifying  : mysql-community-client-5.7.23-1.el7.x86_64                                                                                                                                                                   3/6 
  Verifying  : mysql-community-libs-compat-5.7.23-1.el7.x86_64                                                                                                                                                              4/6 
  Verifying  : mysql-community-common-5.7.23-1.el7.x86_64                                                                                                                                                                   5/6 
  Verifying  : 1:mariadb-libs-5.5.52-1.el7.x86_64                                                                                                                                                                           6/6 

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

Dependency Installed:
  mysql-community-client.x86_64 0:5.7.23-1.el7                                                                   mysql-community-common.x86_64 0:5.7.23-1.el7                                                                  

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

Complete!
[root@ecs-7bec-0002 mysql]#

 

 

4.启动mysql服务

[root@ecs-7bec-0002 mysql]# service mysqld start    //启动命令
[root@ecs-7bec-0002 mysql]# service mysqld status    //查看服务状态
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-10-09 19:05:20 CST; 2min 42s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 5868 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 5763 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 5871 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─5871 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Oct 09 19:04:54 ecs-7bec-0002 systemd[1]: Starting MySQL Server...
Oct 09 19:05:20 ecs-7bec-0002 systemd[1]: Started MySQL Server.
[root@ecs-7bec-0002 mysql]#

 

 

 

 

二. mysql基本配置

 

1.查看mysql中root用户的原始密码,每次安装mysl,系统就会生成一个随机密码。注意冒号后面的才是密码(红色部分

[root@ecs-7bec-0002 ~]# grep 'temporary password' /var/log/mysqld.log
2018-10-09T11:05:15.914001Z 1 [Note] A temporary password is generated for root@localhost: )o=cCvgXJ0%1

 

 

 

2.登入root用户,并在回车后输入密码

 

[root@ecs-7bec-0002 ~]# mysql -uroot -p
Enter password: 

 

 

 

 

3.我们需要修改下root的密码,但是mysql对密码有要求,我们需要修改两个全局参数: 

  validate_password_policy: 

    •   1:符合长度,且必须含有数字,小写或大写字母,特殊字符。
    •   0:判断密码的标准就基于密码的长度了。

  validate_password_length:代表密码长度,最小值为4

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

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

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

mysql> 

 

 

 

三. mysql远程配置

 

1.数据库安装完毕,密码重置成功,下面是在本地远程连接在服务器中的mysql

  此命名的意思是:

  •     使某个ip的账户已用户名:user,密码:pwd连接服务器中的mysql。
  •     执行此命令会自动创建不存在的账户和其密码

 

user:可以直接使用root账户,也可以创建一个账户;
ip:可以是一个具体的ip,也可以用“%”代替,%代表的是所有的IP地址
pwd:新创建一个密码,不建议直接使用root的密码
GRANT ALL PRIVILEGES ON *.* TO 'user'@'ip' IDENTIFIED BY 'pwd' WITH GRANT OPTION;
FLUSH  PRIVILEGES;

使用此命令刷新权限,使上一步的配置生效

FLUSH PRIVILEGES

 

 

如果此时远程登录还是不成功,请检查服务器3306是否对外开放,检查防火墙是对外开放的端口是否有3306

防火墙端口检查和端口规则帮助:https://www.cnblogs.com/zhaosongbin/p/9765599.html

 

 

四. 其他

 

1.mysql常用文件路径:

  /etc/my.cnf   这是mysql的主配置文件

  /var/lib/mysql   mysql数据库的数据库文件存放位置

  /var/log mysql  数据库的日志输出存放位置

 

 2.mysql基本命令:

  service mysqld start    启动mysql服务

  service mysqld status    查看mysql服务状态

  service mysqld stop    关闭mysql服务

  service mysqld restart    重启mysql服务

 

 参考:

  https://blog.csdn.net/qq_38663729/article/details/79327305

  https://blog.csdn.net/babycan5/article/details/81950096

posted @ 2018-10-12 16:07  莫忘丶初心  阅读(5406)  评论(0编辑  收藏  举报