centos7 mysql 小破天_centos7下安装mysql

本文档详细介绍了在 CentOS7 系统中如何安装 MySQL 5.7,包括下载 MySQL YUM 安装包,使用 YUM 安装,启动 MySQL 服务,修改 root 用户默认密码,设置开机启动,以及配置远程访问和默认编码为 UTF8。在安装过程中,还涉及到了密码策略的检查和修改。

前言:mysql官网上yum的位置

http://dev.mysql.com/downloads/repo/yum/

1.检查centos系统中是否有mysql的yum安装包

[root@localhost src]# yum list *mysql*

已加载插件:fastestmirror, langpacks

Loading mirror speeds from cached hostfile

* base: centos.ustc.edu.cn

* extras: centos.ustc.edu.cn

* updates: centos.ustc.edu.cn

错误:没有匹配的软件包可以列出

[root@localhost src]#

2.下载mysql安装包

[root@localhost src]# wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

--2017-02-25 07:01:34--  https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

正在解析主机 dev.mysql.com (dev.mysql.com)... 137.254.60.11

正在连接 dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... 已连接。

已发出 HTTP 请求,正在等待回应... 302 Found

位置:https://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpm [跟随至新的 URL]

--2017-02-25 07:01:36--  https://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpm

正在解析主机 repo.mysql.com (repo.mysql.com)... 104.88.152.154

正在连接 repo.mysql.com (repo.mysql.com)|104.88.152.154|:443... 已连接。

已发出 HTTP 请求,正在等待回应... 200 OK

长度:9224 (9.0K) [application/x-redhat-package-manager]

正在保存至: “mysql57-community-release-el7-9.noarch.rpm”

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

2017-02-25 07:01:36 (72.9 MB/s) - 已保存 “mysql57-community-release-el7-9.noarch.rpm” [9224/9224])

3.用yum命令安装mysql

进入到包含有mysql安装包的路径

[root@localhost src]# ls

mysql57-community-release-el7-9.noarch.rpm

[root@localhost src]# yum localinstall mysql57-community-release-el7-9.noarch.rpm

已加载插件:fastestmirror, langpacks

正在检查 mysql57-community-release-el7-9.noarch.rpm: mysql57-community-release-el7-9.noarch

mysql57-community-release-el7-9.noarch.rpm 将被安装

正在解决依赖关系

--> 正在检查事务

---> 软件包 mysql57-community-release.noarch.0.el7-9 将被 安装

--> 解决依赖关系完成

base/7/x86_64                                            | 3.6 kB     00:00

extras/7/x86_64                                          | 3.4 kB     00:00

extras/7/x86_64/primary_db                               | 122 kB     00:00

updates/7/x86_64                                         | 3.4 kB     00:00

updates/7/x86_64/primary_db                              | 2.9 MB     00:02

依赖关系解决

================================================================================

Package             架构   版本  源                                       大小

================================================================================

正在安装:

mysql57-community-release

noarch el7-9 /mysql57-community-release-el7-9.noarch 8.6 k

事务概要

================================================================================

安装  1 软件包

总计:8.6 k

安装大小:8.6 k

Is this ok [y/d/N]: y

Downloading packages:

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

警告:RPM 数据库已被非 yum 程序修改。

** 发现 7 个已存在的 RPM 数据库问题, 'yum check' 输出如下:

icedtea-web-1.6.2-4.el7.x86_64 有缺少的需求 java-1.8.0-openjdk

ipa-client-4.4.0-12.el7.centos.x86_64 有已安装冲突 freeipa-client: ipa-client-4.4.0-12.el7.centos.x86_64

ipa-client-common-4.4.0-12.el7.centos.noarch 有已安装冲突 freeipa-client-common: ipa-client-common-4.4.0-12.el7.centos.noarch

ipa-common-4.4.0-12.el7.centos.noarch 有已安装冲突 freeipa-common: ipa-common-4.4.0-12.el7.centos.noarch

