Create a database in a Docker container for local development
Start a Docker Container
$ docker run --name=goyuninfo --restart on-failure -d mysql/mysql-server
Connecting to MySQL Server from within the Container
Get initial root password:
$ docker logs goyuninfo 2>&1 | grep GENERATED
[Entrypoint] GENERATED ROOT PASSWORD: 5EjdAv(ucryHFyg3dOv,AmgUnZ
$ docker exec -it goyuninfo mysql -uroot -p
Because the MYSQL_ONETIME_PASSWORD option is true by default, after you have connected a mysql client to the server, you must reset the server root password by issuing this statement:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'your-new-password';
mysql> flush privileges;
If you want to pipe data into one of the command line tools, such as loading a dump, you will need to remove the -t from the command line, like this:
docker exec -i goyuninfo mysql -uroot < dumpfile.sql
Set up TCP/IP access to Docker containers
Find out the IP address of your virtual network interface on your Linux machine:
[ec2-user@ip-172-16-0-86 ~]$ ip addr show docker0
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:77:eb:d4:40 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:77ff:feeb:d440/64 scope link
valid_lft forever preferred_lft forever
The IP address assigned to my virtual Docker interface is 172.17.0.1, this is the endpoint your client application will connect from; this is the IP address you will use to create a user in MySQL.
Determine the IP address assigned to your container instance:
[ec2-user@ip-172-16-0-86 ~]$ docker inspect goyuninfo | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
IP address of the container instance is 172.17.0.2, so that the connection will be 172.17.0.1 <-> 172.17.0.2.
Create a user for the client program to connect from 172.17.0.1
mysql> create user root@172.17.0.1 identified by '';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on *.* to root@172.17.0.1 with grant option;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
Connect to your container instance as root with no password from your local mysql client:
[ec2-user@ip-172-16-0-86 ~]$ mysql -uroot -h 172.17.0.2
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 77
Server version: 8.0.22 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>
Comments
Post a Comment