CentOS7 安装 MySQL 和简单优化

本贴最后更新于 1940 天前,其中的信息可能已经渤澥桑田

CentOS7 安装 MySQL 和简单优化

本篇文章主要记录在 CentOS7 下通过 rpm 安装 MySQL,并且进行简单的配置优化。

[0x0]环境及其软件介绍

  • 操作系统:CentOS7.0 纯净版
  • MySQL 版本:5.7.16

[0x1]关闭 SELinux

SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux 历史上最杰出的新安全子系统。在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。SELinux 默认安装在 Fedora 和 Red Hat Enterprise Linux 上。虽然 SELinux 很好用,但是在多数情况我们还是将其关闭,因为在不了解其机制的情况下使用 SELinux 会导致软件安装或者应用部署失败。
一般如果使用云服务器,已经默认关闭了该服务,如果是虚拟机自己安装 CentOS 的话,那么需要自己手动关闭。如果不关闭,则影响后面的操作。

  1. 查看 selinux 的模式
[root@virtual data]# getenforce
Enforcing
[root@virtual data]#
  1. 关闭 selinux
    • 永久性关闭
      永久性关闭则需要重启服务器后生效
    # vim /etc/selinux/config    #编辑这个文件,下面修改的内容
    #============================================================
    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #     enforcing - SELinux security policy is enforced.
    #     permissive - SELinux prints warnings instead of enforcing.
    #     disabled - No SELinux policy is loaded.
    #SELINUX=enforcing #这个是系统默认的
    SELINUX=disabled   #这是自己修改的,表示禁用该服务
    # SELINUXTYPE= can take one of three two values:
    #     targeted - Targeted processes are protected,
    #     minimum - Modification of targeted policy. Only selected processes are protected.
    #     mls - Multi Level Security protection.
    SELINUXTYPE=targeted
    
    保存文件,然后重启服务器吧。
    • 临时性关闭
      临时性关闭仅对当前有效且立即生效,重启服务器之后无效。
      setenforce 0 命令表示设置 selinux 为 permissive 模式(即关闭);
      setenforce 1 命令表示设置 selinux 为 enforcing 模式(即开启)

[0x2]下载 MySQL

MySQL 的官网下载地址是 http://dev.mysql.com/downloads/mysql/,选择 Red Hat Enterprise Linux / Oracle Linux(因为 CentOS 属于 RHEL 的一个分支),如下图所示
MySQL-01.png

然后下面选择 CentOS7 的全家桶,选择全家桶比较方便,当安装的时候如果提示缺少包,一般可以在全家桶里直接找到的。
MySQL-Packages-CentOS7.png
老套路,可以直接到微云下载(密码:yv8Fib)。
将下载成功 mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar 上传到您的服务器,下面就准备开始安装了。

[0x3]安装 MySQL

首先将上传的 MySQL mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar 解压出来。

[root@virtual mysql]# ls
mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar
[root@virtual mysql]# tar xvf mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar
mysql-community-libs-compat-5.7.16-1.el7.x86_64.rpm
mysql-community-devel-5.7.16-1.el7.x86_64.rpm
mysql-community-minimal-debuginfo-5.7.16-1.el7.x86_64.rpm
mysql-community-libs-5.7.16-1.el7.x86_64.rpm
mysql-community-common-5.7.16-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.16-1.el7.x86_64.rpm
mysql-community-test-5.7.16-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.16-1.el7.x86_64.rpm
mysql-community-server-minimal-5.7.16-1.el7.x86_64.rpm
mysql-community-server-5.7.16-1.el7.x86_64.rpm
mysql-community-client-5.7.16-1.el7.x86_64.rpm
mysql-community-embedded-5.7.16-1.el7.x86_64.rpm
[root@virtual mysql]#

安装 MySQL 数据库的服务器版本,即 mysql-community-server-5.7.16-1.el7.x86_64.rpm,安装命令这里使用 yum localinstall,该命令会自动分析软件依赖包,非常的方便。

