WSL下安装mysql

在我的WSL(Ubuntu)中安装了mysql,试图进行命令行练习时,发现如下问题并解决。

安装mysql

  1. apt-get install mysql-server mysql-client
  2. 此时试图在shell中进入mysql会报错如下:
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

解决方法

使用如下命令即可:

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

问题原因分析

在WSL中,/var/run/mysqld/mysqld.sock文件不存在。执行vim /etc/mysql/my.cnf将会看到如下内容:

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
#
# * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

这里的mysql的配置在/etc/mysql/mysql.conf.d目录下的mysqld.cnf文件,打开可以看到:bind-address = 127.0.0.1 socket = /var/run/mysqld/mysqld.sock 等信息。

而WSL的/var/run下面没有mysqld目录,如果执行上述解决方法中的命令,目录和sock文件就都有了。

此时再输入mysql即可进入mysql的命令环境:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.20-0ubuntu0.20.04.1 (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.

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

mysql>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容