mysqldockerdatabase-migration

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


I am new to docker technologies. Still now I am using xampp for development of PHP. Recently, I have install docker on my localhost. I have gone through documentation. Now its time to implement practically. I know how to run docker images. But I want to learn best way to use mysql docker container for local development.

I am planning to use mysql docker container instead of mysql of xampp. Looking for help for 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.