博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在Ubuntu 18.04上安装MySQL
阅读量:2505 次
发布时间:2019-05-11

本文共 12539 字,大约阅读时间需要 41 分钟。

A previous version of this tutorial was written by

本教程的先前版本由

介绍 (Introduction)

is an open-source database management system, commonly installed as part of the popular (Linux, Apache, MySQL, PHP/Python/Perl) stack. It uses a relational database and SQL (Structured Query Language) to manage its data.

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

The short version of the installation is simple: update your package index, install the mysql-server package, and then run the included security script.

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

  • sudo apt update

    sudo apt更新
  • sudo apt install mysql-server

    sudo apt安装mysql服务器
  • sudo mysql_secure_installation

    须藤mysql_secure_installation

This tutorial will explain how to install MySQL version 5.7 on an Ubuntu 18.04 server. However, if you’re looking to update an existing MySQL installation to version 5.7, you can read instead.

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

先决条件 (Prerequisites)

To follow this tutorial, you will need:

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

  • One Ubuntu 18.04 server set up by following , including a non-root user with sudo privileges and a firewall.

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

第1步-安装MySQL (Step 1 — Installing MySQL)

On Ubuntu 18.04, only the latest version of MySQL is included in the APT package repository by default. At the time of writing, that’s MySQL 5.7

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

To install it, update the package index on your server with apt:

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

  • sudo apt update

    sudo apt更新

Then install the default package:

然后安装默认软件包:

  • sudo apt install mysql-server

    sudo apt安装mysql服务器

This will install MySQL, but will not prompt you to set a password or make any other configuration changes. Because this leaves your installation of MySQL insecure, we will address this next.

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

第2步-配置MySQL (Step 2 — Configuring MySQL)

For fresh installations, you’ll want to run the included security script. This changes some of the less secure default options for things like remote root logins and sample users. On older versions of MySQL, you needed to initialize the data directory manually as well, but this is done automatically now.

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

Run the security script:

运行安全脚本:

  • sudo mysql_secure_installation

    须藤mysql_secure_installation

This will take you through a series of prompts where you can make some changes to your MySQL installation’s security options. The first prompt will ask whether you’d like to set up the Validate Password Plugin, which can be used to test the strength of your MySQL password. Regardless of your choice, the next prompt will be to set a password for the MySQL root user. Enter and then confirm a secure password of your choice.

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

From there, you can press Y and then ENTER to accept the defaults for all the subsequent questions. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately respects the changes you have made.

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

To initialize the MySQL data directory, you would use mysql_install_db for versions before 5.7.6, and mysqld --initialize for 5.7.6 and later. However, if you installed MySQL from the Debian distribution, as described in Step 1, the data directory was initialized automatically; you don’t have to do anything. If you try running the command anyway, you’ll see the following error:

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

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

Note that even though you’ve set a password for the root MySQL user, this user is not configured to authenticate with a password when connecting to the MySQL shell. If you’d like, you can adjust this setting by following Step 3.

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

步骤3 —(可选)调整用户身份验证和特权 (Step 3 — (Optional) Adjusting User Authentication and Privileges)

In Ubuntu systems running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external program (e.g., phpMyAdmin) to access the user.

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

In order to use a password to connect to MySQL as root, you will need to switch its authentication method from auth_socket to mysql_native_password. To do this, open up the MySQL prompt from your terminal:

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

  • sudo mysql

    须藤MySQL

Next, check which authentication method each of your MySQL user accounts use with the following command:

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

  • SELECT user,authentication_string,plugin,host FROM mysql.user;

    选择用户,authentication_string,插件,主机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)

In this example, you can see that the root user does in fact authenticate using the auth_socket plugin. To configure the root account to authenticate with a password, run the following ALTER USER command. Be sure to change password to a strong password of your choosing, and note that this command will change the root password you set in Step 2:

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

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

    ALTER USER'root'@'localhost'由mysql_native_password标识为' password ';

Then, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect:

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

  • FLUSH PRIVILEGES;

    冲洗特权;

