WSL下Kali 2021 启用mysql服务并通过主机连接

安装了kali-linux-large包的wsl中,是安装了mysql和apache服务的,所以我决定直接使用wsl中的环境调试最近要写的一个网页demo,但是在实际操作中发现了一些问题,记录一下。

一、启动mysql服务

1.1 常规启动方式

对于我装在Vmware中的Kali来说,启动mysql服务非常容易,只需要

sudo service mysql start

然后就可以正常调用mysql服务了

1.2 WSL中的启动方式

当我用上面的方法直接执行时,遇到了下面的问题

┌──(kali㉿ZHY)-[~]
└─$ sudo service mysql start
mysql: unrecognized service
┌──(kali㉿ZHY)-[~]
└─$ mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)

从下面一行报错可以看出,系统是安装了mysql的,但是server没有正确启动,解决方法其实很简单,因为在kali中使用了mysql的分支MariaDB,所以应该直接启动mariadb,也就是执行

sudo service mariadb start

当然,如果为了好记,想要和之前一样启动,可以建立一个软链接,然后就可以以mysql的方式启动服务了。

sudo ln -s /etc/init.d/mariadb /etc/init.d/mysql
sudo service mysql start

原理其实也不复杂,参考service的手册

NAME
       service - run a System V init script

DESCRIPTION
       service  runs  a  System  V init script or systemd unit in as predictable an environment as possible, removing
       most environment variables and with the current working directory set to /.

       The SCRIPT parameter specifies a System V init script, located in /etc/init.d/SCRIPT, or the name of a systemd
       unit.  The existence of a systemd unit of the same name as a script in /etc/init.d will cause the unit to take
       precedence over the init.d script.  The supported values of COMMAND depend  on  the  invoked  script.  service
       passes  COMMAND  and OPTIONS to the init script unmodified. For systemd units, start, stop, status, and reload
       are passed through to their systemctl/initctl equivalents.

       All scripts should support at least  the  start  and  stop  commands.   As  a  special  case,  if  COMMAND  is
       --full-restart,  the script is run twice, first with the stop command, then with the start command. Note, that
       unlike update-rc.d(8), service does not check /usr/sbin/policy-rc.d.

       service --status-all runs all init scripts, in alphabetical order, with the status command. The status is [  +
       ]  for running services, [ - ] for stopped services and [ ? ] for services without a status command.  This op‐
       tion only calls status for sysvinit jobs.

EXIT CODES
       service calls the init script and returns the status returned by it.

FILES
       /etc/init.d
              The directory containing System V init scripts.

       /{lib,run,etc}/systemd/system
              The directories containing systemd units.

service命令用于对系统服务进行管理,在/etc/init.d/目录查找指定的服务脚本,然后调用该服务脚本来完成任务。对于kali而言,/etc/init.d/文件夹下并没有mysql的脚本,当然不能启动,建立软链接后就可以了。

不过我Vmware里的kali也没有这个软链接,如何直接启动的,现在还没有搞清楚。

1.3 关于上述报错的其他方法尝试

一开始碰到上面的问题,查了很多方法,有些直接无效,有些不能一劳永逸,都记录一下,万一以后遇到呢(还是不要遇到好

1.3.1 安装mysql-server

有些文章提到unrecognized service错误是因为本地只有mysql客户端,而没有服务端,需要安装server,然后尝试如下:

┌──(kali㉿ZHY)-[~]
└─$ sudo apt install mysql-server
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package mysql-server is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'mysql-server' has no installation candidate

事实上,这并没有定位到我这次问题的核心,因为kali装的本来就不是mysql-server,而是mariadb-server。

1.3.2 手动建立连接

对于ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)报错,有的文章提出手动创建mysqld文件夹,然后链接服务过去,操作如下:

sudo mkdir /run/mysqld
sudo chown mysql /run/mysqld

这种方式可以解决问题,但是存在一个非常大的问题:这是一个一次性的解决方案,每次重启系统都得重复执行。

┌──(kali㉿kali)-[~]
└─$ df
Filesystem     1K-blocks     Used Available Use% Mounted on
udev              976416        0    976416   0% /dev
tmpfs             202264     1168    201096   1% /run
/dev/sda1       81000912 10439408  66400892  14% /
tmpfs            1011312        0   1011312   0% /dev/shm
tmpfs               5120        0      5120   0% /run/lock
tmpfs             202260       64    202196   1% /run/user/1000

可以看到/run文件夹的文件系统是tmpfs,而tmpfs是一种内存文件系统,关机之后内存被清空,下一次启动就又得重新设置了。

二、主机连接

主机连接其实也相当于远程访问了,设置一下账号的访问主机就行了,在mysql内查找

MariaDB [(none)]> 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
MariaDB [mysql]> select host,user,password from user;
+-----------+-------------+-------------------------------------------+
| Host      | User        | Password                                  |
+-----------+-------------+-------------------------------------------+
| localhost | mariadb.sys |                                           |
| localhost | root        | invalid                                   |
| localhost | mysql       | invalid                                   |
+-----------+-------------+-------------------------------------------+
3 rows in set (0.001 sec)

默认账号的访问权限都是localhost,设置成%就可以远程访问了。

mysql> grant all privileges  on *.* to root@'%' identified by "";

现在就可以远程连接了。

 

 

 

 

上一篇:WSL Windows下使用linux 配置环境


下一篇:解决wsl无法同步本地代理的dns信息