如何在Ubuntu 18.04操作系统上安装MySQL?

介绍

MySQL是一个开源数据库管理系统,通常作为流行的LAMP(Linux,Apache,MySQL,PHP / Python / Perl)堆栈的一部分安装。它使用关系数据库和SQL(结构化查询语言)来管理其数据。

简短的安装过程很简单:更新软件包索引,安装mysql-server软件包,然后运行附带的安全脚本。

sudo apt update
sudo apt install mysql-server
sudo mysql_secure_installation

本教程将说明如何在Ubuntu 18.04服务器上安装MySQL 5.7版。但是,如果您希望将现有的MySQL安装更新到5.7版,则可以阅读MySQL 5.7更新指南

先决条件

要遵循本教程,您将需要:

  • 按照此初始服务器设置指南设置一台Ubuntu 18.04服务器,其中包括具有特权的非root用户sudo和防火墙。

第1步-安装MySQL

在Ubuntu 18.04上,默认情况下,APT软件包存储库中仅包含新版本的MySQL。在撰写本文时,这是MySQL 5.7

要安装它,请使用以下命令更新服务器上的软件包索引apt

sudo apt update

然后安装默认软件包:

sudo apt install mysql-server

这将安装MySQL,但不会提示您设置密码或进行任何其他配置更改。因为这会使您的MySQL安装不安全,所以接下来将解决此问题。

第2步-配置MySQL

对于全新安装,您将需要运行随附的安全脚本。这会更改一些不太安全的默认选项,例如远程root登录和demo用户。在旧版本的MySQL上,您也需要手动初始化数据目录,但是现在这是自动完成的。

运行安全脚本:

sudo mysql_secure_installation

这将引导您完成一系列提示,在其中您可以对MySQL安装的安全性选项进行一些更改。第一个提示将询问您是否要设置验证密码插件,该插件可用于测试MySQL密码的强度。无论您选择什么,下一个提示都是为MySQL root用户设置密码。输入,然后确认您选择的安全密码。

从那里,您可以按Y,然后ENTER接受所有后续问题的默认设置。这将删除一些匿名用户和测试数据库,禁用远程root登录,并加载这些新规则,以便MySQL立即尊重您所做的更改。

要初始化MySQL数据目录,您将使用mysql_install_db5.7.6之前的版本以及mysqld --initialize5.7.6和更高版本。但是,如果按照步骤1中的说明从Debian发行版中安装了MySQL,则数据目录将自动初始化;否则,将自动初始化数据目录。您无需执行任何操作。如果仍然尝试运行该命令,则会看到以下错误:

mysqld: Can't create directory '/var/lib/mysql/' (Errcode: 17 - File exists)
. . .
2018-04-23T13:48:00.572066Z 0 [ERROR] Aborting

请注意:即使您已经为MySQL root用户设置了密码,但在连接到MySQL Shell时,该用户仍未配置为使用密码进行身份验证。如果需要,可以按照步骤3调整此设置。

步骤3 —(可选)调整用户身份验证和特权

在运行MySQL 5.7(及更高版本)的Ubuntu系统中,默认情况下,MySQL root用户设置为使用auth_socket插件而不是密码进行身份验证。在许多情况下,这可以提高安全性和可用性,但是当您需要允许外部程序(例如phpMyAdmin)访问用户时,也可能使事情变得复杂。

为了使用密码以root用户身份连接到MySQL ,您需要将其身份验证方法从切换auth_socketmysql_native_password。为此,请从终端打开MySQL提示符:

sudo mysql

接下来,使用以下命令检查每个MySQL用户帐户使用的身份验证方法:

SELECT user,authentication_string,plugin,host FROM mysql.user;
Output+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             |                                           | auth_socket           | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)
Ubuntu安装MySQL
Ubuntu安装MySQL

在此示例中,您可以看到root用户实际上使用auth_socket插件进行了身份验证。要将root帐户配置为使用密码进行身份验证,请运行以下ALTER USER命令。确保更改password为您选择的强密码,并注意此命令将更改您在步骤2中设置的root密码:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

然后,运行FLUSH PRIVILEGES告诉服务器重新加载授权表并使新的更改生效:

FLUSH PRIVILEGES;
设置MySQL密码
设置MySQL密码

再次检查每个用户使用的身份验证方法,以确认root不再使用auth_socket插件进行身份验证:

