代码之家  ›  专栏  ›  技术社区  ›  Mona Jalal

错误2002(HY000):无法通过OSX中的套接字'/tmp/MySQL.sock'(2)连接到本地MySQL服务器

  •  0
  • Mona Jalal  · 技术社区  · 4 年前

    我正试图使用XAMPP conf文件中的用户通行证登录mysql,但我收到了以下错误:

    mysql -u mysql -p;                                                                                                                            1 ✘  at 11:48:02 AM 
    Enter password: 
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
    

    我已进入 1234 作为conf文件中的密码 /Applications/XAMPP/xamppfiles/etc/my.cnf 下面是:

    # The following options will be passed to all MySQL clients
    [client]
    password        =1234
    port            =3306
    socket          =/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
    
    # Here follows entries for some specific programs
    
    # The MySQL server
    default-character-set=utf8mb4
    [mysqld]
    user=mysql
    port=3306
    socket          =/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
    key_buffer=16M
    max_allowed_packet=1M
    table_open_cache=64
    sort_buffer_size=512K
    net_buffer_length=8K
    read_buffer_size=256K
    read_rnd_buffer_size=512K
    myisam_sort_buffer_size=8M
    

    此外,以下是运行mysql守护进程的信息:

    ps aux | grep mysql                                                                                                                      PIPE|2 ✘  at 11:55:38 AM 
    mona              6189   0.0  0.0  4286736    724 s003  S+   11:56AM   0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox mysql
    _mysql            5707   0.0  0.0  4589008   8172   ??  S    11:53AM   0:00.10 /Applications/XAMPP/xamppfiles/sbin/mysqld --basedir=/Applications/XAMPP/xamppfiles --datadir=/Applications/XAMPP/xamppfiles/var/mysql --plugin-dir=/Applications/XAMPP/xamppfiles/lib/mysql/plugin/ --log-error=/Applications/XAMPP/xamppfiles/var/mysql/goku.err --pid-file=/Applications/XAMPP/xamppfiles/var/mysql/goku.pid --socket=/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock --port=3306
    root              5555   0.0  0.0  4308708    844   ??  S    11:53AM   0:00.02 /bin/sh /Applications/XAMPP/xamppfiles/bin/mysqld_safe --datadir=/Applications/XAMPP/xamppfiles/var/mysql --pid-file=/Applications/XAMPP/xamppfiles/var/mysql/goku.pid
    

    以下是我在XAMPP中看到的内容:

    enter image description here

    我有OSX Catalina 10.15.7(19H1713)。

    取消对密码的注释并将其设置为 1234 我在XAMPP UI中停止并启动了mySQL,但仍然无法登录。

    我输入如下,但仍然没有进入mysqlshell: mysql -u mysql -h 127.0.0.1 -port 3306 -p 1234;

    我看到了这样的东西:

    mysql: [Warning] Using a password on the command line interface can be insecure.
    /usr/local/mysql/bin/mysql  Ver 8.0.29 for macos12 on x86_64 (MySQL Community Server - GPL)
    Copyright (c) 2000, 2022, 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.
    
    Usage: /usr/local/mysql/bin/mysql [OPTIONS] [database]
      -?, --help          Display this help and exit.
      -I, --help          Synonym for -?
      --auto-rehash       Enable automatic rehashing. One doesn't need to use
                          'rehash' to get table and field completion, but startup
                          and reconnecting may take a longer time. Disable with
                          --disable-auto-rehash.
                          (Defaults to on; use --skip-auto-rehash to disable.)
    

    在没有传递端口信息的情况下(因为3306是默认端口),我得到了这个错误,尽管 1234 是我在examplep的mysql-conf文件中设置的密码):

    mysql -u mysql  -h 127.0.0.1 -p;                                                                                                              1 ✘  at 12:29:02 PM 
    Enter password: 
    ERROR 1045 (28000): Access denied for user 'mysql'@'localhost' (using password: YES)
    
    1 回复  |  直到 4 年前
        1
  •  -1
  •   Mona Jalal    4 年前
    mysql -u root  -h 127.0.0.1 -p;                                                                                                               1 ✘  at 12:34:13 PM 
    Enter password:
    

    当它要求输入密码时,按enter键,您现在就在mysqlshell中了。您可以在此处查看现有用户的列表:

    
    mysql> SELECT user FROM mysql.user;
    +------+
    | user |
    +------+
    | root |
    | root |
    |      |
    | pma  |
    | root |
    +------+
    

    现在,创建一个用户并为其设置密码,例如:

    mysql> CREATE USER 'mona'@'localhost' IDENTIFIED BY '1234';
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> SELECT user FROM mysql.user;
    +------+
    | user |
    +------+
    | root |
    | root |
    |      |
    | mona |
    | pma  |
    | root |
    +------+
    6 rows in set (0.00 sec)
    

    现在退出mysqlshell,在提示符前输入以下命令:

    mysql -u mona -h 127.0.0.1 -p; 和输入密码 1234

    现在输入用户的mysqlshell mona

    查看更多信息 this tutorial