how to install mysql with docker

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章