Skip to content Skip to main navigation Skip to footer

Ubuntu 16.04 系统下安装 MySQL Server 5.7详细教程

本文将会讲述如何在Ubuntu 16.4 LTS Linux系统(Xenial Xerus)下安装,配置并且管理MySQL Server 5.7. 该版本是现在最新的MySQL 数据库发布版本。

1# 更新当前ubuntu16.04 系统

执行下面的命令:

 $ sudo apt update
 $ sudo apt upgrade

命令输出如下:

root@ubuntutest:~# apt update
 命中:1 http://archive.ubuntu.com/ubuntu xenial InRelease
 获取:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
 获取:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
 获取:4 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
 获取:5 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [344 kB]
 获取:6 http://security.ubuntu.com/ubuntu xenial-security/main i386 Packages [322 kB]
 获取:7 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [619 kB]
 获取:8 http://security.ubuntu.com/ubuntu xenial-security/main Translation-en [145 kB]
 获取:9 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [156 kB]
 获取:10 http://archive.ubuntu.com/ubuntu xenial-updates/main i386 Packages [595 kB]
 获取:11 http://security.ubuntu.com/ubuntu xenial-security/universe i386 Packages [137 kB]
 获取:12 http://security.ubuntu.com/ubuntu xenial-security/universe Translation-en [79.8 kB]
 获取:13 http://archive.ubuntu.com/ubuntu xenial-updates/main Translation-en [250 kB]
 获取:14 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [517 kB]
 获取:15 http://archive.ubuntu.com/ubuntu xenial-updates/universe i386 Packages [497 kB]
 获取:16 http://archive.ubuntu.com/ubuntu xenial-updates/universe Translation-en [202 kB]
 已下载 4,169 kB,耗时 5秒 (771 kB/s)
 正在读取软件包列表... 完成
 正在分析软件包的依赖关系树
 正在读取状态信息... 完成
 有 126 个软件包可以升级。请执行 ‘apt list --upgradable’ 来查看它们。
root@ubuntutest:~# sudo apt upgrade
 正在读取软件包列表... 完成
 正在分析软件包的依赖关系树
 正在读取状态信息... 完成
 正在计算更新... 完成
 下列【新】软件包将被安装:
 linux-headers-4.4.0-92 linux-headers-4.4.0-92-generic linux-image-4.4.0-92-generic linux-image-extra-4.4.0-92-generic
 下列软件包将被升级:
 ......
 uuid-runtime vlan wget zlib1g
 升级了 126 个软件包,新安装了 4 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。
 需要下载 165 MB 的归档。
 解压缩后会消耗 317 MB 的额外空间。
 您希望继续执行吗? [Y/n] Y

2# 在Ubuntu 16.04 LTS 系统上安装MySQL 5.7

执行下面的命令:

$ sudo apt-get install mysql-server mysql-client

命令输出如下:

root@ubuntutest:~# apt-get install mysql-server mysql-client
 正在读取软件包列表... 完成
 正在分析软件包的依赖关系树
 正在读取状态信息... 完成
 将会同时安装下列软件:
 libaio1 libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libevent-core-2.0-5 libfcgi-perl libhtml-parser-perl libhtml-tagset-perl
 libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client-5.7
 mysql-client-core-5.7 mysql-common mysql-server-5.7 mysql-server-core-5.7
 建议安装:
 libdata-dump-perl libipc-sharedcache-perl libwww-perl mailx tinyca
 下列【新】软件包将被安装:
 libaio1 libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libevent-core-2.0-5 libfcgi-perl libhtml-parser-perl libhtml-tagset-perl
 libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client
 mysql-client-5.7 mysql-client-core-5.7 mysql-common mysql-server mysql-server-5.7 mysql-server-core-5.7
 升级了 0 个软件包,新安装了 22 个软件包,要卸载 0 个软件包,有 126 个软件包未被升级。
 需要下载 18.5 MB 的归档。
 解压缩后会消耗 163 MB 的额外空间。
 您希望继续执行吗? [Y/n] Y

安装过程中会弹出MySQL root 用户密码输入窗口:
ubuntu 1604 mysql 5.7 root user password

3# 运行mysql_secure_installation 命令来设置MySQL 的安全配置

执行命令:

$ sudo mysql_secure_installation

命令输出如下:

root@ubuntutest:~# sudo 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: 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
 Using existing password for root.
Estimated strength of the password: 50
 Change the password for root ? ((Press y|Y for Yes, any other key for No) : N
... 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!
 root@ubuntutest:~#

该命令将会修改root用户密码,禁用或者删除匿名用户,禁止MySQL root 用户远程登录,删除测试数据库等操作,进而可以确保MySQL 数据库的安全。

4# 连接MySQL Server

执行下面的命令来登录到MySQL server:

$ sudo mysql -u root -p

此时需要输入mysql root 用户的密码。
命令输出如下:

root@ubuntutest:~# mysql -u root -p
 Enter password:
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 7
 Server version: 5.7.19-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2017, 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>

5# 创建数据库

执行下面的命令:
Create database 数据库名;
比如创建一个名为myosetcdb的数据库:

mysql> create database myosetcdb;
 Query OK, 1 row affected (0.00 sec)
mysql> show databases;
 +--------------------+
 | Database           |
 +--------------------+
 | information_schema |
 | myosetcdb          |
 | mysql              |
 | performance_schema |
 | sys                |
 +--------------------+
 5 rows in set (0.00 sec)

可以使用”show databases” 命令来查看当前server的数据库列表。
启动MySQL 服务

$ sudo systemctl start mysql.service

停止MySQL 服务

$ sudo systemctl stop mysql.service

重启MysQL 服务

$ sudo systemctl restart mysql.service

查看MySQL 服务的运行状态

$ 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 三 2017-08-16 14:28:46 CST; 15min ago
 Main PID: 6195 (mysqld)
 CGroup: /system.slice/mysql.service
 └─6195 /usr/sbin/mysqld
8月 16 14:28:45 ubuntutest systemd[1]: Starting MySQL Community Server...
 8月 16 14:28:46 ubuntutest systemd[1]: Started MySQL Community Server.

重置mysql root 用户的密码
执行下面的命令来更改MySQL root 用户的密码:

$ sudo dpkt-reconfigure mysql-server-5.7

更多详情请参考另一篇文章 ”重置mysql root 用户的密码