今天我们向大家介绍,如何在 Ubuntu 18.04 系统上安装 MySQL 8.0。如何您之前安装了 MySQL 的其他版本,需要将原来 MySQL 的数据进行备份。我们之前也有过相关的内容,大家可以阅读下面的文章了解。
准备工作
在本文中,我们首先将MySQL软件包下载到本地,我们先进入临时文件夹:
$ cd /tmp
接下来,下载 MySQL 软件包到 /tmp 文件夹:
$ curl -OL https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb
下载完成后,.deb软件包可以用 dpkg 命令安装:
$ sudo dpkg -i mysql-apt-config*
安装完成后,配置选择MySQL v8 版本,使用以下命令升级:
$ sudo apt-get update
完成上述步骤后,也可以选择把刚下载的 .deb 软件包从 /tmp 文件夹删除:
$ sudo rm mysql-apt-config*
安装 MySQL 8.0
由于服务器软件库已经更新了最新的软件版本,现在使用 apt-get 命令安装 MySQL:
$ sudo apt-get install mysql-server
安装完成后输入以下命令,查看 MySQL 的运行状态:
$ service mysql status
会返回下面的信息:
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2018-12-05 11:37:47 UTC; 10s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 16032 (mysqld)
Status: "SERVER_OPERATING"
Tasks: 38 (limit: 1152)
CGroup: /system.slice/mysql.service
└─16032 /usr/sbin/mysqld
Dec 05 11:37:43 mysql-v8 systemd[1]: Starting MySQL Community Server...
Dec 05 11:37:47 mysql-v8 systemd[1]: Started MySQL Community Server.
对 MySQL 8.0 进行安全设置
从 MySQL 5.7 版本以后提供对其进行初始安全设置,可以根据系统提示进行设置。
- 安装密码强度验证插件
- 设置密码强度安全等级
- 修改 root 访问权限
- 移除匿名用户
- 移除测试数据库
- 设置完成重载安全表
要进行安全设置,可以执行以下命令:
$ mysql_secure_installation
MySQL安全配置向导会进行如下配置引导:
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?
选择是否安装 Validate Password Component 插件,如果选择是会有三种安全等级可选。
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
Estimated strength of the password: 50
Do you wish to continue with the password provided?
最低安全等级要求密码总长度8位数以上,中等安全等级除了要 8位数密码之外,还需要有数字、大小写英文字母和特殊符号,最强等级还要求符合字典验证标准。
接下来,选择是否禁止 root 远程登录,通常来说 root 只允许从 localhost 运行。
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.
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?
默认情况下,MYSQL 在安装后会创建一个叫 test 的数据库,我们选择是将其移除。
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?
最后,是否重新加载权限表,加载后就完成了设置。
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now?
MySQL 8.0 用户管理
重设 Root 密码
遇到无法登入,可重设密码。
Access denied for user 'root'@'localhost'
mysql -u root
mysql> use mysql;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Current-Root-Password';
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
创建 MySQL 用户账号和密码
这里以新增一个名为 newuser 的用户并将其密码设置为 12345678 为例:
mysql> CREATE USER 'newuser'@'localhost' IDENTIFIED BY '12345678';
给予新用户 newuser 相应的数据库权限:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';
重新加载权限表:
mysql> FLUSH PRIVILEGES;
创建 MySQL 远程账号和密码
新增远程 remote 用户 newuser 并设置其密码 12345678 :
mysql> CREATE USER 'newuser'@'%' IDENTIFIED BY '12345678';
给予用户 newuser 相应的数据库权限:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'%';
重新加载权限表:
mysql> FLUSH PRIVILEGES;
另外 MySQL 默认监听 127.0.0.1 ,如果要远程访问,需修改设置:
/etc/my.cnf
/etc/mysql/my.cnf
$MYSQL_HOME/my.cnf
[datadir]/my.cnf
~/.my.cnf
使用 nano 或 vi等编辑器,将 bind-address 注释掉或将其改成 0.0.0.0,然后保存。
[mysqld]
# Only allow connections from localhost
#bind-address = 127.0.0.1
重启 MySQL 8.0,即可生效。
$ service mysql restart
以上就是在 Ubuntu 18.04 上安装 MySQL 8.0 的操作方法。
作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/linux/how-to-install-mysql-8-0-on-ubuntu-18-04/