[root@virtual mysql]# yum localinstall mysql-community-server-
mysql-community-server-5.7.16-1.el7.x86_64.rpm          mysql-community-server-minimal-5.7.16-1.el7.x86_64.rpm
[root@virtual mysql]# yum localinstall mysql-community-server-5.7.16-1.el7.x86_64.rpm
Loaded plugins: fastestmirror
Examining mysql-community-server-5.7.16-1.el7.x86_64.rpm: mysql-community-server-5.7.16-1.el7.x86_64
Marking mysql-community-server-5.7.16-1.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.16-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.16-1.el7 for package: mysql-community-server-5.7.16-1.el7.x86_64
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.yun-idc.com
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.16-1.el7.x86_64
--> Processing Dependency: net-tools for package: mysql-community-server-5.7.16-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.16-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.16-1.el7 for package: mysql-community-server-5.7.16-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.16-1.el7.x86_64
---> Package net-tools.x86_64 0:2.0-0.17.20131004git.el7 will be installed
--> Finished Dependency Resolution
Error: Package: mysql-community-server-5.7.16-1.el7.x86_64 (/mysql-community-server-5.7.16-1.el7.x86_64)
           Requires: mysql-community-common(x86-64) = 5.7.16-1.el7
Error: Package: mysql-community-server-5.7.16-1.el7.x86_64 (/mysql-community-server-5.7.16-1.el7.x86_64)
           Requires: mysql-community-client(x86-64) >= 5.7.9
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest
[root@virtual mysql]#

根据上面的错误信息可以知道,安装 MySQL 服务器 mysql-community-server-5.7.16-1.el7.x86_64.rpm 需要先安装 mysql-community-client(x86-64) 软件,这个时候体现出全家桶的好处,发现全家桶里面带有这个软件,所以直接先安装这个软件即可。

[root@virtual mysql]# yum localinstall mysql-community-client-5.7.16-1.el7.x86_64.rpm
Loaded plugins: fastestmirror
Examining mysql-community-client-5.7.16-1.el7.x86_64.rpm: mysql-community-client-5.7.16-1.el7.x86_64
Marking mysql-community-client-5.7.16-1.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.16-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.16-1.el7.x86_64
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.yun-idc.com
--> Finished Dependency Resolution
Error: Package: mysql-community-client-5.7.16-1.el7.x86_64 (/mysql-community-client-5.7.16-1.el7.x86_64)
           Requires: mysql-community-libs(x86-64) >= 5.7.9
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest
[root@virtual mysql]#

套路出来了,根据错误信息,很显然先安装 mysql-community-libs(x86-64),走起:

[root@virtual mysql]# yum localinstall mysql-community-libs-
mysql-community-libs-5.7.16-1.el7.x86_64.rpm         mysql-community-libs-compat-5.7.16-1.el7.x86_64.rpm
[root@virtual mysql]# yum localinstall mysql-community-libs-5.7.16-1.el7.x86_64.rpm
Loaded plugins: fastestmirror
Examining mysql-community-libs-5.7.16-1.el7.x86_64.rpm: mysql-community-libs-5.7.16-1.el7.x86_64
Marking mysql-community-libs-5.7.16-1.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.44-2.el7.centos will be obsoleted
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.yun-idc.com
--> 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.16-1.el7 will be obsoleting
--> Processing Dependency: mysql-community-common(x86-64) >= 5.7.9 for package: mysql-community-libs-5.7.16-1.el7.x86_64
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.44-2.el7.centos will be updated
---> Package mariadb-libs.x86_64 1:5.5.50-1.el7_2 will be an update
---> Package mysql-community-libs.x86_64 0:5.7.16-1.el7 will be obsoleting
--> Processing Dependency: mysql-community-common(x86-64) >= 5.7.9 for package: mysql-community-libs-5.7.16-1.el7.x86_64
Removing mariadb-libs.x86_64 1:5.5.50-1.el7_2 - u due to obsoletes from mysql-community-libs.x86_64 0:5.7.16-1.el7 - u
--> Restarting Dependency Resolution with new changes.
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.50-1.el7_2 will be an update
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
---> Package mysql-community-libs.x86_64 0:5.7.16-1.el7 will be obsoleting
--> Processing Dependency: mysql-community-common(x86-64) >= 5.7.9 for package: mysql-community-libs-5.7.16-1.el7.x86_64
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.47-1.el7_2 will be an update
---> Package mysql-community-libs.x86_64 0:5.7.16-1.el7 will be obsoleting
--> Processing Dependency: mysql-community-common(x86-64) >= 5.7.9 for package: mysql-community-libs-5.7.16-1.el7.x86_64
Removing mariadb-libs.x86_64 1:5.5.47-1.el7_2 - u due to obsoletes from mysql-community-libs.x86_64 0:5.7.16-1.el7 - u
--> Restarting Dependency Resolution with new changes.
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.47-1.el7_2 will be an update
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64
---> Package mysql-community-libs.x86_64 0:5.7.16-1.el7 will be obsoleting
--> Processing Dependency: mysql-community-common(x86-64) >= 5.7.9 for package: mysql-community-libs-5.7.16-1.el7.x86_64
--> Finished Dependency Resolution
Error: Package: mysql-community-libs-5.7.16-1.el7.x86_64 (/mysql-community-libs-5.7.16-1.el7.x86_64)
           Requires: mysql-community-common(x86-64) >= 5.7.9
