JyLie

vuePress-theme-reco JyLie    2017 - 2023
JyLie

Choose mode

  • dark
  • auto
  • light
主页
分类
  • API
  • HTML
  • css
  • vue
  • Linux
  • Docker
  • Webpack
  • WebGL
  • PixiJS
  • Github
  • BOM
  • XML
  • bug
  • ie
  • uniapp
  • IE
  • mysql
  • font
  • bom
  • canvas
  • video
  • html
  • JavaScript
  • js
  • 运算符
  • RegExp
  • 编码
  • MiniApp
  • nginx
  • Tool
  • node.js
  • cat
  • nodejs
  • protocol
  • URL
  • FLOW
  • DNS
  • Protocol
  • python
  • 安全
  • linux
  • shell
  • IDE
  • Packer
  • ViteJS
  • git
  • vendor
  • WebApp
  • WebView
  • Window API
  • webview
  • 规范
标签
时光轴
GitHub
author-avatar

JyLie

74

Article

79

Tag

主页
分类
  • API
  • HTML
  • css
  • vue
  • Linux
  • Docker
  • Webpack
  • WebGL
  • PixiJS
  • Github
  • BOM
  • XML
  • bug
  • ie
  • uniapp
  • IE
  • mysql
  • font
  • bom
  • canvas
  • video
  • html
  • JavaScript
  • js
  • 运算符
  • RegExp
  • 编码
  • MiniApp
  • nginx
  • Tool
  • node.js
  • cat
  • nodejs
  • protocol
  • URL
  • FLOW
  • DNS
  • Protocol
  • python
  • 安全
  • linux
  • shell
  • IDE
  • Packer
  • ViteJS
  • git
  • vendor
  • WebApp
  • WebView
  • Window API
  • webview
  • 规范
标签
时光轴
GitHub
  • wsl2 安装 mysql 实战

wsl2 安装 mysql 实战

vuePress-theme-reco JyLie    2017 - 2023

wsl2 安装 mysql 实战


JyLie 2021-05-13 mysql

持续更新...

# 前言

近期在 Win10 中装了 WSL(Windows Subsystem for Linux),由于最近搞 nodejs 时经常用到 mysql,于是试图在 WSL(Ubuntu)中安装 mysql 。下面为大家复现情景 L:

# 安装 mysql

sudo apt install mysql-server mysql-client
1

安装完成后尝试启动 MySQL

sudo mysql
1

接着出现报错

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
1

带着问题到 Google 求知若渴的寻找解决方案。

从前辈的经验可得知:在 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/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

根据上述配置在/etc/mysql/mysql.conf.d 目录下的 打开 mysqld.cnf 文件发现

wsl 的 mysqld.cnf 只有*[mysql]*这个内容,缺少了 socket 等配置信息

因此撸起键盘就是哒哒啊,输入内容

bind-address = 127.0.0.1

socket = /var/run/mysqld/mysqld.sock
1
2
3

sock 文件配置成功~~

接着,由于 WSL 的/var/run 下面没有 mysqld 目录,所以执行下述命令新增目录

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

接着重启 mysql 服务,当以为大功告成时,mysql 再次给了我一个晴天霹雳,shell 上冒出以下错误

  • mysql 启动失败:su: warning: cannot change directory to /nonexistent: No such file or directory

这种错误一般是 mysql 服务器异常关机导致的

解决方案如下:

# Ubuntu
sudo service mysql stop
sudo usermod -d /var/lib/mysql/ mysql
sudo service mysql start
1
2
3
4
# CentOS
sudo systemctl stop mysql.service
sudo usermod -d /var/lib/mysql/ mysql
sudo systemctl start mysql.service
1
2
3
4

至此,问题基本解决,下面还有 mysql 的安全检测问题,后面在更新吧~~

# 链接

  • 公众号-wsl2 安装 mysql 实战