This article directory
1. Pull the MySQL image
Mirror list before pulling the MySQL mirror:
- The MySQL image can be pulled by the following command:
docker pull mysql
This will download the latest version of the MySQL image into the virtual machine.
2. Create and run a MySQL container
docker run --name=mysql-test -itd -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root123456 -d mysql
Parameter Description:
- –name: Specifies the name of the container, which is convenient for entering the command line of the container later.
- -itd: Among them, i is interactive operation, t is a terminal, and d refers to running in the background.
- -p: means to generate a random port locally for mapping
mysql
port 3306. - -e: Set environment variables.
- MYSQL_ROOT_PASSWORD=root123456: MySQL
root
password is specified. - -d mysql: Refers to running
mysql
the image, setting the container to run in the background all the time.
Run the screenshot:
[Supplementary knowledge]:
In Docker, -e
environment variables can be set using parameters. Environment variables are configuration values that affect the runtime behavior of a container. These values can be used inside the container, for example when accessing a database in an application, you can use environment variables to specify database connection information.
Here are some -e
examples when running Docker containers with the parameter:
-
Set individual environment variables:
docker run -e VAR_NAME=VAR_VALUE image_name
VAR_NAME
This will set an environment variable named in the container with a value ofVAR_VALUE
. -
Set multiple environment variables:
docker run -e VAR1_NAME=VAR1_VALUE -e VAR2_NAME=VAR2_VALUE image_name
VAR1_NAME
This will set two environment variables and in the containerVAR2_NAME
with values corresponding toVAR1_VALUE
and respectivelyVAR2_VALUE
. -
Set from environment variable file:
docker run --env-file env_file_name image_name
env_file_name
is a file containing key-value pairs of environment variables. Each row should contain a key-value pair, egVAR_NAME=VAR_VALUE
. Docker will read the key-value pairs in the file and set the corresponding environment variables in the container.
By setting environment variables, you can configure various parameters required by the application in the container, such as database connection strings, API keys, etc. This makes it easy to deploy containers in different environments without hardcoding the application's configuration.
3. Verify that the MySQL container is created and running successfully
docker ps
3.1 Enter the MySQL container
docker exec -it mysql-test /bin/bash
3.2 Enter MySQL
mysql -uroot -p
Enter password:root123456
3.3 View host and user
select host,user from mysql.user;
- Make sure
root
thathost
is%
means that you can connect remotely.
4. MySQL enables remote access
4.1 Switch database
Note: This should be the default, it’s okay if you don’t switch, just switch it to be on the safe side
use mysql;
4.2 Assign remote access privileges to the root user
GRANT ALL PRIVILEGES ON *.* TO root@'%' WITH GRANT OPTION;
Parameter Description:
- GRANT: grant command
- ALL PRIVILEGES: all privileges of the current user
- ON: preposition
- . : The current user’s corresponding operation authority on all databases and tables
- TO: preposition
- 'root'@'%': the authority is assigned to
root
the user, all ip can connect - WITH GRANT OPTION: allow cascading grants
4.3 Force Refresh Permissions
FLUSH PRIVILEGES;
5. The server configures the open port of 3306
-
Open port 3306 on the virtual machine to allow inbound connections to MySQL. You can use the following command to allow inbound connections through the firewall:
ufw allow 3306
6. View Ubuntu IP
# 查看 IP 地址
ip addr
# 也可以用这个命令
ifconfig
7. Possible problems
- Screenshots of possible problems :
- The reason for the problem :
This is because the password encryption rule before MySQL8 is mysql_native_password
, and the encryption rule after MySQL8 is caching_sha2_password
, that is to say, if you want to use Navicat to connect to MySQL, you only need to change the password rule back to mysql_native_password
;
- The solution is as follows :
7.1 Enter the MySQL database
docker exec -it mysql-test /bin/bash
mysql -uroot -p
Enter password:root123456
7.2 Select database
use mysql;
7.3 Change the password encryption method
IDENTIFIED BY 'root123456': Enter the password when connecting, the password is root123456
ALTER USER 'root'@'%' IDENTIFIED BY 'root123456' PASSWORD EXPIRE NEVER;
7.4 Update user password
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root123456';
7.5 Refresh permissions
FLUSH PRIVILEGES;
Run the screenshot:
8. Navicat connection to MySQL test
At this point, Docker installs MySQL and connects with Navicat officially ends!