jline-1.0-8.el7.noarch 有缺少的需求 java >= ('0', '1.5', None)

1:libreoffice-core-5.0.6.2-3.el7.x86_64 有缺少的需求 java-headless >= ('1', '1.6', None)

1:libreoffice-ure-5.0.6.2-3.el7.x86_64 有缺少的需求 libjvm.so()(64bit)

正在安装    : mysql57-community-release-el7-9.noarch                      1/1

验证中      : mysql57-community-release-el7-9.noarch                      1/1

已安装:

mysql57-community-release.noarch 0:el7-9

完毕!

4 检查mysql源是否安装成功,如下截图显示mysql源安装成功

[root@localhost src]# yum repolist enabled |grep "mysql.*-community.*"

mysql-connectors-community/x86_64        MySQL Connectors Community           3

mysql-tools-community/x86_64             MySQL Tools Community                4

mysql57-community/x86_64                 MySQL 5.7 Community Server          16

[root@localhost src]#

5 安装mysql

[root@localhost src]# yum install mysql-community-server

已加载插件:fastestmirror, langpacks

Loading mirror speeds from cached hostfile

* base: centos.ustc.edu.cn

* extras: centos.ustc.edu.cn

* updates: centos.ustc.edu.cn

正在解决依赖关系

--> 正在检查事务

---> 软件包 mysql-community-server.x86_64.0.5.7.17-1.el7 将被 安装

--> 正在处理依赖关系 mysql-community-common(x86-64) = 5.7.17-1.el7,它被软件包 mysql-community-server-5.7.17-1.el7.x86_64 需要

--> 正在处理依赖关系 mysql-community-client(x86-64) >= 5.7.9,它被软件包 mysql-community-server-5.7.17-1.el7.x86_64 需要

--> 正在检查事务

---> 软件包 mysql-community-client.x86_64.0.5.7.17-1.el7 将被 安装

--> 正在处理依赖关系 mysql-community-libs(x86-64) >= 5.7.9,它被软件包 mysql-community-client-5.7.17-1.el7.x86_64 需要

---> 软件包 mysql-community-common.x86_64.0.5.7.17-1.el7 将被 安装

--> 正在检查事务

---> 软件包 mariadb-libs.x86_64.1.5.5.52-1.el7 将被 取代

--> 正在处理依赖关系 libmysqlclient.so.18()(64bit),它被软件包 2:postfix-2.10.1-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.17-1.el7 将被 舍弃

--> 正在检查事务

---> 软件包 mysql-community-libs-compat.x86_64.0.5.7.17-1.el7 将被 舍弃

--> 解决依赖关系完成

依赖关系解决

======================================================================================================================================================

Package                                        架构                      版本                             源                                    大小

======================================================================================================================================================

正在安装:

mysql-community-libs                           x86_64                    5.7.17-1.el7                     mysql57-community                    2.1 M

替换  mariadb-libs.x86_64 1:5.5.52-1.el7

mysql-community-libs-compat                    x86_64                    5.7.17-1.el7                     mysql57-community                    2.0 M

替换  mariadb-libs.x86_64 1:5.5.52-1.el7

mysql-community-server                         x86_64                    5.7.17-1.el7                     mysql57-community                    162 M

为依赖而安装:

mysql-community-client                         x86_64                    5.7.17-1.el7                     mysql57-community                     24 M

mysql-community-common                         x86_64                    5.7.17-1.el7                     mysql57-community                    271 k

事务概要

======================================================================================================================================================

安装  3 软件包 (+2 依赖软件包)

总下载量:190 M

Is this ok [y/d/N]: y

Downloading packages:

警告:/var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.17-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY

mysql-community-common-5.7.17-1.el7.x86_64.rpm 的公钥尚未安装

(1/5): mysql-community-common-5.7.17-1.el7.x86_64.rpm                                                                          | 271 kB  00:00:00

