[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"global-header-tutorials-static":3,"article-how-to-setup-mysql-using-docker-on-ubuntu":4,"initial-similar-fetch":21},[],{"alias":5,"title":6,"description":7,"content":8,"thumbnail":9,"keywords":10,"categories":16,"tags":19,"createdAt":20},"how-to-setup-mysql-using-docker-on-ubuntu","How to pull and setup the MySQL using docker on Ubuntu","Set up MySQL using Docker on Ubuntu\u002FLinux System","{\"type\":\"doc\",\"content\":[{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"In this tutorial, we are going to set up the MySQL 8 server using Docker.\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"First, verify the Docker installation using the following command.\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"docker -v\"}]},{\"type\":\"heading\",\"attrs\":{\"textAlign\":null,\"level\":2},\"content\":[{\"type\":\"text\",\"text\":\"Pulling the MySQL image\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Let's pull MySQL 8 from Docker Hub\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"sudo docker pull mysql:8.0\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Output:\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":null},\"content\":[{\"type\":\"text\",\"text\":\"139627f60883: Pull complete \\nDigest: sha256:d0304ed9fdb64a3f6c7ad11a5fb4f13abfc10e6dfa3f288d652e7320c34df7f9\\nStatus: Downloaded newer image for mysql:8.0\\ndocker.io\u002Flibrary\u002Fmysql:8.0\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"We can verify if the MySQL Docker image is pulled or not by using the following command:\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"docker images\"}]},{\"type\":\"heading\",\"attrs\":{\"textAlign\":null,\"level\":2},\"content\":[{\"type\":\"text\",\"text\":\"Running MySQL container\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Use the following command to start the container\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"sudo docker run -d \\\\\\n  --name mysql-server \\\\\\n  -e MYSQL_ROOT_PASSWORD=your_strong_password \\\\\\n  -p 3306:3306 \\\\\\n  --restart unless-stopped \\\\\\n  mysql:8.0\"}]},{\"type\":\"bulletList\",\"content\":[{\"type\":\"listItem\",\"content\":[{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"-d: runs the container in the background\"}]}]},{\"type\":\"listItem\",\"content\":[{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"--name: this will be the name of your container\"}]}]},{\"type\":\"listItem\",\"content\":[{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"-e MYSQL_ROOT_PASSWORD: Set the root password\"}]}]},{\"type\":\"listItem\",\"content\":[{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"-p 3306:3306: Maps the port on Ubuntu to the port inside the container\"}]}]},{\"type\":\"listItem\",\"content\":[{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"--restart unless-stopped: the database starts automatically if the server reboots\"}]}]}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Once succeed, verify it wheather the container is running or not:\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"docker ps\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"We can see the output something like this:\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":null},\"content\":[{\"type\":\"text\",\"text\":\"455s5esf6f983   mysql:8.0   \\\"docker-entrypoint.s…\\\"   29 hours ago   Up 29 hours   0.0.0.0:3306->3306\u002Ftcp, [::]:3306->3306\u002Ftcp, 33060\u002Ftcp   mysql-server\"}]},{\"type\":\"heading\",\"attrs\":{\"textAlign\":null,\"level\":2},\"content\":[{\"type\":\"text\",\"text\":\"Persist MySQL data\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"If we delete the container, the MySQL data inside the container will be deleted.  This is not good in a production environment. So we need to map the directory on Ubuntu. \"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"sudo docker run -d \\\\\\n  --name mysql-server \\\\\\n  -v \u002Fmy\u002Flocal\u002Fdirectory:\u002Fvar\u002Flib\u002Fmysql \\\\\\n  -e MYSQL_ROOT_PASSWORD=your_strong_password \\\\\\n  -p 3306:3306 \\\\\\n  mysql:8.0\"}]},{\"type\":\"heading\",\"attrs\":{\"textAlign\":null,\"level\":2},\"content\":[{\"type\":\"text\",\"text\":\"Using a shared Docker network\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"When we are running an application that needs to connect to this container, we need to put it into the same Docker network. By default, standalone containers run on the \"},{\"type\":\"text\",\"marks\":[{\"type\":\"bold\"}],\"text\":\"bridge\"},{\"type\":\"text\",\"text\":\" network. In order to create the Docker network, use the following command.\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"docker network create tutorial-net\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Where \"},{\"type\":\"text\",\"marks\":[{\"type\":\"bold\"}],\"text\":\"tutorial-net\"},{\"type\":\"text\",\"text\":\" is the shared network name.\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Now update you docker command:\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"sudo docker run -d \\\\\\n  --name mysql-server \\\\\\n  --network tutorial-net \\\\\\n  -e MYSQL_ROOT_PASSWORD=your_strong_password \\\\\\n  -p 3306:3306 \\\\\\n  --restart unless-stopped \\\\\\n  mysql:8.0\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Now, when we set up the other container, we will use the same network so they can communicate with each other.\"}]},{\"type\":\"heading\",\"attrs\":{\"textAlign\":null,\"level\":2},\"content\":[{\"type\":\"text\",\"text\":\"Restrict Public Access\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Now, this will allow MySQL to connect remotely. To verify the access, use the following command.\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"nc -zv server-ip-address 3306\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"If this returns \"},{\"type\":\"text\",\"marks\":[{\"type\":\"code\"}],\"text\":\"Connection refused\"},{\"type\":\"text\",\"text\":\"  everything is good. If it succeeds, then we need to alow the mysql to connect only inside the local network, not remotely.\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"For this, we will set up MySQL to bind to localhost using \"},{\"type\":\"text\",\"marks\":[{\"type\":\"code\"}],\"text\":\"-p 127.0.0.1:3306:3306\"},{\"type\":\"text\",\"text\":\" . Here is the final updated version of the MySQL command for the Docker container.\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"sudo docker run -d \\\\\\n  --name mysql-server \\\\\\n  --restart unless-stopped \\\\\\n  --network tutorial-net \\\\\\n  -v \u002Fmy\u002Flocal\u002Fdirectory:\u002Fvar\u002Flib\u002Fmysql \\\\\\n  -e MYSQL_ROOT_PASSWORD=your_strong_password \\\\\\n  -p 127.0.0.1:3306:3306 \\\\\\n  mysql:8.0\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Now, again check or try to connect to MySQL; you will get \"},{\"type\":\"text\",\"marks\":[{\"type\":\"code\"}],\"text\":\"port 3306 (tcp) failed: Connection refused\"},{\"type\":\"text\",\"text\":\" \"}]},{\"type\":\"blockquote\",\"content\":[{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Note: If you want to give access to the MySQL server remotely with a specific IP address, make sure to set the firewall inside the server provider.\"}]}]},{\"type\":\"heading\",\"attrs\":{\"textAlign\":null,\"level\":3},\"content\":[{\"type\":\"text\",\"text\":\"Backup Mysql\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Make sure to back up MySQL databases before working on the MySQL container. Use the following command to back up the database.\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"marks\":[{\"type\":\"bold\"}],\"text\":\"Backup inside the server:\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"docker exec mysql-server mysqldump -u mysql_username -p'db_password' database_name > \u002Fopt\u002Fcsbyte_backup.sql\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"marks\":[{\"type\":\"bold\"}],\"text\":\"Backup  remotely in local system:\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"ssh root@server-ip-address \\\"docker exec mysql-server mysqldump -u mysql_username -p'db_password' database_name\\\" > ~\u002FDocuments\u002Fcsbyte_backup.sql\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"marks\":[{\"type\":\"bold\"}],\"text\":\"Backup remotely inside the remote server:\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"ssh root@server-ip-address \\\"docker exec mysql-server mysqldump -u mysql_username -p'db_password' database_name > \u002Fopt\u002Fcsbyte_backup.sql\\\"\"}]},{\"type\":\"heading\",\"attrs\":{\"textAlign\":null,\"level\":2},\"content\":[{\"type\":\"text\",\"text\":\"Accessing MySQL Shell\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"To create a database for MySQL users, we can use the MySQL shell.\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"sudo docker exec -it mysql-server mysql -u root -p\"}]},{\"type\":\"heading\",\"attrs\":{\"textAlign\":null,\"level\":2},\"content\":[{\"type\":\"text\",\"text\":\"Commands for MySQL server\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Check Status:\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"sudo docker ps\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Stop the database:\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"sudo docker stop mysql-server\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Where \"},{\"type\":\"text\",\"marks\":[{\"type\":\"bold\"}],\"text\":\"mysql-server\"},{\"type\":\"text\",\"text\":\" is the name of the MySQL container.\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Start the database:\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"sudo docker start mysql-server\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"View MySQL server logs:\"}]},{\"type\":\"codeBlock\",\"attrs\":{\"language\":\"bash\"},\"content\":[{\"type\":\"text\",\"text\":\"sudo docker logs mysql-server\"}]},{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":null},\"content\":[{\"type\":\"text\",\"text\":\"Using Docker to containerize MySQL will help make its installation easy. By utilizing volumes and port mapping, we can build a robust database environment.\"}]}]}","\u002Fuploads\u002Fthumbnails\u002F89199a59-7e48-4077-a579-11d03901c430_mysql-docke.jpeg",[11,12,13,14,15],"docker-hub","mysql","docker","ubuntu","deploy",[17,18],"Docker","MySQL",[12,13],"2026-05-02T13:53:32.698Z",[22],{"alias":23,"title":24,"description":25,"thumbnail":26,"createdAt":27,"tutorialAlias":28,"lessonAlias":28},"how-to-install-and-setup-the-docker-on-ubuntu","How to install and setup the docker on Ubuntu","Setting up Docker in Ubuntu Linux","https:\u002F\u002Fapi.csbyte.com\u002Fuploads\u002Feditor\u002F6c8baf8b-90b9-486c-8a92-56621ea6a234_show_container.png","2026-05-02T12:15:48.659Z",null]