Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install MySQL in Docker

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to install Docker MySQL, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Install MySQL

Pull the image

Use the following command to pull the mirror of the MySQL database:

$sudo docker pull mysql # pull the latest version of the image, which is currently MySQL version 8. Tag specifies to pull MySQL version 5.7 for latest$ sudo docker pull mysql:5.7 #.

You can also use the search command to find other MySQL-related images, which identify the number of Stars, that is, popularity.

$sudo docker search mysql

Run MySQL

$sudo docker run-p 3306 PWD/conf:/etc/mysql/conf.d 3306\-name mysql\-v $PWD/conf:/etc/mysql/conf.d\-v $PWD/logs:/logs\-v $PWD/data:/var/lib/mysql\-e MYSQL_ROOT_PASSWORD=your-password\-d mysql

Command description:

-p 3306virtual 3306: map port 3306 of the container to port 3306 of the host.

-v $PWD/conf:/etc/mysql/conf.d: Mount the conf/my.cnf under the current directory of the host to the / etc/mysql/my.cnf of the container.

-v $PWD/logs:/logs: Mount the logs directory under the current directory of the host to the / logs of the container.

-v $PWD/data:/var/lib/mysql: Mount the data directory under the current directory of the host to the / var/lib/mysql of the container.

-e MYSQL_ROOT_PASSWORD=your-password: initializes the password of the root user. It is recommended to use a password with high complexity.

-d mysql: the name of the image to be deployed. If it is version 5.7, it is mysql:5.7.

Configure remote access

Remote access to MySQL belongs to the basic configuration, but we should pay attention to the security problems when configuring, otherwise there will be security risks, especially the enterprise servers should pay more attention to the security.

And you need to note that the server firewall needs to open port 3306, and the server provider's security group also needs to be turned on, otherwise it will be inaccessible.

To configure remote access, first open the control terminal of MySQL and turn it on with the following command:

$sudo docker exec-it mysql bash # enter the MySQL container $mysql-uroot-p # log in to MySQL, and enter the password to enter MySQL$ use mysql; # after execution. Choose to use mysql database.

MySQL 8 configuration

CREATE USER 'username'@'%' IDENTIFIED BY' password';# creates an account for remote access # {usernama} is the user name of remote access login, and root;# {password} is not recommended as the login password for remote access. #'% 'represents all IP, if you can set the specified IP or IP segment GRANT ALL ON *. * TO' username'@'%';# to grant all permissions to the previously created account ALTER USER 'username'@'%' IDENTIFIED WITH mysql_native_password BY' password'. # confirm the FLUSH PRIVILEGES;# refresh permission to log in to this account with the password here

The complete command is as follows:

CREATE USER 'james'@'%' IDENTIFIED BY' 123456asdholders # create an account-james for remote access; GRANT ALL ON *. * TO 'james'@'%';# grants all permissions to previously created accounts: jamesALTER USER' james'@'%' IDENTIFIED WITH mysql_native_password BY '123456asdholders # confirm that the password {123456asd} login to this account {james} # is as complex as possible and more secure. FLUSH PRIVILEGES;# refresh permissions

Once configured, you can use the Navicat tool or other tools to test the connection.

MySQL 5.7configuration

GRANT ALL PRIVILEGES ON *. * TO 'username'@'%' IDENTIFIED BY' password' WITH GRANT OPTION;# {usernama} is the user name of remote access login, and root;# {password} is not recommended as the login password for remote access; #'% 'represents all IP, if you can set the specified IP or IP segment FLUSH PRIVILEGES;# refresh permission as far as possible

Once configured, you can use the Navicat tool or other tools to test the connection.

Note: if you cannot access, please pay attention to whether port 3306 of the firewall is open, and whether the port in the security group of the server provider is open.

The above is all the contents of the article "how to install MySQL in Docker". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report