51
43

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Dockerコンテナ内に入らずにMySQL公式コンテナの中にSQL等を実行するメモ

Last updated at Posted at 2016-05-13

MacからMySQLコンテナ内にコマンドを実行したかったり、SQLを実行したい時などに使うコマンドをメモっておきます

MySQLコンテナ起動

docker run --name container_mysql -e MYSQL_ROOT_PASSWORD=password -d -p 3306:3306 mysql

MySQLコンテナ接続

docker exec -it container_mysql bash

Macから直接コンテナ内のMySQLに接続する

docker exec -it container_mysql mysql -u user -p

MacからコンテナのMySQLにローカルのMySQLコマンドで接続する

mysql -h$(docker-machine ip iti-dev) -P 3306 -u root -p

Macから直接指定したDBにクエリを発行する

docker exec -it container_mysql mysql -u user -p database_name -e"create table kkk (id int);"

Macから直接SQLファイルを実行する

docker exec -it container_mysql mysql -u user -p database_name -e"$(cat xxx.sql)"

// またはこちら ※passwordが履歴に残ってしまうけど
cat jjj.sql | docker exec -i container_mysql mysql -u root -ppassword database_name
51
43
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
51
43

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?