On the previous post we created a
MySQL Docker Container with the following command:
docker run -d -p 3306:3306 --name=container_mysql8 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_ROOT_HOST=% \ image_mysql8 --default-authentication-plugin=mysql_native_password
The resulting container had the database “data” files stored inside the container. However you can also build a container that uses database files stored on a folder of your host machine.
We can define three possible scenarios:
If you already had a previous instance of a MySQL server running on your host machine you can use
the databases created on that instance on your docker container.
Lets say that, for example, you had been using MAMP on your machine, and you created several databases there.
On Mac OS X, the folder that contains those databases would be something like: “/Applications/MAMP/db/mysql57”
Now lets make a copy of those files to a new folder with the following commands on a terminal window:
cp -r /Applications/MAMP/db/mysql57 ~/mysql57
Then, we will use the parameter “-v ~/mysql57:/var/lib/mysql” to set this new folder as the location of the database files for the Docker Container.
In this example I used data files of a previous version of MySQL. The version 5.7. Then, when the database is initialized--on our container using the newer version 8--the data files will be upgraded. Please note that this upgrade will cause these database files to not be usable on the previous version (5.7)
docker run -d -p 3306:3306 --name=container_mysql8 -v ~/mysql57:/var/lib/mysql \ -e MYSQL_ROOT_HOST=% image_mysql8
docker: Error response from daemon: Mounts denied: The path /Users/USERNAME/mysql57 is not shared from OS X and is not known to Docker. You can configure shared paths from Docker -> Preferences... -> File Sharing. See https://docs.docker.com/docker-for-mac/osxfs/#namespaces for more info.
docker logs container_mysql8
2020-02-17T03:58:10.167661Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.19) starting as process 1 2020-02-17T03:58:10.563879Z 1 [System] [MY-011012] [Server] Starting upgrade of data directory. 2020-02-17T03:58:41.305954Z 2 [System] [MY-011003] [Server] Finished populating Data Dictionary tables with data. 2020-02-17T03:58:47.586804Z 5 [System] [MY-013381] [Server] Server upgrade from '50700' to '80019' started. 2020-02-17T03:59:11.822160Z 5 [System] [MY-013381] [Server] Server upgrade from '50700' to '80019' completed.
We can also use an empty folder on our host machine to store the database files.
Lets say that we have created the “my_db” folder on our home folder.
Then, we will use the parameter “-v ~/my_db:/var/lib/mysql” to set our local folder as the location of the database files for the Docker Container.
docker run -d -p 3306:3306 --name=container_mysql8 -v ~/my_db:/var/lib/mysql \ -e MYSQL_ROOT_HOST=% image_mysql8 --default-authentication-plugin=mysql_native_password
And search for the “GENERATED ROOT PASSWORD” text:
docker logs container_mysql8
[Entrypoint] MySQL Docker Image 8.0.19-1.1.15 [Entrypoint] No password option specified for new database. [Entrypoint] A random onetime password will be generated. [Entrypoint] Initializing database ... [Entrypoint] GENERATED ROOT PASSWORD: ydas[3mID0kyKOmzoHLIw)4HjUfAfter you login, you must assign a new password before you can start using the database.
#innodb_temp client-cert.pem mysql server-cert.pem auto.cnf client-key.pem mysql.ibd server-key.pem binlog.000001 ib_buffer_pool mysql.sock sys binlog.000002 ib_logfile0 mysql.sock.lock undo_001 binlog.index ib_logfile1 performance_schema undo_002 ca-key.pem ibdata1 private_key.pem ca.pem ibtmp1 public_key.pem
When you use an empty folder, you can also have the option to set the password.
This way a random password won't be generated, and you can start using your new database with the password you specified.
docker run -d -p 3306:3306 --name=container_mysql8 -v ~/my_db:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=root -e MYSQL_ROOT_HOST=% image_mysql8 --default-authentication-plugin=mysql_native_password