How to Set Up MySQL Database with Docker | Run MySQL in Docker container (using Docker Desktop)

Опубликовано: 26 Октябрь 2024
на канале: LearningFromExperience
117
4

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


Смотрите видео How to Set Up MySQL Database with Docker | Run MySQL in Docker container (using Docker Desktop) онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь LearningFromExperience 26 Октябрь 2024, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 117 раз и оно понравилось 4 людям.