本文是一篇 转载 文章,原文地址:https://zhangrenzheng.com/34.html
目前在 Ubuntu 18.04.3 LTS 通过 apt 安装的 MySql 默认版本为 5.7.28。

  1. root@nyc:~# mysql -V
  2. mysql Ver 14.14 Distrib 5.7.28, for Linux (x86_64) using EditLine wrapper

本文以下所有步骤均使用 root 用户在系统版本为 Ubuntu 18.04.3 LTS 的 DigitalOcean 云服务器上进行。
系统版本详细信息

  1. root@nyc:~# lsb_release -a
  2. No LSB modules are available.
  3. Distributor ID: Ubuntu
  4. Description: Ubuntu 18.04.3 LTS
  5. Release: 18.04
  6. Codename: bionic

安装 MySQL

首先运行以下命令更新软件包列表:

  1. apt update

然后安装 MySQL:

  1. apt install mysql-server

在执行以上命令的时候,不会像安装其他 MySQL 版本一样提示设置 root 密码以及进行其他任何配置的更改。以上命令执行结束后,MySQL 将自动启动。
查看 MySQL 运行状态:

  1. root@nyc:~# systemctl status mysql
  2. mysql.service - MySQL Community Server
  3. Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
  4. Active: active (running) since Fri 2019-12-06 15:32:48 CST; 9min ago
  5. Main PID: 22275 (mysqld)
  6. Tasks: 27 (limit: 2318)
  7. CGroup: /system.slice/mysql.service
  8. └─22275 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

配置 MySQL

MySQL 5.7 这种的全新的安装方式要求运行一个软件包附带的安全脚本。运行这个脚本会更改一些不太安全的默认选项,例如远程 root 登录和测试用户。
执行以下命令运行安全脚本:

  1. mysql_secure_installation

以下是运行脚本是的交互内容:

  1. root@ubuntu-bj:~# mysql_secure_installation
  2. Securing the MySQL server deployment.
  3. Connecting to MySQL using a blank password.
  4. VALIDATE PASSWORD PLUGIN can be used to test passwords
  5. and improve security. It checks the strength of password
  6. and allows the users to set only those passwords which are
  7. secure enough. Would you like to setup VALIDATE PASSWORD plugin?
  8. # 是否启用验证密码插件以检测密码强度?是
  9. Press y|Y for Yes, any other key for No: y
  10. There are three levels of password validation policy:
  11. LOW Length >= 8
  12. MEDIUM Length >= 8, numeric, mixed case, and special characters
  13. STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
  14. # 选择密码强度:2(强)
  15. Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
  16. Please set the password for root here.
  17. # 输入两遍 root 密码
  18. New password:
  19. Re-enter new password:
  20. Estimated strength of the password: 100
  21. Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
  22. By default, a MySQL installation has an anonymous user,
  23. allowing anyone to log into MySQL without having to have
  24. a user account created for them. This is intended only for
  25. testing, and to make the installation go a bit smoother.
  26. You should remove them before moving into a production
  27. environment.
  28. # 移除匿名登录用户?是
  29. Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
  30. Success.
  31. Normally, root should only be allowed to connect from
  32. 'localhost'. This ensures that someone cannot guess at
  33. the root password from the network.
  34. # 是否不允许 root 远程登录?否
  35. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
  36. ... skipping.
  37. By default, MySQL comes with a database named 'test' that
  38. anyone can access. This is also intended only for testing,
  39. and should be removed before moving into a production
  40. environment.
  41. # 移除测试(样本?)数据库?是
  42. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
  43. - Dropping test database...
  44. Success.
  45. - Removing privileges on test database...
  46. Success.
  47. Reloading the privilege tables will ensure that all changes
  48. made so far will take effect immediately.
  49. # 使得更改生效?是
  50. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
  51. Success.
  52. All done!

请注意:在 MySQL 5.7 以及之后的版本中, 尽管在运行安全脚本时已经设置好了 MySQL root 用户的密码,在使用 root 用户登录至 MySQL Shell 时不会使用密码认证,而是使用一个叫 auth_socket的插件认证。
以下演示使用 root 登录 MySQL Shell:

  1. root@nyc:~# mysql -u root -p
  2. Enter password: # 由于不是使用密码验证登录,这里随便输入便可以正确登录
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 6
  5. Server version: 5.7.28-0ubuntu0.18.04.4 (Ubuntu)
  6. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql>

