MySql教程

MySQL 8 & MySQL Workbench Installation on Ubuntu 20.04

本文主要是介绍MySQL 8 & MySQL Workbench Installation on Ubuntu 20.04,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

MySQL 8 & MySQL Workbench Installation on Ubuntu 20.04

1. 安装MySQL Server

在终端中执行下列命令。

sudo apt update
sudo apt install mysql-server

安装完成后MySQL服务将会自动启动,在终端中执行以下命令以验证运行状态。

sudo systemctl status mysql

2. 初始化设置

在终端中输入启动初始化设置命令,然后通过几个“是”“否”选项逐步进行配置。

(1)Would you like to setup VALIDATE PASSWORD component?**

—— n

然后设置root账户密码,并重复输入一遍。两遍密码输入都不会在终端中显示。

(2)Remove anonymous users?

—— n

(3)Disallow root login remotely?

—— n

(4)Remove test database and access to it?

—— n

(5)Reload privilege tables now?

—— y

当显示“All done!”时,表示初始化完成。终端里的具体显示内容如下。

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: n
Please set the password for root here.

New password: 

Re-enter new password: 
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) : n

 ... skipping.


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) : n

 ... skipping.
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) : n

 ... skipping.
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! 

3. 安装MySQL Workbench

通过sudo apt-get install mysql-workbench命令进行安装,据说安装不了,所以先不尝试了,直接从官网下载。

下载地址:MySQL :: Download MySQL Workbench

当前最新版本是# MySQL Workbench 8.0.27,官方根据不同系统给出了很多版本。基于我的电脑的情况,在“Select Operating System”中选择“Ubuntu Linux”,在“Select OS Version”中选择“Ubuntu Linux 20.04 (x86, 64-bit)”。选择完以后,列表里就只剩下2个了,我下载的是第一个,稍微小一点的安装包。

接下来会跳转到下载页面,这里提示登陆Oracle账号,但其实不登陆也行,于是点下面的小字“No thanks, just start my download.”,然后浏览器就会开始下载。

下载完毕后,找到下载的.deb文件,双击打开,在安装器界面点击“Install”按钮。按下去以后会提示输入用户密码,正常流程,输入就好。紧接着就会开始安装。

4. 连接MySQL Workbench和MySQL Server

打开安装好的MySQL Workbench发现已经有一个root账户在了,于是点击登录,若登录成功,则正常使用即可;若登录失败,报错“Cannot Connect to Database Server”,则可按照以下步骤修复。

所报错误大致意思是连接root账户失败,让用户检查MySQL服务是否在运行、检查端口号等等。以下步骤是参考了一些排障教程外加亲手操作以后总结而成。

(1)删除MySQL Workbench已经存在的root账户

(2)以root身份从终端中登陆MySQL

sudo mysql -uroot -p

(3)检查端口号3306

通过以下命令检查端口是否启用,如果返回结果列表中有端口号3306,则说明端口没问题。从网上大量的教程来看,大多数用户的端口没问题。

show global variables like 'port';

# 返回结果应为
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)

(4)修改账户host

初始化的时候实际上就已经把root的host设置成了“localhost”,这里再做一次相当于二次确认。

use mysql;
update user set host = 'localhost' where user = 'root';

# 返回结果应为
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

验证一下修改是否成功。

select user, host from user;

# 返回结果应为 
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

(5)授权

MySQL 8版本中新增了一个“SYSTEM_USER“帐户类型,因为root用户没有SYSTEM_USER权限,所以如果不授权就执行修改密码命令,就会报error。

grant all privileges on *.* to 'root'@'localhost';

# 返回结果应为
Query OK, 0 rows affected (0.01 sec)

# 若不执行这一步,便会报错
ERROR 1227 (42000): Access denied; you need (at least one of) the SYSTEM_USER privilege(s) for this operation

(6)修改密码

以往MySQL Workbench支持的是“mysql_native_password“这种加密方式,但MySQL 8引入了新特性“caching_sha2_password”,这种密码加密方式MySQL Workbench有可能不支持。

所以这一步要用“mysql_native_password”的方式将密码改成MySQL Workbench支持的样子。

alter user 'root'@'localhost' identified with mysql_native_password by '(your password)';

# 返回结果应为
Query OK, 0 rows affected (0.01 sec)

(7)刷新权限

“flush privileges”的作用是:将当前user和privilige表中的用户信息/权限设置从mysql库(MySQL数据库的内置库)中提取到内存里。MySQL用户数据和权限有修改后,希望在"不重启MySQL服务"的情况下直接生效,那么就需要执行这个命令。通常是在修改ROOT帐号的设置后,怕重启后无法再登录进来,那么直接flush之后就可以看权限设置是否生效。而不必冒太大风险。

flush privileges;

# 返回结果应为
Query OK, 0 rows affected (0.01 sec)

(8)再次打开MySQL Workbench

这次再打开MySQL Workbench,会发现一开始删除的root账号又回来了。再次点击、登录,应该就能正常使用了。

References

[1] ubuntu20 安装mysql和workbench

[2] 如何在 Ubuntu 20.04 上安装 MySQLhttps://zhuanlan.zhihu.com/p/137339787)

[3] Ubuntu20.04安装Mysql(亲测有效,一定要按步骤来)

[4] Ubuntu20.04安装MySQL8.0

[5] Ubuntu下mysql-workbench连接mysql报“access denied for user root@localhost”

[6] mysql 解决 ERROR 1227 (42000): Access denied

[7] 成功解决:Workbench等客户端连接不上服务器的MySQL问题

[8] 安装 mysql 8.0后;root用户在客户端连接不上

这篇关于MySQL 8 & MySQL Workbench Installation on Ubuntu 20.04的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!