SELECT user,authentication_string,plugin,host FROM mysql.user;
Output+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             | *3636DACC8616D997782ADD0839F92C1571D6D78F | mysql_native_password | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)
Ubuntu安装MySQL
Ubuntu安装MySQL

您可以在此示例输出中看到,MySQL root用户现在使用密码进行了身份验证。一旦在自己的服务器上确认了这一点,就可以退出MySQL Shell:

exit

另外,有些人可能会发现它更适合他们的工作流程,以便与专门的用户连接到MySQL。要创建这样的用户,请再次打开MySQL Shell:

sudo mysql

注意:如果您如前段所述为root启用了密码认证,则将需要使用其他命令来访问MySQL Shell。以下内容将以常规用户权限运行您的MySQL客户端,并且您将仅通过身份验证在数据库内获得管理员权限:

mysql -u root -p

从那里,创建一个新用户并为其设置一个强密码:

CREATE USER 'sammy'@'localhost' IDENTIFIED BY 'password';

然后,为新用户授予适当的特权。例如,您可以使用以下命令向数据库内的所有表授予用户特权,以及添加,更改和删除用户特权的能力:

GRANT ALL PRIVILEGES ON *.* TO 'sammy'@'localhost' WITH GRANT OPTION;

请注意,此时,您无需FLUSH PRIVILEGES再次运行该命令。当你修改使用语句授权表喜欢,才需要此命令INSERTUPDATEDELETE。因为您创建了一个新用户,而不需要修改现有用户,FLUSH PRIVILEGES所以这里不需要。

接下来,退出MySQL shell:

exit

后,让我们测试MySQL的安装。

第4步-测试MySQL

无论您如何安装,MySQL都应该已经开始自动运行。要对此进行测试,请检查其状态。

systemctl status mysql.service

您将看到类似于以下内容的输出:

● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: en
   Active: active (running) since Wed 2018-04-23 21:21:25 UTC; 30min ago
 Main PID: 3754 (mysqld)Tasks: 28
   Memory: 142.3M  CPU: 1.994s
   CGroup: /system.slice/mysql.service       └─3754 /usr/sbin/mysqld

如果MySQL未运行,则可以使用来启动它sudo systemctl start mysql

要进行其他检查,可以尝试使用mysqladmin工具连接到数据库,该工具是允许您运行管理命令的客户端。例如,此命令说要以root-u root)身份连接到MySQL ,提示输入密码(-p),然后返回版本。

sudo mysqladmin -p -u root version

您应该看到类似于以下的输出:

mysqladmin  Ver 8.42 Distrib 5.7.21, for Linux on x86_64
Copyright (c) 2000, 2018, 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.

Server version      5.7.21-1ubuntu1
Protocol version    10
Connection      Localhost via UNIX socket
UNIX socket     /var/run/mysqld/mysqld.sock
Uptime:         30 min 54 sec

Threads: 1  Questions: 12  Slow queries: 0  Opens: 115  Flush tables: 1  Open tables: 34  Queries per second avg: 0.006

这意味着MySQL已启动并正在运行。

总结

现在,您已在服务器上安装了基本的MySQL设置。以下是一些您可以采取的后续步骤的示例:

附安装命令:

louis@ubuntu:~$ sudo apt install mysql-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libaio1 libevent-core-2.1-6 libhtml-template-perl mysql-client-5.7
  mysql-client-core-5.7 mysql-common mysql-server-5.7 mysql-server-core-5.7
Suggested packages:
  libipc-sharedcache-perl mailx tinyca
The following NEW packages will be installed:
  libaio1 libevent-core-2.1-6 libhtml-template-perl mysql-client-5.7
  mysql-client-core-5.7 mysql-common mysql-server mysql-server-5.7
  mysql-server-core-5.7