调整 MySQL root 用户登录验证方式

如上面提到的,MySQL root 使用auth_socket插件而不是密码进行身份验证。所以当使用到其他数据库管理工具时,容易遇到很多错误。
为了使用密码以 root 用户身份连接到 MySQL ,您需要将其身份验证方法从 auth_socket 切换为mysql_native_password
进入 MySQL Shell 界面:

  1. mysql

接下来,使用以下命令检查每个MySQL用户帐户使用的身份验证方法:

  1. mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
  2. +------------------+-------------------------------------------+-----------------------+-----------+
  3. | user | authentication_string | plugin | host |
  4. +------------------+-------------------------------------------+-----------------------+-----------+
  5. | root | | auth_socket | localhost |
  6. | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
  7. | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
  8. | debian-sys-maint | *E19B559DDA1964F6C56B92185EC94FDA646E43E9 | mysql_native_password | localhost |
  9. +------------------+-------------------------------------------+-----------------------+-----------+
  10. 4 rows in set (0.00 sec)

在以上命令的执行结果中,可以看到 root 用户的验证方式为 auth_socket。要修改验证方式为密码验证,需要运行以下命令,其中 password 替换为密码。

  1. mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'kangkang';
  2. mysql> FLUSH PRIVILEGES;

注意: 这一操作会将安全脚本中设置的 MySQL root 用户密码修改。
再次检查 MySQL root 用户的身份验证方式,可以看到已经修改为 mysql_native_password

  1. mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
  2. +------------------+-------------------------------------------+-----------------------+-----------+
  3. | user | authentication_string | plugin | host |
  4. +------------------+-------------------------------------------+-----------------------+-----------+
  5. | root | *C80E73759CC86EB71DFFC81448098EE1D33437AA | mysql_native_password | localhost |
  6. | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
  7. | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
  8. | debian-sys-maint | *E19B559DDA1964F6C56B92185EC94FDA646E43E9 | mysql_native_password | localhost |
  9. +------------------+-------------------------------------------+-----------------------+-----------+
  10. 4 rows in set (0.00 sec)

在进行上述更改之后,可以使用 exit 退出 MySQL Shell。但是下次要进入 MySQL Shell 时,需要使用 mysql -u root -p,然后输入刚才设置的密码。

安装 phpMyAdmin

phpMyAdmin 是一个以PHP为基础,以Web-Base方式架构在网站主机上的MySQL的数据库管理工具,让管理者可用Web接口管理MySQL数据库。
这里将 phpMyAdmin 安装到默认站点下(通过 http://ip/phpmyadmim 访问)。
修改默认站点的服务器块 /etc/nginx/sites-available/default
1、找到 index 这一行,添加 index.php

  1. # Add index.php to the list if you are using PHP
  2. index index.html index.htm index.nginx-debian.html index.php;

2、找到以下内容,并且去除一些注释,其中 fastcgi_pass 要和 /etc/php/7.2/fpm/pool.d/www.conf 中一致(见 安装 php-fpm 说明的第2条

  1. # pass PHP scripts to FastCGI server
  2. location ~ \.php$ {
  3. include snippets/fastcgi-php.conf;
  4. # With php-fpm (or other unix sockets):
  5. fastcgi_pass unix:/run/php/php7.2-fpm.sock;
  6. # # With php-cgi (or other tcp sockets):
  7. # fastcgi_pass 127.0.0.1:9000;
  8. }

重载 Nginx:

  1. service nginx reload

切换到 /var/www/html 目录,下载 phpMyadmin:

  1. cd /var/www/html
  2. wget https://files.phpmyadmin.net/phpMyAdmin/4.7.9/phpMyAdmin-4.7.9-all-languages.zip

解压压缩包、重命名 phpMyAdmin 文件夹以及修改以下文件权限:

  1. unzip phpMyAdmin-4.7.9-all-languages.zip # 如果 unzip 没有安装 可以通过 apt install unzip 安装
  2. mv phpMyAdmin-4.7.9-all-languages/ phpmyadmin
  3. rm -f phpMyAdmin-4.7.9-all-languages.zip
  4. chown -R www-data:www-data .

到此,就可以通过 http://ip/phpmyadmim 访问 phpMyAdmin 了。