Docker Install MySQL of 8 and 5.7)
- 2021-07-03 01:08:30
- OfStack
This article describes how to deploy an MySQL database and remote access configuration using Docker
Installing MySQL
Pull mirror image
Use the following command to pull the mirror image of the MySQL database:
$ sudo docker pull mysql # Pull the latest version of the image, which is currently MySQL 8 Version, tag For latest
$ sudo docker pull mysql:5.7 # Specify pull MySQL 5.7 Version
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: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 3306: 3306: Maps port 3306 of the container to port 3306 of the host. -v $PWD/conf:/etc/mysql/conf. d: Mount conf/my. cnf in the host's current directory to the/etc/mysql/ES40cnf of the container. -v $PWD/logs:/logs: Mounts the logs directory under the host's current directory to the/logs of the container. -v $PWD/data:/var/lib/mysql: Mount the data directory under the host's current directory to the/var/lib/mysql of the container. -e MYSQL_ROOT_PASSWORD=your-password: Initializes the password for the root user. Complicated passwords are recommended. -d mysql: The name of the image to be deployed, mysql: 5.7 if it is version 5.7
Configure remote access
Remote access to MySQL is a basic configuration, but pay attention to security issues when configuring, otherwise there will be security risks, especially for enterprise servers.
And need to pay attention to the server firewall to open port 3306, the server provider's security group also needs to open, otherwise it will not be accessible.
To configure remote access, first open the control terminal of MySQL, using the following command:
$ sudo docker exec -it mysql bash # Enter MySQL Container
$ mysql -uroot -p # Login MySQL After execution, enter the password to enter MySQL
$ use mysql; # Select to use mysql Database
MySQL 8 Configuration
CREATE USER 'username'@'%' IDENTIFIED BY 'password';
# Create 1 Account for remote access;
# {usernama} Is the user name for remote access login, and it is not recommended to use root;
# {password} Is the login password for remote access ;
# '%' It represents all IP If you can try to set the specified IP Or IP Segment
GRANT ALL ON *.* TO 'username'@'%';
# Give all permissions to previously created accounts
ALTER USER 'username'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
# Confirm to use the password here to log in to this account
FLUSH PRIVILEGES;
# Refresh permissions
The complete command is as follows:
CREATE USER 'james'@'%' IDENTIFIED BY '123456asd';
# Create 1 Account number -james For remote access;
GRANT ALL ON *.* TO 'james'@'%';
# Give all permissions to previously created accounts :james
ALTER USER 'james'@'%' IDENTIFIED WITH mysql_native_password BY '123456asd';
# Confirm use of password {123456asd} Log in to this account {james}
# Passwords are as complex and secure as possible.
FLUSH PRIVILEGES;
# Refresh permissions
After configuration, use Navicat tool or other tools for connection test.
MySQL 5.7 Configuration
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
# {usernama} Is the user name for remote access login, and it is not recommended to use root;
# {password} Is the login password for remote access ;
# '%' It represents all IP If you can try to set the specified IP Or IP Segment
FLUSH PRIVILEGES;
# Refresh permissions
After configuration, use Navicat tool or other tools to test the connection.
Note: If you cannot access, please note that Firewall 3306 port is open and that the port in the server provider's security group is open.