Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use mysql Database in docker

Shulou Source: shulou.com Published: 2022-06-01 22:18:35 09月12日 Update

This article to share with you is about how to use mysql database in docker, Xiaobian think it is very practical, so share it with you to learn, I hope you can gain something after reading this article, not much to say, follow Xiaobian to see it.

use

docker pull mysql

Get mysql mirror, then use

docker images

View the list of mirrors as follows

use

docker run -itd -P mysql bash

Start mysql image, docker run is the command to start the container;i is the interactive operation, t is a terminal, d means to run in the background, -P means to generate a random port locally to map mysql port 3306, mysql means to run mysql image, bash means to create an interactive shell.

use

docker ps -a

View docker images that are already running, as follows image

You can see that port 3306 of mysql mirror is bound to local port 32773, so if you want to access mysql database in docker in local area network, you need to use server IP:32773 to access it.

use the command

docker exec -it sick_hawking bash

Connect to mysql mirror,

docker exec

Docker is the connection command for the image, similar to ssh. Sick_hawking is the name of the image. Every time the image is started, it must have a name. The name can be manually specified or generated by itself.

After successful connection, as follows,

In the figure, you have entered the docker mysql image, using

service mysql status

Check mysql startup status, as follows,

mysql is not enabled and can be used

service mysql start

, as follows,

Enter mysql to verify that mysql starts successfully, as follows,

So far, mysql in docker has been successfully launched.

How to root this mysql externally?

For security, you need to set the password of the root account first, as follows

update user set authentication_string = password('root') where user = 'root';

Change the password for root to root. Then, because root execution in mysql is bound to localhost, root needs to be authorized, the code is as follows,

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; The above is how to use mysql database in docker. Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Mirror command port data database success name run interactive password more knowledge article generation utility security so far code background container Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MariaDB Shulou Information vpn Apple