In this video, I'll walk you through setting up a MySQL database using Docker. We will cover creating the container, connecting to the MySQL database, creating tables, and inserting data. This tutorial includes both Docker CLI commands and Docker Desktop for easy implementation.
Commands Used in This Video:
Run a MySQL Container with Environment Variables:
docker run --name mysql_container -e MYSQL_ROOT_PASSWORD=password -p 3306:3306 -d mysql
This command pulls the latest MySQL image, creates a container named 'mysql_container', sets the root password to 'password', and exposes port 3306 for host connection.
Check Port Mapping for the Container:
docker port mysql_container
Use this command to verify which ports are mapped for the MySQL container.
Access the MySQL Container Shell:
docker exec -it mysql_container sh
This command starts a shell session inside the running MySQL container.
Connect to MySQL from Inside the Container:
mysql -u root -ppassword
Connects to MySQL as the root user with the password set earlier.
Create a New Database:
create database testdb;
Creates a new database named 'testdb' inside the MySQL container.
Use the New Database:
USE testdb;
Switches to the newly created 'testdb' database.
Create an Employee Table:
CREATE TABLE employee (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) NOT NULL, age INT, location VARCHAR(100));
Creates a table named 'employee' with fields for ID, name, age, and location.
Insert Data into the Employee Table:
INSERT INTO employee (name, age, location) VALUES ('Alice', 30, 'New York');
INSERT INTO employee (name, age, location) VALUES ('Bob', 25, 'San Francisco');
INSERT INTO employee (name, age, location) VALUES ('Peter', 30, 'San Francisco');
#docker #mysql #dockercontainer
Watch video How to Set Up MySQL Database with Docker | Run MySQL in Docker container (using Docker Desktop) online without registration, duration hours minute second in high quality. This video was added by user LearningFromExperience 26 October 2024, don't forget to share it with your friends and acquaintances, it has been viewed on our site 117 once and liked it 4 people.