Docker installs MySQL and uses Navicat to connect

1. Pull the MySQL image

Mirror list before pulling the MySQL mirror:

insert image description here

  • The MySQL image can be pulled by the following command:
docker pull mysql

insert image description here

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 mysqlport 3306.
  • -e: Set environment variables.
  • MYSQL_ROOT_PASSWORD=root123456: MySQL rootpassword is specified.
  • -d mysql: Refers to running mysqlthe image, setting the container to run in the background all the time.

Run the screenshot:

insert image description here

[Supplementary knowledge]:

In Docker, -eenvironment 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 -eexamples when running Docker containers with the parameter:

  1. Set individual environment variables:

    docker run -e VAR_NAME=VAR_VALUE image_name
    

    VAR_NAMEThis will set an environment variable named in the container with a value of VAR_VALUE.

  2. Set multiple environment variables:

    docker run -e VAR1_NAME=VAR1_VALUE -e VAR2_NAME=VAR2_VALUE image_name
    

    VAR1_NAMEThis will set two environment variables and in the container VAR2_NAMEwith values ​​corresponding to VAR1_VALUEand respectively VAR2_VALUE.

  3. Set from environment variable file:

    docker run --env-file env_file_name image_name
    

    env_file_nameis a file containing key-value pairs of environment variables. Each row should contain a key-value pair, eg VAR_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

insert image description here

3.1 Enter the MySQL container

docker exec -it mysql-test /bin/bash

3.2 Enter MySQL

mysql -uroot -p
Enter password:root123456

insert image description here

3.3 View host and user

select host,user from mysql.user;

insert image description here

  • Make sure rootthat hostis %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 rootthe user, all ip can connect
  • WITH GRANT OPTION: allow cascading grants

4.3 Force Refresh Permissions

FLUSH PRIVILEGES;

insert image description here

5. The server configures the open port of 3306

How does windows open ports

insert image description here

insert image description here

  • 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
    

insert image description here

6. View Ubuntu IP

# 查看 IP 地址
ip addr

# 也可以用这个命令
ifconfig

insert image description here

insert image description here

7. Possible problems

  • Screenshots of possible problems :

img

  • 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:

insert image description here

8. Navicat connection to MySQL test

insert image description here

At this point, Docker installs MySQL and connects with Navicat officially ends!

Guess you like

Origin blog.csdn.net/m0_51913750/article/details/131520076
Recommended