Docker - How to Connect to a MySQL Running Container Using MySQL Command Line Client
来源:互联网 发布:网络乞丐伴奏 编辑:程序博客网 时间:2024/05/21 17:51
1. What we want to achieve:
We used the official mysql image from Docker Hub (https://hub.docker.com/_/mysql/), and created a running database container.
Now we need to connect to the MySQL database using MySQL command line client, to do some database operation.
2. Implementation:
Reference: MySQL Image Official Doc
In the official guide, it states:
The following command starts another mysql container instance and runs the mysql
command line client against your original mysql container, allowing you to execute SQL statements against your database instance:
$ docker run -it --link some-mysql:mysql --rm mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'
The variables here are really confusing. Here is a more readable version of this command:
$ docker run -it --link <container_name>:<image_name> --rm <image_name> sh -c 'exec mysql -h"<container_ip_address>" -P"<mysql_port_number>" -u"<mysql_username>" -p"<mysql_password>"'
Now, lets use an example to explain how to find all the variables.
1. Get container_name, image_name, mysql_port_number:
$ docker ps
We can get:
container_name: romuiweb_db_1
image_name: mysql
container_port_number: 3306
2. Get container_ip_address:
$ docker inspect –format ‘{{ .NetworkSettings.IPAddress }}’ <container_id>
3. Get mysql_username, mysql_password:
When we start the mysql image, we set environment variables to decide the username and password.
username is default to root.
password is set by MYSQL_ROOT_PASSWORD, and we can find it in docker-compose.yml file:
So we can get:
mysql_username: root,
mysql_password: password.
4. Create the real command:
Now we have all the values of the variables. We can construct the real command:
$ docker run -it --link romuiweb_db_1:mysql --rm mysql sh -c 'exec mysql -h"172.17.0.2" -P"3306" -u"root" -p"password"'
5. Run the command, gain access to MySQL:
- Docker - How to Connect to a MySQL Running Container Using MySQL Command Line Client
- How to Connect to MySQL Using C#
- error 2003: cann't connect to mysql server & MySQL Command Line Client一闪而过
- How to Interactively Create a Docker Container
- How to Allow MySQL Client to Connect to Remote MySQL server
- MySQL Command Line Client
- MySQL Command Line Client
- using DTP to connect to MySQL
- How to program EMW3165 using command line interface?
- How to Get Code into a Docker Container
- mysql command line client语法
- PowerShell - How to enter a multi-line command
- How to Deleting a Storage Repository From the Command Line?
- How to Take Ownership of a File/Folder Using Command-Line in Windows 7 or newer version System
- Mysql Warning :Using a password on the command line...
- How to Navigate Linux Command Line Using Keybinding (Linux Ctrl Command Examples)
- Using Scala with JDBC to connect to MySQL
- wifi command line connect to ap
- 语音识别资料
- 谈谈使用 promise 时候的一些反模式
- 黑马程序员_多线程安全问题的解决方法
- Linux挂载点和文件系统类型介绍
- 关于屏幕适配
- Docker - How to Connect to a MySQL Running Container Using MySQL Command Line Client
- iOS 9.0 AFnetworking程序无法连接网络
- Java 继承详解
- ffmpeg 和 x264的参数对照
- OpenGL 画曲线
- 数据挖掘之标准流程
- hdu4545魔法串
- Unix简介(2)
- nyoj746 整数划分(四)