Docker进入mysql容器执行数据导入
进入mysql
参考 https://medium.com/@sreejithezhakkad/how-i-set-up-laravel-in-docker-container-f80987559bc6
操作数据库前 如果数据库中,我们配置的没有env中的用户在数据库中 那么
GRANT ALL ON ***api.* TO 'root***'@'%' IDENTIFIED BY '7J**********5***ISGAhz';FLUSH PRIVILEGES;然后可以退出
EXIT;如果执行【参考https://blog.csdn.net/nzyalj/article/details/79204589】
select user,host from mysql.user;会看到
阐释:
Host的
%
标示匹配任何Host的名称可以参考
RENAME USER 'schwarzeni'@'localhost' TO 'schwarzeni'@'%';
然后编辑.env填入上述设置 数据库名,用户名,数据库密码
DB_CONNECTION=mysql DB_HOST=db DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=laraveluser DB_PASSWORD=my_password然后执行
docker-compose -f docker-compose.prod.yml exec app php artisan migrate登录可以
mysql -u schwarzeni –p
如果docker-compose.prod.yml中配置了,那用户和密码都已经设置好了的。不用再添加用户。
version: "3.7"
services:
app:
...
db:
image: mysql:5.7
container_name: haaapi-db
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD} #实际采用了7K****u***
MYSQL_USER: ${DB_USERNAME} #实际采用了 roothaa
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker-compose/mysql:/docker-entrypoint-initdb.d
networks:
- haaapin
nginx:
...
networks:
haaapin:
driver: bridge
上面这个在docker build并up启动的时候,db的用户和密码就配置成了.env中设置好的值了。
https://www.cnblogs.com/todarcy/p/11283882.html
****@laravel-docker****:~/****/***-api/docker-compose/mysql$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8b05998735cf mysql:5.7 "docker-entrypoint.s…" 12 hours ago Up 12 hours 3306/tcp, 33060/tcp ***api-db 4d50a6c67048 ***apii "docker-php-entrypoi…" 12 hours ago Up 12 hours 9000/tcp ***api-app dc8c9e9918a0 nginx:1.19.1-alpine "/docker-entrypoint.…" 12 hours ago Up 12 hours 0.0.0.0:80->80/tcp ***api-nginx 005652ce45ac travellist "docker-php-entrypoi…" 36 hours ago Up 18 hours 9000/tcp travellist-app 69a53c064d41 nginx:1.19.1-alpine "/docker-entrypoint.…" 36 hours ago Up 18 hours 0.0.0.0:8000->80/tcp travellist-nginx 66a2c4bdeb73 mysql:5.7 "docker-entrypoint.s…" 36 hours ago Up 18 hours 3306/tcp, 33060/tcp travellist-db ****@laravel-docker****:~/****/***-api/docker-compose/mysql$ docker exec –it ***api-db /bin/bash root@8b05998735cf:/# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 32 Server version: 5.7.30 MySQL Community Server (GPL) Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 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 | | performance_schema | | ***api | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> use ***api; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +-------------------------+ | Tables_in_***api | +-------------------------+ | ***** | | ***** | | failed_jobs | | images | | jobs | | likes | | migrations | | password_resets | | private_messages | | ***** | | users | | verify_email_tokens | | verify_new_email_tokens | | zip_***** | +-------------------------+ 14 rows in set (0.00 sec) mysql> select * from ****ls; Empty set (0.00 sec) mysql> #执行导入: #docker cp ./****.sql ***api-db:/tmp/ ****@laravel-docker****:~/****/***-api/docker-compose/mysql$ docker cp ./***api.sql ***api-db:/tmp/ ****@laravel-docker****:~/****/***-api/docker-compose/mysql$ docker exec –it ***api-db /bin/bash root@8b05998735cf:/# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 33 Server version: 5.7.30 MySQL Community Server (GPL) Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 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> use ***api;#注意要有;号 Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +-------------------------+ | Tables_in_***api | +-------------------------+ | ***** | | ***** | | failed_jobs | | images | | jobs | | likes | | migrations | | password_resets | | private_messages | | **** | | users | | verify_email_tokens | | verify_new_email_tokens | | zip_*****s | +-------------------------+ 14 rows in set (0.00 sec) mysql> source /tmp/***api.sql; Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 14 rows affected (0.07 sec) Query OK, 1 row affected (0.00 sec) Database changed Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.02 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 928 rows affected (0.20 sec) Records: 928 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 1183 rows affected (0.03 sec) Records: 1183 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.01 sec) Query OK, 5370 rows affected (0.35 sec) Records: 5370 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.02 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 20 rows affected (0.00 sec) Records: 20 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 14 rows affected (0.00 sec) Records: 14 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 6 rows affected (0.00 sec) Records: 6 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected, 1 warning (0.01 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 42 rows affected (0.01 sec) Records: 42 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 9 rows affected (0.01 sec) Records: 9 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 247 rows affected (0.02 sec) Records: 247 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 17367 rows affected (0.34 sec) Records: 17367 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql>