mysqldockerdatabase-migration

How to use mysql docker container as mysql server for local development?


I am new to Docker, and still use XAMPP for PHP. Recently, I have installed Docker on my localhost. I have gone through the documentation. Now it's time to implement it practically. I know how to run Docker images, but I want to learn the best way to use the MySQL Docker image for local development.

I am planning to use a MySQL Docker container instead of MySQL in XAMPP. Looking for help with the best setup.


Solution

  • Its good to learn new technologies. You want use mysql container instead of mysql of xampp stack. You should prefer official image. You can search docker images at hub.docker.com You need to run this command from command line.

    docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=db-password -v /my/own/datadir:/var/lib/mysql mysql
    

    Above command will create docker container named mysql. You will be able to access this on 127.0.0.1:3306 using user root and password as you given in command. Your mysql data will be stored at /my/own/datadir directory. So your data will be retained after restart.

    Now its time to dump local mysql server data to container mysql. You can dump data using this command. Assuming that you have created backup dump file (say dbdump.sql for example). Copy dbdump.sql file to '/my/own/datadir/'

    Now run this command.

    docker exec -i -t mysql
    

    from inside of container write these comands.

    cd /var/lib/mysql
    mysql -uroot -p < dbdump.sql
    

    Note : You need to stop mysql of xampp before executing above commands. Enter password when asked.