问题如下:
在服务器用docker 安装启动mysql 容器 之后一直无法进入mysql
docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
显示已经安装成功
[root@179-180 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
18b734a8eb9c mysql "docker-entrypoint..." 2 hours ago Up 2 hours 0.0.0.0:3306->3306/tcp, 33060/tcp mysql-test
然后准备开始连接mysql 时,一直都是这样的:
[root@179-180 log]# mysql -h localhost -u root -p
-bash: mysql: 未找到命令
或者是:
[root@179-180 lib]# cd mysql
-bash: cd: mysql: 没有那个文件或目录
直到刚刚看了一张图片才发现,首先需要进入容器才能执行容器相关命令
docker exec -it mysql-test bash
[root@179-180 ~]# docker exec -it mysql-test bash
root@18b734a8eb9c:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 8.0.28 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.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| openser |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
...
对比进入容器前后 root 的标识
总结
多看多学