在Ubuntu上安装MySQL

MySQL软件包已包含在Ubuntu存储库中。

运行下面的命令来安装mysql服务器。

sudo apt update
sudo apt install mysql-server

安装服务器后,下面的命令可用于停止,启动和重新启动数据库服务。

sudo systemctl stop mysql.service
sudo systemctl start mysql.service
sudo systemctl restart mysql.service

要检查服务器的状态,请运行以下命令:

sudo systemctl status mysql.service

输出示例:

● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2016-04-30 09:04:02 CDT; 5s ago
Process: 4222 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, stat>
Main PID: 4248 (mysqld)
Status: "Server is operational"
Tasks: 39 (limit: 4657)
Memory: 376.8M
CGroup: /system.slice/mysql.service
└─4248 /usr/sbin/mysqld
Apr 30 09:04:01 ubuntu2004 systemd[1]: Starting MySQL Community Server. 
Apr 30 09:04:02 ubuntu2004 systemd[1]: Started MySQL Community Server.

MySQL Server附带一个脚本,允许我们通过将root用户安全使用密码来增强其安全性,并且删除其他不安全的设置。

运行以下命令以调用脚本:

sudo mysql_secure_installation

将会让我们回答一些问题:

Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT 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 component?
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: 2
Please set the password for root here.
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!

即使我们在上面配置了密码,当我们运行下面的命令时,我们将被授予访问,而无需密码。

sudo mysql

我们将自动授予访问权限。

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.19-0ubuntu5 (Ubuntu)
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>

之所以会出现这种情况,是因为当前版本8.0附带了一个功能,通过“auth_socket”插件提供根身份验证。

此插件通过套接字文件对从LocalHost连接的用户进行身份验证,而不会提示或者密码。

这可能会导致需要通过root连接到数据库的某些应用程序的问题。

要解决此问题,我们需要将默认身份验证机制 auth_socket更改为 mysql_native_password

登录返回MySQL控制台。

sudo mysql

然后运行以下命令以更改为mysql_native_password。

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

保存更改并退出:

FLUSH PRIVILEGES;
EXIT;

现在再访问MySQL控制台时将提示我们输入密码。

由于我们不想使用MySQL root用户 供外部应用程序来连接,
因此我们应该创建与root用户分开的管理员帐户。

GRANT ALL PRIVILEGES ON *.* TO 'superadmin'@'localhost' IDENTIFIED BY 'very_strong_password';
如何在Ubuntu 20.04上安装mysql

MySQL是当今最受欢迎的关系数据库服务器。

MySQL是一个开源的,快速,安全和可扩展的关系数据库管理系统。

从存储库安装MySQL

虽然Ubuntu包含了MySQL Server 软件包,但它们可能不一定是最新的。
如果我们想始终获取下载到服务器的最新版本的MySQL Server,则可能需要添加其存储库。

https://dev.mysql.com/downloads/repo/apt/

访问下载页面并查找适用于Ubuntu/debian的 deb包。

然后使用下面的命令进行下载并安装:

cd /tmp
wget https://dev.mysql.com/get/mysql-apt-config_0.8.15-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.15-1_all.deb

运行以下命令进行安装

sudo apt update
sudo apt upgrade
日期:2020-07-07 20:55:02 来源:oir作者:oir