# 更新你的 ubuntu 依赖库索引 sudo apt update在更新后,你可以查看可用的 mysql-server 的版本。注意,mysql 在这里的名称为 mysql-server。如果你不介意版本的话,可以跳过下面的命令,直接开始安装。
# 查看 mysql-server 的可用版本 (可选) apt-cache policy mysql-server # 应该输出类似如下 kala@58baab1b577c:~$ apt-cache policy mysql-server mysql-server: Installed: (none) Candidate: 8.0.20-0ubuntu0.20.04.1 Version table: 8.0.20-0ubuntu0.20.04.1 500 500 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages 500 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages 8.0.19-0ubuntu5 500 500 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages开始安装
sudo apt install mysql-server在开始安装时,Ubuntu 会出现了个弹窗问你是不是要继续。这里需要输入 Y 告诉它你要继续。
kala@58baab1b577c:~$ sudo apt install mysql-server .... 中间省略 Do you want to continue? [Y/n] Y请注意,上面这行我们输入了 Y 然后回车,表示我们需要继续安装。在安装完毕后,有少数情况下 MySQL 不会自动启动。为了以防万一,请先确认 MySQL 已经被成功启动起来了
# 重启 MySQL 服务以保证 MySQL 已经启动 sudo service mysql restart # 输出为 root@58e152781375:/home/kala# sudo service mysql restart * Stopping MySQL database server mysqld [ OK ] * Starting MySQL database server mysqld su: warning: cannot change directory to /nonexistent: No such file or directory [ OK ]第二步 - 设置 MySQL
sudo mysql_secure_installation这个脚本开始运行时,会问我们几个问题,主要包括
root@58e152781375:/home/kala# 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: Y2.选择哪种强度的密码 0 = 低强度,1 = 中等强度, 2 = 强密码 (我们建议选择2)
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: 23.设置你的 root 密码。如果你选择了强密码,那么你的密码里必须有数字、大小写字母混合,且密码中不允许有常见英文单词出现。比如说,Ywie_8371jmchw 就是一个强密码。如果你实在不愿意用强密码,那在步骤 2 中需要选择 0 或者 1
Please set the password for root here. New password: Re-enter new password: Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y 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.其余的几个问题是加强安全系数用的,一路答 Y 代表 Yes 即可。它会做这么几个操作:
# 连接 MySQL 数据库 sudo mysql接下来,在 MySQL 里(注意光标前面的 mysql> 这代表我们已经在 MySQL 服务器上了)我们需要查看用户的鉴权设置
SELECT user,authentication_string,plugin,host FROM mysql.user; # 输出应该如下 mysql> SELECT user,authentication_string,plugin,host FROM mysql.user; +------------------+------------------------------------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+------------------------------------------------------------------------+-----------------------+-----------+ | debian-sys-maint | $A$005$6&hRoVc['"Ig %y24QdUap.naCDqqph1IImWumU6AWlSLhcwR/NlA7tbn5 | caching_sha2_password | localhost | | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | root | | auth_socket | localhost | +------------------+------------------------------------------------------------------------+-----------------------+-----------+ 5 rows in set (0.00 sec)注意看 root 用户这一行(第5行),它的 plugin 这一列就是我们刚说的 auth_socket,这是不允许用密码登录的。那么,我们用 ALTER USER SQL 语句来把它改掉,如下
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'YOUR_PASS_WORD';注意,上面这里的 YOUR_PASS_WORD 应该输入你在第二步中的密码。如果你输了另一个新的密码,那么你的 root 用户的密码会被改掉,这里很容易掉坑。
请留心,这里我们用 ALTER USER 命令把 root 用户的鉴权改为了 caching_sha2_password 方式。如果你看 MySQL 文档的话,会发现 MySQL 官方是推荐这个方式的。但是如果你需要用 PHP 或者 phpMyAdmin 的话,你需要把 caching_sha2_password 换成 mysql_native_password 否则会报错。也即是把上面的那句换成:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YOUR_PASS_WORD';最后的最后,我们用 FLUSH PRIVILEGES 命令让刚才的权限更改生效:
# 让鉴权生效 FLUSH PRIVILEGES # 输出应为 Query OK, 0 rows affected (0.01 sec)然后我们查看一下更改是不是成功:
SELECT user,authentication_string,plugin,host FROM mysql.user; # 输出应类似 mysql> SELECT user,authentication_string,plugin,host FROM mysql.user; +------------------+------------------------------------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+------------------------------------------------------------------------+-----------------------+-----------+ | debian-sys-maint | $A$005$6&hRoVc['"Ig %y24QdUap.naCDqqph1IImWumU6AWlSLhcwR/NlA7tbn5 | caching_sha2_password | localhost | | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password | localhost | | root | $A$005$7|YHi0 bd@p.Wn60IYYHvty9/pZiesuIT.UjXdpcz1LvXviI0Hxrw.tm3 | caching_sha2_password | localhost | +------------------+------------------------------------------------------------------------+-----------------------+-----------+ 5 rows in set (0.00 sec)请注意 root 这行,鉴权已经换成了 caching_sha2_password。这时我们可以用 ctrl + D 或者敲入 exit 来退出 MySQL 回到 Ubuntu 命令行。
sudo service mysql status # 输出应该类似 * /usr/bin/mysqladmin Ver 8.0.20-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu)) Copyright (c) 2000, 2020, 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. Server version 8.0.20-0ubuntu0.20.04.1 Protocol version 10 Connection Localhost via UNIX socket UNIX socket /var/run/mysqld/mysqld.sock Uptime: 42 min 5 sec Threads: 2 Questions: 27 Slow queries: 0 Opens: 157 Flush tables: 3 Open tables: 78 Queries per second avg: 0.010如果 MySQL 没有在运行的话,你可以用
sudo service mysql restart来重启它。
# -p 代表使用密码,-u root 代表用户为 root,最后的 version 代表我们想要查看 MySQL 版本,这里查看版本这个 # 动作只是为了验证 MySQL 是否可以连上而已。如果连不上,会报错 sudo mysqladmin -p -u root version # 这里它会询问你 root 用户的密码 Enter password: # 如果你的密码输入正确,那么输出应该类似 mysqladmin Ver 8.0.20-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu)) Copyright (c) 2000, 2020, 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. Server version 8.0.20-0ubuntu0.20.04.1 Protocol version 10 Connection Localhost via UNIX socket UNIX socket /var/run/mysqld/mysqld.sock Uptime: 52 sec Threads: 2 Questions: 6 Slow queries: 0 Opens: 115 Flush tables: 3 Open tables: 36 Queries per second avg: 0.115上面的输出说明你的 MySQL 正在健康地运行