Error: Package: 2:postfix-2.10.1-6.el7.x86_64 (@anaconda)
           Requires: libmysqlclient.so.18(libmysqlclient_18)(64bit)
           Removing: 1:mariadb-libs-5.5.44-2.el7.centos.x86_64 (@anaconda)
               libmysqlclient.so.18(libmysqlclient_18)(64bit)
           Obsoleted By: mysql-community-libs-5.7.16-1.el7.x86_64 (/mysql-community-libs-5.7.16-1.el7.x86_64)
               Not found
           Updated By: 1:mariadb-libs-5.5.50-1.el7_2.x86_64 (updates)
               libmysqlclient.so.18(libmysqlclient_18)(64bit)
           Available: 1:mariadb-libs-5.5.47-1.el7_2.x86_64 (updates)
               libmysqlclient.so.18(libmysqlclient_18)(64bit)
Error: Package: 2:postfix-2.10.1-6.el7.x86_64 (@anaconda)
           Requires: libmysqlclient.so.18()(64bit)
           Removing: 1:mariadb-libs-5.5.44-2.el7.centos.x86_64 (@anaconda)
               libmysqlclient.so.18()(64bit)
           Obsoleted By: mysql-community-libs-5.7.16-1.el7.x86_64 (/mysql-community-libs-5.7.16-1.el7.x86_64)
              ~libmysqlclient.so.20()(64bit)
           Updated By: 1:mariadb-libs-5.5.50-1.el7_2.x86_64 (updates)
               libmysqlclient.so.18()(64bit)
           Available: 1:mariadb-libs-5.5.47-1.el7_2.x86_64 (updates)
               libmysqlclient.so.18()(64bit)
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest
[root@virtual mysql]#

这次出现的错误不太一样,出现了两个错误,一个错误仍然提示缺少依赖包,mysql-community-common(x86-64),另外一个错误是与 mariadb-libs-5.5.44-2.el7.centos.x86_64 发生冲突,所以需要卸载这个软件。

[root@virtual mysql]# yum remove mariadb-libs
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.50-1.el7_2 will be erased
--> 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
--> Running transaction check
---> Package postfix.x86_64 2:2.10.1-6.el7 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================================================
 Package                              Arch                           Version                                     Repository                         Size
=========================================================================================================================================================
Removing:
 mariadb-libs                         x86_64                         1:5.5.50-1.el7_2                            @updates                          4.4 M
Removing for dependencies:
 postfix                              x86_64                         2:2.10.1-6.el7                              @anaconda                          12 M

Transaction Summary
=========================================================================================================================================================
Remove  1 Package (+1 Dependent package)

