docker run --detach \
--restart always \
--publish 3306:3306 \
--name mysql \
--volume /data/docker/mysql/data:/var/lib/mysql \
--volume /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime \
--env MYSQL_DATABASE=harbin \
--env MYSQL_RANDOM_ROOT_PASSWORD=yes \
--env MYSQL_USER=harbin \
--env MYSQL_PASSWORD=Gah6kuP7ohfio4 \
mysql:8.0.27 \
--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
- login with mysql linux client by socket
lwk@harbin:~$ mysql -u harbin -h localhost -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
lwk@harbin:~$
出现这个问题主要是由于没有安装mysql server,所以出现找不到的/var/run/mysqld/mysqld.sock
情况,安装了mysql server以后就好了。如果我们确实不需要安装mysql server,那么可以借助tcp方式来访问。
根据MySQL官方文档,在Linux中以mysql客户端命令访问服务器的过程中,如果期望以tcp方式访问,遇到需要访问本地服务的时候,要以127.0.0.1替代localhost。
To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1 (instead of localhost), or the IP address or name of the local server. You can also specify the transport protocol explicitly, even for localhost, by using the --protocol=TCP option. Examples:
mysql --host=127.0.0.1
mysql --protocol=TCP
If the server is configured to accept IPv6 connections, clients can connect to the local server over IPv6 using --host=::1. See Section 5.1.13, “IPv6 Support”.
lwk@harbin:~$ mysql -u harbin -h 127.0.0.1 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 56
Server version: 8.0.23 MySQL Community Server - GPL
Copyright (c) 2000, 2021, 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.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit
Bye
lwk@harbin:~$ mysql -u harbin -h ::1 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 57
Server version: 8.0.23 MySQL Community Server - GPL
Copyright (c) 2000, 2021, 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.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit
Bye
lwk@harbin:~$
- login with mysql linux client by tcp
lwk@harbin:~$ mysql -u harbin -h localhost -p --protocol=tcp
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23 MySQL Community Server - GPL
Copyright (c) 2000, 2021, 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.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit
Bye
lwk@harbin:~$ mysql -u root -h localhost -p --protocol=tcp
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.23 MySQL Community Server - GPL
Copyright (c) 2000, 2021, 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.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Reference