0 upgraded, 9 newly installed, 0 to remove and 26 not upgraded.
Need to get 19.2 MB of archives.
After this operation, 155 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://mirrors.aliyun.com/ubuntu bionic/main amd64 mysql-common all 5.8+1.0.4 [7,308 B]
Get:2 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 libaio1 amd64 0.3.110-5ubuntu0.1 [6,476 B]
Get:3 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 mysql-client-core-5.7 amd64 5.7.31-0ubuntu0.18.04.1 [6,653 kB]
Get:4 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 mysql-client-5.7 amd64 5.7.31-0ubuntu0.18.04.1 [1,948 kB]
Get:5 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 mysql-server-core-5.7 amd64 5.7.31-0ubuntu0.18.04.1 [7,452 kB]
Get:6 http://mirrors.aliyun.com/ubuntu bionic/main amd64 libevent-core-2.1-6 amd64 2.1.8-stable-4build1 [85.9 kB]
Get:7 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 mysql-server-5.7 amd64 5.7.31-0ubuntu0.18.04.1 [2,931 kB]
Get:8 http://mirrors.aliyun.com/ubuntu bionic/main amd64 libhtml-template-perl all 2.97-1 [59.0 kB]
Get:9 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 mysql-server all 5.7.31-0ubuntu0.18.04.1 [9,948 B]
Fetched 19.2 MB in 3s (5,789 kB/s) 
Preconfiguring packages ...
Selecting previously unselected package mysql-common.
(Reading database ... 125938 files and directories currently installed.)
Preparing to unpack .../0-mysql-common_5.8+1.0.4_all.deb ...
Unpacking mysql-common (5.8+1.0.4) ...
Selecting previously unselected package libaio1:amd64.
Preparing to unpack .../1-libaio1_0.3.110-5ubuntu0.1_amd64.deb ...
Unpacking libaio1:amd64 (0.3.110-5ubuntu0.1) ...
Selecting previously unselected package mysql-client-core-5.7.
Preparing to unpack .../2-mysql-client-core-5.7_5.7.31-0ubuntu0.18.04.1_amd64.deb ...
Unpacking mysql-client-core-5.7 (5.7.31-0ubuntu0.18.04.1) ...
Selecting previously unselected package mysql-client-5.7.
Preparing to unpack .../3-mysql-client-5.7_5.7.31-0ubuntu0.18.04.1_amd64.deb ...
Unpacking mysql-client-5.7 (5.7.31-0ubuntu0.18.04.1) ...
Selecting previously unselected package mysql-server-core-5.7.
Preparing to unpack .../4-mysql-server-core-5.7_5.7.31-0ubuntu0.18.04.1_amd64.deb ...
Unpacking mysql-server-core-5.7 (5.7.31-0ubuntu0.18.04.1) ...
Selecting previously unselected package libevent-core-2.1-6:amd64.
Preparing to unpack .../5-libevent-core-2.1-6_2.1.8-stable-4build1_amd64.deb ...
Unpacking libevent-core-2.1-6:amd64 (2.1.8-stable-4build1) ...
Setting up mysql-common (5.8+1.0.4) ...
update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Selecting previously unselected package mysql-server-5.7.
(Reading database ... 126106 files and directories currently installed.)
Preparing to unpack .../mysql-server-5.7_5.7.31-0ubuntu0.18.04.1_amd64.deb ...
Unpacking mysql-server-5.7 (5.7.31-0ubuntu0.18.04.1) ...
Selecting previously unselected package libhtml-template-perl.
Preparing to unpack .../libhtml-template-perl_2.97-1_all.deb ...
Unpacking libhtml-template-perl (2.97-1) ...
Selecting previously unselected package mysql-server.
Preparing to unpack .../mysql-server_5.7.31-0ubuntu0.18.04.1_all.deb ...
Unpacking mysql-server (5.7.31-0ubuntu0.18.04.1) ...
Setting up libevent-core-2.1-6:amd64 (2.1.8-stable-4build1) ...
Setting up libhtml-template-perl (2.97-1) ...
Setting up libaio1:amd64 (0.3.110-5ubuntu0.1) ...
Setting up mysql-client-core-5.7 (5.7.31-0ubuntu0.18.04.1) ...
Setting up mysql-server-core-5.7 (5.7.31-0ubuntu0.18.04.1) ...
Setting up mysql-client-5.7 (5.7.31-0ubuntu0.18.04.1) ...
Setting up mysql-server-5.7 (5.7.31-0ubuntu0.18.04.1) ...
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Renaming removed key_buffer and myisam-recover options (if present)
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
Setting up mysql-server (5.7.31-0ubuntu0.18.04.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.2) ...
Processing triggers for systemd (237-3ubuntu10.42) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for ureadahead (0.100.0-21) ...
louis@ubuntu:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.

New password: 

Re-enter new password: 

Estimated strength of the password: 25 
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) : no

 ... skipping.
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) : no

 ... skipping.
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! 
滚动至顶部
扫描微信二维码联系我们 关闭