📜  docker open terminal mysql server - SQL (1)

📅  最后修改于: 2023-12-03 15:00:29.066000             🧑  作者: Mango

Docker Open Terminal MySQL Server - SQL

Introduction

Docker is a containerization platform that allows developers to package an application and all its dependencies into a single, self-contained unit. One popular use case for Docker is to run MySQL servers locally for testing.

In this guide, we will walk through the process of opening a terminal in a Docker container running a MySQL server and executing SQL commands.

Prerequisites

To follow along with this guide, you will need the following:

  • Docker installed on your machine
  • Basic knowledge of the command line interface (CLI)
Step 1: Pull the MySQL Docker Image

Before we can run a MySQL server in a Docker container, we need to pull the MySQL Docker image. Run the following command in your terminal:

docker pull mysql

This will download the latest MySQL Docker image to your local machine.

Step 2: Start a MySQL Container

Now that we have the MySQL Docker image, we can start a new container using the following command:

docker run -d -p 3306:3306 --name mysql-server -e MYSQL_ROOT_PASSWORD=mysecretpassword mysql:latest

Here's what's happening in the command above:

  • The docker run command is used to start a new container.
  • The -d flag tells Docker to run the container in the background (detached mode).
  • The -p 3306:3306 flag maps port 3306 on the host machine to port 3306 in the container (MySQL's default port). This allows us to connect to the MySQL server using a database client on our local machine.
  • The --name mysql-server flag gives the container a name that we can use to refer to it later.
  • The -e MYSQL_ROOT_PASSWORD=mysecretpassword flag sets the MySQL root user's password to "mysecretpassword".
  • mysql:latest specifies the name and tag of the Docker image we want to use (latest version).
Step 3: Open a Terminal in the Container

To open a terminal in the container, run the following command:

docker exec -it mysql-server bash

This tells Docker to execute the bash command inside the mysql-server container, with the -it flag enabling an interactive terminal session.

Step 4: Connect to the MySQL Server

Now that we're inside the container's terminal, we can connect to the MySQL server using the mysql command:

mysql -u root -p

Enter the password you set earlier ("mysecretpassword") when prompted.

Step 5: Execute SQL Commands

Once you're connected to the MySQL server, you can execute SQL commands as you normally would. For example, you can create a new database with the following command:

CREATE DATABASE mydatabase;

You can also query an existing database, for example, to show all tables:

USE mydatabase;
SHOW TABLES;
Conclusion

In this guide, we've walked through the process of opening a terminal in a Docker container running a MySQL server and executing SQL commands. Docker provides an easy and convenient way to run isolated instances of MySQL servers for testing or development purposes. Happy coding!