Installed size: 17 M
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : 2:postfix-2.10.1-6.el7.x86_64                                                                                                         1/2
  Erasing    : 1:mariadb-libs-5.5.50-1.el7_2.x86_64                                                                                                  2/2
  Verifying  : 1:mariadb-libs-5.5.50-1.el7_2.x86_64                                                                                                  1/2
  Verifying  : 2:postfix-2.10.1-6.el7.x86_64                                                                                                         2/2

Removed:
  mariadb-libs.x86_64 1:5.5.50-1.el7_2

Dependency Removed:
  postfix.x86_64 2:2.10.1-6.el7

Complete!
[root@virtual mysql]#

卸载完成后解决最后一个依赖问题 mysql-community-common-5.7.16-1.el7.x86_64.rpm

[root@virtual mysql]# yum localinstall mysql-community-common-5.7.16-1.el7.x86_64.rpm
Loaded plugins: fastestmirror
Examining mysql-community-common-5.7.16-1.el7.x86_64.rpm: mysql-community-common-5.7.16-1.el7.x86_64
Marking mysql-community-common-5.7.16-1.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-common.x86_64 0:5.7.16-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================================================
 Package                              Arch                 Version                       Repository                                                 Size
=========================================================================================================================================================
Installing:
 mysql-community-common               x86_64               5.7.16-1.el7                  /mysql-community-common-5.7.16-1.el7.x86_64               2.5 M

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

Total size: 2.5 M
Installed size: 2.5 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql-community-common-5.7.16-1.el7.x86_64                                                                                            1/1
  Verifying  : mysql-community-common-5.7.16-1.el7.x86_64                                                                                            1/1

Installed:
  mysql-community-common.x86_64 0:5.7.16-1.el7

Complete!
[root@virtual mysql]#

接下来就是倒着安装相关依赖,首先安装 mysql-community-libs-5.7.16-1.el7.x86_64.rpm

[root@virtual mysql]# yum localinstall mysql-community-libs-5.7.16-1.el7.x86_64.rpm
Loaded plugins: fastestmirror
Examining mysql-community-libs-5.7.16-1.el7.x86_64.rpm: mysql-community-libs-5.7.16-1.el7.x86_64
Marking mysql-community-libs-5.7.16-1.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-libs.x86_64 0:5.7.16-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================================================
 Package                             Arch                  Version                        Repository                                                Size
=========================================================================================================================================================
Installing:
 mysql-community-libs                x86_64                5.7.16-1.el7                   /mysql-community-libs-5.7.16-1.el7.x86_64                9.4 M

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

Total size: 9.4 M
Installed size: 9.4 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql-community-libs-5.7.16-1.el7.x86_64                                                                                              1/1
  Verifying  : mysql-community-libs-5.7.16-1.el7.x86_64                                                                                              1/1

Installed:
  mysql-community-libs.x86_64 0:5.7.16-1.el7

Complete!
[root@virtual mysql]#

继续安装 mysql-community-client-5.7.16-1.el7.x86_64.rpm

[root@virtual mysql]# yum localinstall mysql-community-client-5.7.16-1.el7.x86_64.rpm
Loaded plugins: fastestmirror
Examining mysql-community-client-5.7.16-1.el7.x86_64.rpm: mysql-community-client-5.7.16-1.el7.x86_64
Marking mysql-community-client-5.7.16-1.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.16-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================================================
 Package                              Arch                 Version                       Repository                                                 Size
=========================================================================================================================================================
Installing:
 mysql-community-client               x86_64               5.7.16-1.el7                  /mysql-community-client-5.7.16-1.el7.x86_64               106 M

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

Total size: 106 M
Installed size: 106 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql-community-client-5.7.16-1.el7.x86_64                                                                                            1/1
  Verifying  : mysql-community-client-5.7.16-1.el7.x86_64                                                                                            1/1

Installed:
  mysql-community-client.x86_64 0:5.7.16-1.el7

Complete!
[root@virtual mysql]#

word 的天哪,终于到了安装 mysql-community-server-5.7.16-1.el7.x86_64.rpm 的时候了。

