WSL下安裝mysql


WSL下安裝mysql

近期我在Win10中裝了WSL(Windows Subsystem for Linux),又在學習mysql,試圖在WSL(Ubuntu)中安裝mysql進行命令行練習時,發現如下問題並解決。

1. 安裝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)

2. 解決方法

使用如下命令即可:

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

3. 問題原因分析

在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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM