一、安装数据库(最新版)

Ubuntu中,默认情况下,只有最新版本的MySQL包含在APT软件包存储库中,要安装它,只需更新服务器上的包索引并安装默认包apt-get

#命令1
sudo apt-get update
#命令2
sudo apt-get install mysql-server

二、配置数据库

2.1初始化配置

sudo mysql_secure_installation

配置项较多,如下所示:

#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (我的选项)

#2
Please set the password for root here...
New password: (输入密码)
Re-enter new password: (重复输入)

#3
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...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的选项)

#4
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 (我的选项)

#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的选项)

#6
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 (我的选项)

2.2检查数据库状态

systemctl status mysql.service

显示以下内容则表示数据库服务正常

● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:>
     Active: active (running) since Tue 2020-09-29 09:40:27 CST; 33min ago
   Main PID: 13163 (mysqld)
     Status: "Server is operational"
      Tasks: 41 (limit: 9376)
     Memory: 344.9M
     CGroup: /system.slice/mysql.service
             └─13163 /usr/sbin/mysqld

9月 29 09:40:26 cs systemd[1]: Starting MySQL Community Server...
9月 29 09:40:27 cs systemd[1]: Started MySQL Community Server.

三、配置远程访问

在Ubuntu下MySQL缺省是只允许本地访问的,使用workbench连接工具是连不上的;在网上其他教程配置以后workbench依然显示无法访问,因此,如果你要其他机器也能够访问的话,需要进行如下可行配置;

3.1、在终端根用户进入

sudo mysql -u root

3.2、配置命令

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';

四、使用workbench连接数据库

在ubuntu软件商店中搜索workbench,进行安装,安装后启动,编辑默认root,填写你的用户名密码即可访问。

workbench汉化 :edit –> preferences –> appearance –> configure Fonts for : 选中Simplified Chinese OK 然后重启;