[root@virtual mysql]# yum localinstall mysql-community-server-5.7.16-1.el7.x86_64.rpm
Loaded plugins: fastestmirror
Examining mysql-community-server-5.7.16-1.el7.x86_64.rpm: mysql-community-server-5.7.16-1.el7.x86_64
Marking mysql-community-server-5.7.16-1.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.16-1.el7 will be installed
--> Processing Dependency: net-tools for package: mysql-community-server-5.7.16-1.el7.x86_64
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.yun-idc.com
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.17.20131004git.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================================================
 Package                           Arch              Version                                Repository                                              Size
=========================================================================================================================================================
Installing:
 mysql-community-server            x86_64            5.7.16-1.el7                           /mysql-community-server-5.7.16-1.el7.x86_64            691 M
Installing for dependencies:
 net-tools                         x86_64            2.0-0.17.20131004git.el7               base                                                   304 k

Transaction Summary
=========================================================================================================================================================
Install  1 Package (+1 Dependent package)

Total size: 691 M
Total download size: 304 k
Installed size: 692 M
Is this ok [y/d/N]: y
Downloading packages:
net-tools-2.0-0.17.20131004git.el7.x86_64.rpm                                                                                     | 304 kB  00:00:06
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : net-tools-2.0-0.17.20131004git.el7.x86_64                                                                                             1/2
  Installing : mysql-community-server-5.7.16-1.el7.x86_64                                                                                            2/2
  Verifying  : mysql-community-server-5.7.16-1.el7.x86_64                                                                                            1/2
  Verifying  : net-tools-2.0-0.17.20131004git.el7.x86_64                                                                                             2/2

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

Dependency Installed:
  net-tools.x86_64 0:2.0-0.17.20131004git.el7

Complete!
[root@virtual mysql]#

至此 MySQL 的服务器软件安装完毕,并且一并的安装了 MySQL 客户端软件。

[0x4]启动 MySQL

默认安装完成后可以检查下 MySQL 是否启动了。

[root@virtual mysql]# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
[root@virtual mysql]#

Active: inactive (dead)表示没启动,所以先启动 MySQL 服务。

[root@virtual mysql]# systemctl start mysqld
[root@virtual mysql]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2016-11-23 21:36:10 CST; 31s ago
  Process: 12769 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 12752 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 12773 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─12773 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Nov 23 21:36:09 virtual.m1 systemd[1]: Starting MySQL Server...
Nov 23 21:36:10 virtual.m1 systemd[1]: Started MySQL Server.
[root@virtual mysql]#

Active: active (running) 表示启动成功了。

[0x5]登录 MySQL

好像 MySQL 每个版本对于 root 的初始密码存放目录有变更,MySQL5.7 的 root 初始密码位置在 /var/log/mysqld.log,查看初始密码

[root@virtual mysql]# cat /var/log/mysqld.log | grep 'password'
2016-11-23T13:35:47.379026Z 1 [Note] A temporary password is generated for root@localhost: 8Elr*VgB7I?E
2016-11-23T13:36:08.112362Z 0 [Note] Shutting down plugin 'validate_password'
2016-11-23T13:36:09.489444Z 0 [Note] Shutting down plugin 'sha256_password'
2016-11-23T13:36:09.489450Z 0 [Note] Shutting down plugin 'mysql_native_password'
[root@virtual mysql]#

有了 root 初始密码就可以登录了。

[root@virtual mysql]# 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.16

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>

这个时候还不能正常使用 MySQL,会提示先修改 root 密码。

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql>

以前的 password() 函数将会被抛弃,官方建议使用下面的命令来修改密码 ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';;同时,如果你设置的密码过于简单也会报错。

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root123';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql>

对于 MySQL5.7 用户的密码规则大致如下:

  • 长度要大于 8
  • 同时要有数字,大小写,特殊字符
    也可以暂时不在这里设置 root 密码,下面的安装优化里也会涉及到 root 密码的修改。

PS:MySQL5.6 的 root 初始密码位置 /root/.mysql_secret