Check the authentication methods employed by each of your users again to confirm that root no longer authenticates using the auth_socket plugin:

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

  • SELECT user,authentication_string,plugin,host FROM mysql.user;

    选择用户,authentication_string,插件,主机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)

You can see in this example output that the root MySQL user now authenticates using a password. Once you confirm this on your own server, you can exit the MySQL shell:

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

  • exit

    出口

Alternatively, some may find that it better suits their workflow to connect to MySQL with a dedicated user. To create such a user, open up the MySQL shell once again:

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

  • sudo mysql

    须藤MySQL

Note: If you have password authentication enabled for root, as described in the preceding paragraphs, you will need to use a different command to access the MySQL shell. The following will run your MySQL client with regular user privileges, and you will only gain administrator privileges within the database by authenticating:

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

  • mysql -u root -p

    mysql -u root -p

From there, create a new user and give it a strong password:

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

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

    创建用户' sammy '@'localhost'由' password '标识;

Then, grant your new user the appropriate privileges. For example, you could grant the user privileges to all tables within the database, as well as the power to add, change, and remove user privileges, with this command:

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

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

    将所有特权授予*。*到' sammy '@'localhost'并带有格randint选项;

Note that, at this point, you do not need to run the FLUSH PRIVILEGES command again. This command is only needed when you modify the grant tables using statements like INSERT, UPDATE, or DELETE. Because you created a new user, instead of modifying an existing one, FLUSH PRIVILEGES is unnecessary here.

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

Following this, exit the MySQL shell:

接下来,退出MySQL shell:

  • exit

    出口

Finally, let’s test the MySQL installation.

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

第4步-测试MySQL (Step 4 — Testing MySQL)

Regardless of how you installed it, MySQL should have started running automatically. To test this, check its status.

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

  • systemctl status mysql.service

    systemctl状态mysql.service

You’ll see output similar to the following:

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

Output
输出量
● 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

If MySQL isn’t running, you can start it with sudo systemctl start mysql.

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

For an additional check, you can try connecting to the database using the mysqladmin tool, which is a client that lets you run administrative commands. For example, this command says to connect to MySQL as root (-u root), prompt for a password (-p), and return the version.

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

  • sudo mysqladmin -p -u root version

    sudo mysqladmin -p -u根版本

You should see output similar to this:

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

Output
输出量
mysqladmin  Ver 8.42 Distrib 5.7.21, for Linux on x86_64Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Server version      5.7.21-1ubuntu1Protocol version    10Connection      Localhost via UNIX socketUNIX socket     /var/run/mysqld/mysqld.sockUptime:         30 min 54 secThreads: 1  Questions: 12  Slow queries: 0  Opens: 115  Flush tables: 1  Open tables: 34  Queries per second avg: 0.006

This means MySQL is up and running.

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

结论 (Conclusion)

You now have a basic MySQL setup installed on your server. Here are a few examples of next steps you can take:

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

翻译自:

转载地址:http://wxhgb.baihongyu.com/

你可能感兴趣的文章
sessionStorage
查看>>
代码示例_进程
查看>>
Java中关键词之this,super的使用
查看>>
人工智能暑期课程实践项目——智能家居控制(一)
查看>>
前端数据可视化插件(二)图谱
查看>>
kafka web端管理工具 kafka-manager【转发】
查看>>
获取控制台窗口句柄GetConsoleWindow
查看>>
Linux下Qt+CUDA调试并运行
查看>>
51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)
查看>>
OKMX6Q在ltib生成的rootfs基础上制作带QT库的根文件系统
查看>>
zabbix
查看>>
多线程基础
查看>>
完美解决 error C2220: warning treated as error - no ‘object’ file generated
查看>>
使用SQL*PLUS,构建完美excel或html输出
查看>>
SQL Server数据库笔记
查看>>
X-Forwarded-For伪造及防御
查看>>
android系统平台显示驱动开发简要:LCD驱动调试篇『四』
查看>>
Android 高仿微信头像截取 打造不一样的自定义控件
查看>>
Jenkins的初级应用(1)-Publish Over SSH
查看>>
利用正则表达式群发定制邮件
查看>>