(2/5): mysql-community-libs-5.7.17-1.el7.x86_64.rpm                                                                            | 2.1 MB  00:00:03

(3/5): mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm                                                                     | 2.0 MB  00:00:02

(4/5): mysql-community-client-5.7.17-1.el7.x86_64.rpm                                                                          |  24 MB  00:00:29

(5/5): mysql-community-server-5.7.17-1.el7.x86_64.rpm                                                                          | 162 MB  00:01:23

------------------------------------------------------------------------------------------------------------------------------------------------------

总计                                                                                                                  2.1 MB/s | 190 MB  00:01:29

从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥

导入 GPG key 0x5072E1F5:

用户ID     : "MySQL Release Engineering "

指纹       : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5

软件包     : mysql57-community-release-el7-9.noarch (installed)

来自       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

是否继续?[y/N]:y

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

正在安装    : mysql-community-common-5.7.17-1.el7.x86_64                                                                                        1/6

正在安装    : mysql-community-libs-5.7.17-1.el7.x86_64                                                                                          2/6

正在安装    : mysql-community-client-5.7.17-1.el7.x86_64                                                                                        3/6

正在安装    : mysql-community-server-5.7.17-1.el7.x86_64                                                                                        4/6

正在安装    : mysql-community-libs-compat-5.7.17-1.el7.x86_64                                                                                   5/6

正在删除    : 1:mariadb-libs-5.5.52-1.el7.x86_64                                                                                                6/6

验证中      : mysql-community-common-5.7.17-1.el7.x86_64                                                                                        1/6

验证中      : mysql-community-server-5.7.17-1.el7.x86_64                                                                                        2/6

验证中      : mysql-community-libs-5.7.17-1.el7.x86_64                                                                                          3/6

验证中      : mysql-community-libs-compat-5.7.17-1.el7.x86_64                                                                                   4/6

验证中      : mysql-community-client-5.7.17-1.el7.x86_64                                                                                        5/6

验证中      : 1:mariadb-libs-5.5.52-1.el7.x86_64                                                                                                6/6

已安装:

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

作为依赖被安装:

mysql-community-client.x86_64 0:5.7.17-1.el7                              mysql-community-common.x86_64 0:5.7.17-1.el7

替代:

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

完毕!

[root@localhost src]#

6 启动mysql服务(这个命令有点慢,大概2-3分钟的样子。。。,启动时候屏幕没有提示信息)

[root@localhost src]# systemctl start mysqld

[root@localhost src]#

7 查看MySQL的启动状态(显示下面的信息,表示mysql启动成功)

[root@localhost src]# systemctl status mysqld

● mysqld.service - MySQL Server

Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)

Active: active (running) since 六 2017-02-25 08:01:43 CST; 1min 41s ago

Docs: man:mysqld(8)

http://dev.mysql.com/doc/refman/en/using-systemd.html

Process: 6228 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)

Process: 6139 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)

Main PID: 6230 (mysqld)

CGroup: /system.slice/mysqld.service

└─6230 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

2月 25 08:01:12 localhost systemd[1]: Starting MySQL Server...

2月 25 08:01:43 localhost systemd[1]: Started MySQL Server.

8 设置开机启动

shell> systemctl enable mysqld

shell> systemctl daemon-reload

9 修改root默认密码

9.1 mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改:

[root@localhost log]# grep 'temporary password' /var/log/mysqld.log

2017-02-25T00:01:29.530714Z 1 [Note] A temporary password is generated for root@localhost: +Fx;UZHzy6MI

9.2 在下面的Enter password 输入上面那个自动生成的密码 +Fx;UZHzy6MI,回车后出现mysql>提示符号

[root@localhost log]# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.7.17

Copyright (c) 2000, 2016, 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>

9.3 在mysql提示符号下,输入如下命令,修改mysql root用户密码,把密码修改为Root123456!@ (默认密码策略要求:字母,数字,特殊字符,字母必须把含有大写的)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root123456!@';

