WSL2 Ubuntu 子系统安装 MySQL8 的一些小问题

运行命令安装 sudo apt install -y mysql-server
输出的日志,可以看到安装完之后启动没成功.

...
...
emitting double-array: 100% |###########################################|
reading /usr/share/mecab/dic/ipadic/matrix.def ... 1316x1316
emitting matrix      : 100% |###########################################|

done!
update-alternatives: using /var/lib/mecab/dic/ipadic-utf8 to provide /var/lib/mecab/dic/debian (mecab-dictionary) in auto mode
Setting up libhttp-message-perl (6.22-1) ...
Setting up mysql-server-8.0 (8.0.25-0ubuntu0.20.04.1) ...
invoke-rc.d: could not determine current runlevel
 * Stopping MySQL database server mysqld                                                                                                                    [ OK ]
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Renaming removed key_buffer and myisam-recover options (if present)
mysqld will log errors to /var/log/mysql/error.log
mysqld is running as pid 587
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
invoke-rc.d: could not determine current runlevel
Setting up libcgi-fast-perl (1:2.15-1) ...
Setting up mysql-server (8.0.25-0ubuntu0.20.04.1) ...
Processing triggers for systemd (245.4-4ubuntu3.4) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...
nathan@DESKTOP-A6TDDCF:~$

启动没成功,vi /var/log/mysql/error.log 看其中的错误信息

2021-07-02T04:04:41.896709Z 0 [ERROR] [MY-011292] [Server] Plugin mysqlx reported: 'Preparation of I/O interfaces failed, X Protocol won't be accessible'
2021-07-02T04:04:41.896883Z 0 [ERROR] [MY-011300] [Server] Plugin mysqlx reported: 'Setup of socket: '/var/run/mysqld/mysqlx.sock' failed, can't create lock file /var/run/mysqld/mysqlx.sock.lock'
...
...
2021-07-02T04:04:45.101529Z 0 [ERROR] [MY-011300] [Server] Plugin mysqlx reported: 'Setup of socket: '/var/run/mysqld/mysqlx.sock' failed, can't create lock file /var/run/mysqld/mysqlx.sock.lock'

没有 /var/run/mysqld 文件夹,没法创建 mysqlx.sock 文件。

手动创建并授权给 mysql 用户。

sudo mkdir -p /var/run/mysqld
sudo chown mysql /var/run/mysqld/
sudo service mysql restart

还是有错误,这种错误一般是 mysql 服务器异常关机导致的,这里明显不是,但解决方法一样的。

nathan@DESKTOP-A6TDDCF:~$ service mysql status
 * MySQL is stopped.
nathan@DESKTOP-A6TDDCF:~$ sudo service mysql start
 * Starting MySQL database server mysqld                                                                                                                           
su: warning: cannot change directory to /nonexistent: No such file or directory
																																										[ OK ]

参考 stackoverflow 上的解释和解决方法,更改 mysql 用户 home 目录再启动

sudo service mysql stop
sudo usermod -d /var/lib/mysql/ mysql
sudo service mysql start

启动成功

nathan@DESKTOP-A6TDDCF:~$ sudo usermod -d /var/lib/mysql/ mysql
usermod: user mysql is currently used by process 1984
nathan@DESKTOP-A6TDDCF:~$ sudo service mysql start
 * Starting MySQL database server mysqld                                                                                                                    [ OK ]
nathan@DESKTOP-A6TDDCF:~$ pstree
init─┬─init─┬─mysqld_safe───mysqld───37*[{mysqld}]
     │      └─wslconnect───bash───bash───pstree
     ├─init───wslconnect───bash───vi
     └─{init}
nathan@DESKTOP-A6TDDCF:~$ sudo service mysql status
 * /usr/bin/mysqladmin  Ver 8.0.25-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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.25-0ubuntu0.20.04.1
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/run/mysqld/mysqld.sock
Uptime:                 12 min 13 sec

Threads: 2  Questions: 10  Slow queries: 0  Opens: 436  Flush tables: 3  Open tables: 27  Queries per second avg: 0.013
nathan@DESKTOP-A6TDDCF:~$

安装 mysql8 时 root 是没给设置密码的,可以直接用 sudo mysql 命令以 root 用户登录

nathan@DESKTOP-A6TDDCF:~$ sudo mysql
[sudo] password for nathan:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 8.0.25-0ubuntu0.20.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

看下 root 的密码,authentication_string 字段空的