[0x6]加强 MySQL 安全

默认安装完 MySQL 服务器之后,一般需要执行这个脚本 mysql_secure_installation,这里就会涉及到 root 的密码修改。

[root@virtual mysql]# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:

The existing password for the user account root has expired. Please set a new password.

New password:

Re-enter new password:
Sorry, passwords do not match.

New password:

Re-enter new password:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!
[root@virtual mysql]#

下面列出 mysql_secure_installation 做的事情

  • Set root password? [Y/n] Y
  • Remove anonymous users? [Y/n] Y
  • Disallow root login remotely? [Y/n] Y
  • Remove test database and access to it? [Y/n] Y
  • Reload privilege tables now? [Y/n] Y

其中设置 root 密码根据情况而来吧,可以修改也可以不修改。

[0x7]设置默认字符集

一般都是设置为 utf8,MySQL 的配置文件路径是 /etc/my.cnf,下面就是设置字符集的相关的配置。

# vim /etc/my.cnf

[mysqld]                  #下面的配置是属于mysqld的,所以一定要配置在这个下面
#====设置服务端字符集====
#设置数据库字符集UTF8
character-set-server=utf8
#设置数据库字符集排序规则,注意要和character-set-server对应
collation-server=utf8_general_ci
#设置连接是的字符集
init-connect= 'SET NAMES utf8'

保存配置文件,然后重启 MySQL(systemctl restart mysqld),然后登录 MySQL,查看现在的字符集。

[root@virtual ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.16 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> 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.10 sec)

mysql>

[0x8]修改 MySQL 数据目录

迁移数据库目录 MySQL 数据库默认路径 /var/lib/mysql,实际工作中,常常需要定制数据库路径,比如 /data/mysql,或者 /opt/mysql,一般是单独的数据盘或者分区,这样有利于性能调优和保护数据安全,同时也方便进行维护。

  1. 首先在数据盘创建一个目录专门用来存放 MySQL 的数据的
    mkdir -p /data/mysql
  2. 分配目录的权限
    chown -R mysql:mysql /data/mysql
  3. 关闭 MySQL 服务 systemctl stop mysqld
  4. 编辑 MySQL 配置文件
[mysqld]
#下面是系统默认的数据目录位置
#datadir=/var/lib/mysql
#下面这个是修改后的数据目录位置
datadir=/data/mysql
#socket=/var/lib/mysql/mysql.sock
socket=/data/mysql/mysql.sock
  1. 迁移数据,即将 /var/lib/mysql 下的所有数据移动到 /data/mysql 下,执行命令 mv /var/lib/mysql/* /data/mysql/

这个时候如果执行重启命令,会提示错误。

[root@virtual data]# systemctl start mysqld
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
[root@virtual data]#

查看错误日志,看到其中有这样的日志 2016-11-24T14:01:36.732839Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation. 突然明白没有关闭 SELinux 服务,所以如果按照这个笔记来的话,则不会出现这个问题啦。关闭 SELinux 然后重启服务器,就可以正常启动 MySQL 服务了。

[0x9]结束

至此 MySQL 的安装和简单优化操作完成了,总体来看,MySQL 的安装比较简单,繁琐的地方还是在配置,如果想要优化的更好的话,那还有一坨事情等着呢,不过这样已经满足一般需求了。

未完待续(To be Continued...)

  • MySQL

    MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司。MySQL 是最流行的关系型数据库管理系统之一。

    675 引用 • 535 回帖
  • CentOS

    CentOS(Community Enterprise Operating System)是 Linux 发行版之一,它是来自于 Red Hat Enterprise Linux 依照开放源代码规定释出的源代码所编译而成。由于出自同样的源代码,因此有些要求高度稳定的服务器以 CentOS 替代商业版的 Red Hat Enterprise Linux 使用。两者的不同在于 CentOS 并不包含封闭源代码软件。

    238 引用 • 224 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...
  • Asimov

    好帖子!根据楼主的步骤来基本没什么问题

    1 回复
  • mzlion
    作者

    能帮到您,挺好的。