9.4 注意:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误,如下图所示:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

9.5 通过环境变量查看密码策略信息

validate_password_policy              | MEDIUM 表示为当前使用的是中级密码策略

mysql> show variables like '%password%';

+---------------------------------------+--------+

| Variable_name                         | Value  |

+---------------------------------------+--------+

| default_password_lifetime             | 0      |

| disconnect_on_expired_password        | ON     |

| log_builtin_as_identified_by_password | OFF    |

| mysql_native_password_proxy_users     | OFF    |

| old_passwords                         | 0      |

| report_password                       |        |

| sha256_password_proxy_users           | OFF    |

| validate_password_check_user_name     | OFF    |

| 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      |

+---------------------------------------+--------+

14 rows in set (0.01 sec)

mysql>

validate_password_policy:密码策略,默认为MEDIUM策略  validate_password_dictionary_file:密码策略文件,策略为STRONG才需要  validate_password_length:密码最少长度  validate_password_mixed_case_count:大小写字符长度,至少1个  validate_password_number_count :数字至少1个  validate_password_special_char_count:特殊字符至少1个  上述参数是默认策略MEDIUM的密码检查规则。

共有以下几种密码策略:

策略     检查规则

0 or LOW     Length

1 or MEDIUM     Length; numeric, lowercase/uppercase, and special characters

2 or STRONG     Length; numeric, lowercase/uppercase, and special characters; dictionary file

MySQL官网密码策略详细说明:http://dev.mysql.com/doc/refman/5.7/en/validate-password-options-variables.html#sysvar_validate_password_policy

修改密码策略

在/etc/my.cnf文件添加validate_password_policy配置,指定密码策略

# 选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件

validate_password_policy=0

如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:

validate_password = off

重新启动mysql服务使配置生效:

systemctl restart mysqld

10 添加远程登陆用户

默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,我添加一个新的帐户:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'zhan'@'%' IDENTIFIED BY 'Zhan123456!@' WITH GRANT OPTION;

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>

11 配置默认编码为utf8

11.1 首先退出mysql  mysql> exit

11.2 修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示:

# For advice on how to change settings please see

# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]

character_set_server=utf8

init_connect='SET NAMES utf8'

#

# Remove leading # and set to the amount of RAM for the most important data

# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.

# innodb_buffer_pool_size = 128M

#

# Remove leading # to turn on a very important data integrity option: logging

# changes to the binary log between backups.

# log_bin

#

# Remove leading # to set options mainly useful for reporting servers.

# The server defaults are faster for transactions and fast SELECTs.

# Adjust sizes as needed, experiment to find the optimal values.

# join_buffer_size = 128M

# sort_buffer_size = 2M

# read_rnd_buffer_size = 2M

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

"/etc/my.cnf" 29L, 1016C

12 重新启动mysql服务,让11节设置的编码生效

[root@localhost log]# systemctl restart mysqld

[root@localhost log]#

13 重新登陆mysql

[root@localhost log]# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.7.17 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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>

14 查看mysql数据库编码

mysql> show variables like '%character%'

-> ;

+--------------------------+----------------------------+

| Variable_name            | Value                      |

+--------------------------+----------------------------+

| character_set_client     | utf8                       |

| character_set_connection | utf8                       |

| character_set_database   | utf8                       |

| character_set_filesystem | binary                     |

| character_set_results    | utf8                       |

| character_set_server     | utf8                       |

| character_set_system     | utf8                       |

| character_sets_dir       | /usr/share/mysql/charsets/ |

+--------------------------+----------------------------+

8 rows in set (0.01 sec)

mysql>

15 mysql默认配置文件路径

配置文件:/etc/my.cnf

日志文件:/var/log//var/log/mysqld.log

服务启动脚本:/usr/lib/systemd/system/mysqld.service

socket文件:/var/run/mysqld/mysqld.pid

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值