0

I want to run and open a mysql Cli in docker just with one command . Something like this is not working:

docker run --rm -it -p 33060:3306 --name mydb -e MYSQL_ROOT_PASSWORD=secret mysql mysql -p

I know I can connect to mysql after running my container this way

docker -it docker exec -it mydb mysql -p

but i want to do it in one liner.

Thanks

(Updated)*****

Seems that you can do it in version 8 calling MySQLsh at the end of the command. But unable to do it for previous versions

docker run --name=mk-mysql -p3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -it mysql/mysql-server:8.0.20 mysqlsh 

2 Answers 2

2

The database server and client are two separate programs. A container only runs one program, so you can't run both the server and the client in the same container, both as the main process. You could write a script that starts the container and then runs mysql to connect to it, but that's about the best you can do.

#!/bin/sh
docker run -d -p 33060:3306 --name mydb -e MYSQL_ROOT_PASSWORD=secret mysql
exec mysql --host=127.0.0.1 --port=33060 --connect-timeout=60 --wait --password

If you're trying to do this to create a database or do other first-time initialization, you can bind-mount an initialization script into /docker-entrypoint-initdb.d and it will run as part of the database setup (only the very first time the database is started).

# Create the storage for the database
# (delete and recreate to rerun the init script)
docker volume create mysql-data

docker run \
  -v mysql-data:/var/lib/mysql \
  -v $PWD/init.sql:/docker-entrypoint-initdb.d/init.sql \
  ... \
  mysql

If you're just trying to experiment with SQL commands, a serverless database like SQLite might fit your needs better.

Sign up to request clarification or add additional context in comments.

1 Comment

You can do it using version 8: ‘ docker run --name=mk-mysql -p3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -it mysql/mysql-server:8.0.20 mysqlsh ’
0

the -p parameter is for the ports to be published and should not be part of the -it interactive, that should be your error,

Have a read of the docker run command, in the docker documentation, https://docs.docker.com/engine/reference/run/

1 Comment

Using version 8 is possible but unable to call cli using version 5.7. (See updated question)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.