mysql> select * from user where user = 'root'\G
*************************** 1. row ***************************
                    Host: localhost
                    User: root
             Select_priv: Y
             Insert_priv: Y
             Update_priv: Y
             Delete_priv: Y
             Create_priv: Y
               Drop_priv: Y
             Reload_priv: Y
           Shutdown_priv: Y
            Process_priv: Y
               File_priv: Y
              Grant_priv: Y
         References_priv: Y
              Index_priv: Y
              Alter_priv: Y
            Show_db_priv: Y
              Super_priv: Y
   Create_tmp_table_priv: Y
        Lock_tables_priv: Y
            Execute_priv: Y
         Repl_slave_priv: Y
        Repl_client_priv: Y
        Create_view_priv: Y
          Show_view_priv: Y
     Create_routine_priv: Y
      Alter_routine_priv: Y
        Create_user_priv: Y
              Event_priv: Y
            Trigger_priv: Y
  Create_tablespace_priv: Y
                ssl_type:
              ssl_cipher: NULL
             x509_issuer: NULL
            x509_subject: NULL
           max_questions: 0
             max_updates: 0
         max_connections: 0
    max_user_connections: 0
                  plugin: auth_socket
   authentication_string:
        password_expired: N
   password_last_changed: 2021-07-02 12:04:33
       password_lifetime: NULL
          account_locked: N
        Create_role_priv: Y
          Drop_role_priv: Y
  Password_reuse_history: NULL
     Password_reuse_time: NULL
Password_require_current: NULL
         User_attributes: NULL
1 row in set (0.00 sec)

mysql>

创建一个用户

mysql> create user 'nathan'@'%' identified by 'test!#123';
Query OK, 0 rows affected (0.04 sec)

## 修改的话用 alter user 'nathan'@'%' identified by 'test!#123';
## 授权 grant all privileges on databasename.tablename to 'user'@'host' identified by 'password';

mysql> GRANT ALL PRIVILEGES ON *.* TO 'nathan'@'%';
Query OK, 0 rows affected (0.03 sec)

mysql> select * from user where user='nathan'\G
*************************** 1. row ***************************
                    Host: %
                    User: nathan
             Select_priv: Y
             Insert_priv: Y
             Update_priv: Y
             Delete_priv: Y
             Create_priv: Y
               Drop_priv: Y
             Reload_priv: Y
           Shutdown_priv: Y
            Process_priv: Y
               File_priv: Y
              Grant_priv: N
         References_priv: Y
              Index_priv: Y
              Alter_priv: Y
            Show_db_priv: Y
              Super_priv: Y
   Create_tmp_table_priv: Y
        Lock_tables_priv: Y
            Execute_priv: Y
         Repl_slave_priv: Y
        Repl_client_priv: Y
        Create_view_priv: Y
          Show_view_priv: Y
     Create_routine_priv: Y
      Alter_routine_priv: Y
        Create_user_priv: Y
              Event_priv: Y
            Trigger_priv: Y
  Create_tablespace_priv: Y
                ssl_type:
              ssl_cipher: NULL
             x509_issuer: NULL
            x509_subject: NULL
           max_questions: 0
             max_updates: 0
         max_connections: 0
    max_user_connections: 0
                  plugin: caching_sha2_password
   authentication_string: $A$005$Y}G[/z|        hz/?:RGYa4.xc4oW4iIm1SAax1BjY9zvwaYKrRK/ShSfkM7KF2/
        password_expired: N
   password_last_changed: 2021-07-02 15:35:14
       password_lifetime: NULL
          account_locked: N
        Create_role_priv: Y
          Drop_role_priv: Y
  Password_reuse_history: NULL
     Password_reuse_time: NULL
Password_require_current: NULL
         User_attributes: NULL
1 row in set (0.00 sec)

mysql>

使用 Navicat 客户端连接有错误
在这里插入图片描述

因为 mysql8 之前的版本中加密规则是 mysql_native_password,而在mysql8之后,加密规则是 caching_sha2_password,我这个 Navicat 不支持,改下加密规则,方法如下

use mysql; #选择数据库
# 远程连接请将'localhost'换成'%'

ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; #更改加密方式

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; #更新用户密码

FLUSH PRIVILEGES; #刷新权限

根据自己实际情况设置

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> ALTER USER 'nathan'@'%' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.01 sec)

mysql> ALTER USER 'nathan'@'%' IDENTIFIED WITH mysql_native_password BY 'test!#123';
Query OK, 0 rows affected (0.01 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

mysql>

客户端用 127.0.0.1 IP 能访问,但其他内网 IP 或公网 IP 都不行,比如我这里的虚拟机 IP 是 192.168.50.28,因为 MySQL 默认的配置就是绑定的 127.0.0.1,监听端口信息如下

nathan@DESKTOP-A6TDDCF:~$ ss -lnp|grep 3306
tcp   LISTEN 0       70                                    127.0.0.1:33060                    0.0.0.0:*
tcp   LISTEN 0       151                                   127.0.0.1:3306                     0.0.0.0:*

修改绑定 IP sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf ,把 127.0.0.1 改为 0.0.0.0

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address           = 127.0.0.1
bind-address            = 0.0.0.0
mysqlx-bind-address     = 127.0.0.1

sudo service mysql restart 重启数据库就可连接了。


版权声明:本文为u012809062原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。