Let’s learn how to install MySQL on Ubuntu today. MySQL is a free, open-source database management system. This database management system uses Structured Query Language (SQL) to manage data saved in a relational database. Developed by the Oracle Corporation in 1995, this efficient and scalable program finds applications in both small and big tech companies.

现在让我们学习如何在Ubuntu上安装MySQL。 MySQL是一个免费的开源数据库管理系统。 该数据库管理系统使用结构化查询语言(SQL)来管理关系数据库中保存的数据。 这个高效且可扩展的程序由Oracle Corporation在1995年开发,可在大型和小型科技公司中找到应用程序。

In this tutorial, we will go through the process to install the latest version of MySQL (ver 8.0.20 at the time of writing) on an Ubuntu system.

在本教程中,我们将介绍在Ubuntu系统上安装最新版本MySQL(在撰写本文时为8.0.20版)的过程。

先决条件 (Pre-requisites )

For following this tutorial to install MySQL your system, you need to fulfil the following requirements.

为了按照本教程安装MySQL系统,您需要满足以下要求。

  • A non-root user with Sudo privileges

    具有Sudo特权的非root用户

  • A system running Ubuntu

    运行Ubuntu的系统

  • An active firewall

    主动防火墙

在Ubuntu上安装MySQL的步骤 (Steps to Install MySQL on Ubuntu)

With the prerequisites out of the way, let’s jump right into installing the database system on Ubuntu.

有了先决条件,让我们直接进入在Ubuntu上安装数据库系统。

更新储存库 (Updating the Repositories)

Before we begin the tutorial to install MySQL on Ubuntu, we might wish to make sure that we have the latest version of the MySQL package. To do this, we need to update our default repositories using the apt package management service. 

在开始在Ubuntu上安装MySQL的教程之前,我们不妨确保我们具有最新版本MySQL软件包。 为此,我们需要使用apt软件包管理服务更新默认存储库。

To do this, you need to open the terminal on your Ubuntu system and type the following. 

为此,您需要在Ubuntu系统上打开终端并输入以下内容。Apt Update 更新

sudo apt update

Apt Update

You should see an output similar to this on your screen. Next, we will use the upgrade command in our terminal.

您应该在屏幕上看到类似的输出。 接下来,我们将在终端中使用upgrade命令。Apt Upgrade 升级

sudo apt update

Apt Upgrade

This is what you will see on the terminal screen. Enter ‘Y’ in the command line to proceed with the operation. These commands have replaced the default Ubuntu repositories with the latest available repositories for your system. Now, this ensures that we will get the latest version of the MySQL package when we commence with the installation on our system.

这就是您在终端屏幕上看到的内容。 在命令行中输入“ Y”以继续操作。 这些命令已使用系统的最新可用存储库替换了默认的Ubuntu存储库。 现在,这可以确保我们在系统上开始安装时获得最新版本的MySQL软件包。

在Ubuntu上安装MySQL服务器软件包 (Installing MySQL Server Package on Ubuntu)

Now that we have the latest available packages, we use the apt package manager to install the MySQL server on our system. This can be done using the following command in our terminal window.

现在我们有了最新的可用软件包,我们使用apt软件包管理器在我们的系统上安装MySQL服务器。 可以在终端窗口中使用以下命令来完成此操作。Apt安装Mysql

sudo apt install mysql-server

Apt Install Mysql

This will install the MySQL on our Ubuntu system. However, we cannot use it yet as the service is currently insecure. Let us now configure our service and make it ready for work.

这将在我们的Ubuntu系统上安装MySQL。 但是,由于该服务当前不安全,我们尚不能使用它。 现在,让我们配置我们的服务,使其准备工作。

在Ubuntu上配置MySQL的步骤 (Steps to Configure MySQL on Ubuntu)

Now that we’re done with the installation, it’s time to set things up for our system. Let’s begin with opening up the firewall ports.

现在我们已经完成了安装,是时候为我们的系统进行设置了。 让我们从打开防火墙端口开始。

允许MySQL通过防火墙 (Allowing MySQL through the firewall)

Note that this step is not required when MySQL is being used locally and does not require any remote client-server interaction.

请注意,当在本地使用MySQL时不需要此步骤,并且不需要任何远程客户端-服务器交互。

Usually, MySQL is used in a setting where multiple systems can send queries for data. Hence we need to allow remote access through the firewall. The default port at which the MySQL service listens to is 3306. So we use the following command to allow it through the firewall. 

通常,MySQL是在多个系统可以发送数据查询的环境中使用的。 因此,我们需要允许通过防火墙的远程访问。 MySQL服务侦听的默认端口是3306。因此,我们使用以下命令允许它通过防火墙。

sudo ufw allow mysql

UFW has default settings for multiple setups like Apache, LAMP, MySQL, HTTP, and others.

UFW具有Apache,LAMP,MySQL,HTTP等多种设置的默认设置。

运行安全脚本 (Running the security script)

Now, we will launch the secure installation utility by entering the following command in our terminal. 

现在,我们将在终端中输入以下命令来启动安全安装实用程序。

sudo mysql_secure_installation

This script will make changes to the security options of our freshly installed MySQL service such as remote root logins and sample test users. First, we will be prompted to choose whether we wish to set up the Validate Password Plugin.

