MySQL 是一个开源关系数据库管理系统。本教程将帮助您在 Ubuntu 和 Debian 系统上安装 MySQL 服务器。与之前的版本相比,该版本有多项安全改进。
步骤 1 – 配置 Apt 存储库
下载 MySQL 团队官方提供的 MySQL apt 配置 Debian 软件包并将其安装到您的系统上。对于 Ubuntu 16.04 及更高版本,MySQL 5.7 在默认 apt 存储库下可用,因此您无需启用其他存储库。
wget http://repo.mysql.com/mysql-apt-config_0.8.9-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.9-1_all.deb
这将提示您选择要安装的 MySQL 版本。这样 dpkg 就可以相应地配置 apt 存储库。
第 2 步 – 安装 MySQL 服务器
使用以下命令在 Ubuntu 16.04 和 14.04 系统上安装 MySQL 服务器。目前,这是用户使用最流行的版本。
sudo apt-get update
sudo apt-get install mysql-server
安装过程会提示输入root用户的默认密码并再次确认密码。
步骤 3 – 安全 MySQL 安装
现在运行 mysql_secure_installation 命令。这将提示您配置变量安全更改。就像使用验证密码插件启用密码强度一样,禁用远程 root 登录、远程测试数据库及其连接。
mysql_secure_installation
按照屏幕上的说明进行操作
Securing the MySQL server deployment.
Enter password for user root:
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:
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
... skipping.
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!
第 4 步 – 使用 MySQL
最后,使用以下命令登录MySQL服务器。输入安装过程中创建的密码。在这里您可以创建数据库和表来存储数据。您可以在线阅读我们的MySQL教程使用 MySQL 命令行执行各种任务。您可能还需要禁用严格 SQL 模式.
mysql -u root -p
MySQL 命令提示符
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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>