该脚本将更改我们新安装MySQL服务的安全性选项,例如远程root登录和示例测试用户。 首先,将提示我们选择是否要设置验证密码插件。

Irrespective of the choice we make, we will next get a prompt to choose the password for the root user. 

无论我们做出什么选择,我们接下来都会提示您选择root用户的密码。

It will be followed by a series of less important options, so you can choose to modify the settings or enter ‘Y’ to confirm the default settings.

它后面会出现一系列次要的选项,因此您可以选择修改设置或输入“ Y”以确认默认设置。

When you are done, the MySQL service will load the modified rules and update itself to match your configurations immediately.

完成后,MySQL服务将加载修改后的规则并进行更新以立即匹配您的配置。

启动MySQL服务 (Starting the MySQL service)

Now that the installation is complete, we should start the MySQL service on our system. To do so, we use the systemctl command as shown below.

现在安装已完成,我们应该在系统上启动MySQL服务。 为此,我们使用systemctl命令 ,如下所示。

sudo systemctl start mysql

As we will use it in other sessions in the future and starting the process every single time is an unnecessary chore, we will set it to launch at startup. We can do this using the command given below.

由于我们将来会在其他会话中使用它,并且每次启动该过程都是不必要的工作,因此我们将其设置为在启动时启动。 我们可以使用下面给出的命令来做到这一点。

sudo systemctl enable mysql

Now every time we log into our system, MySQL process is started automatically.

现在,每次我们登录系统时,MySQL进程都会自动启动。

设置登录凭证 (Setting up the login credentials)

Remember you set up a root password earlier? We have yet to configure the password for logging into the MySQL server.

还记得您之前设置的root密码吗? 我们尚未配置用于登录MySQL服务器的密码。

Right now, our MySQL service is authenticating the root user using the auth_socket plugin. To fix this, we need to first launch the MySQL server on our system. we use the following command to do so.

现在,我们MySQL服务正在使用auth_socket插件对根用户进行身份验证。 要解决此问题,我们需要首先在系统上启动MySQL服务器。 我们使用以下命令来这样做。

sudo mysql

Next, we need to update the authentication. Now we choose a strong password and use it to update the authentication method with the following SQL command. 

接下来,我们需要更新身份验证。 现在,我们选择一个强密码,并使用它通过以下SQL命令更新身份验证方法。

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword'

Replace ‘newpassword’ with a password of your choice. Note that this will overwrite the password you chose earlier with the security script.

将“ newpassword”替换为您选择的密码。 请注意,这将覆盖您先前使用安全脚本选择的密码。

Now, we will reload all the grant tables to update the changes we just made. This can be done with the following command.

现在,我们将重新加载所有授权表以更新我们刚刚进行的更改。 可以使用以下命令来完成。

FLUSH PRIVILEGES

Now that we have configured our MySQL server, it’s time to test it out by logging into the server using the root user. Let us exit the service using the ‘exit’ command and log in again using our newly updated password.

现在我们已经配置了MySQL服务器,是时候通过使用root用户登录服务器来对其进行测试了。 让我们使用“退出”命令退出服务,然后使用新近更新的密码再次登录。

在我们的系统上测试MySQL (Testing MySQL on our system)

We have installed the MySQL server on our system and set up some basic configurations. But we just cannot be done without being sure that our freshly installed MySQL server is working properly.

我们已经在系统上安装了MySQL服务器,并设置了一些基本配置。 但是,如果不能确保刚安装MySQL服务器能够正常运行,我们就无法完成工作。

We will test the MySQL server with a basic query. First, we log on to the MySQL server using our root account and its associated password. 

我们将使用基本查询来测试MySQL服务器。 首先,我们使用根帐户及其关联的密码登录到MySQL服务器。

Login to MySQL Server

You should see a screen similar to this. Notice that this time we used a different method to log on to our root user. This is because without specifying the user and entering the correct password we cannot get administrative privileges of the root user.

您应该看到与此类似的屏幕。 请注意,这次我们使用了另一种方法登录到root用户。 这是因为如果不指定用户并输入正确的密码,我们将无法获得root用户的管理特权。

Now we will ask our MySQL server to display the list of users, hosts, and plugins on our system. This can be done using the following SQL command.

现在,我们将要求我们MySQL服务器显示系统上的用户,主机和插件的列表。 可以使用以下SQL命令来完成。

SELECT User, Host, Plugin FROM mysql.user;

Mysql Display Table

The above output confirms that our MySQL server is working properly.

上面的输出确认我们MySQL服务器正常工作。

结语 (Wrapping up)

MySQL is one of the leading relational database management systems available for users. This tutorial aimed to help you install a MySQL server on Ubuntu and set some basic configurations. If you have any feedback, queries, or suggestions, feel free to reach out to us in the comments below.

MySQL是可供用户使用的领先的关系数据库管理系统之一。 本教程旨在帮助您在Ubuntu上安装MySQL服务器并设置一些基本配置。 如果您有任何反馈,疑问或建议,请随时通过以下评论与我们联系。

翻译自: The Complete Guide to Install MySQL on Ubuntu - JournalDev

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