Set up a MySQL environment on the cloud server and set up remote access users with different permissions
1. Install docker under CentOS7.6
-
update source
yum -y update
-
Install docker
yum -y install docker
-
Start docker and set it to start automatically at boot
systemctl start docker systemctl enable docker
-
Check docker version
docker info
2. Use docker to install mysql8.0
-
Download mysql8.0 image
docker pull mysql:8.0
-
View image
docker images
-
Create mount directory
mkdir -p /data/mysql8/conf mkdir -p /data/mysql8/data mkdir -p /data/mysql8/logs
-
Create the my.cnf file and place it in the /data/mysql/conf directory. Pay attention to the port number, character set, and time zone in the configuration file. Since I am not very good at setting my.cnf here, I will ignore it here. -
Create and start the container, please refer to it appropriately
docker run -d --name mysql8 \ -e MYSQL_ROOT_PASSWORD=123456 \ -v /data/mysql8/data:/var/lib/mysql \ -v /data/mysql8/logs:/var/log/mysql \ -p 53306:3306 \ --restart=unless-stopped \ mysql:8.0
Parameter explanation:
-d
: Let the container run in the background--name
: What to name the container-p
: Mapping the docke port number to the host port number Left: Host port number Right: Docker port number-e MYSQL_ROOT_PASSWORD=
:Set root user password-v
: Mount point. The left side is the mounting host directory and the right side is the container directory.--restart
: Specify the policy for automatic container restart.unless-stopped
: After the container is started, it will always restart automatically as long as it is not stopped manually.always
: The container always restarts automatically when it exits. Even if the container has been manually stopped under normal circumstances, it will be automatically restarted.on-failure[:max-retries]
: If the exit status of the container is not 0, it will automatically restart up to max-retries times (this parameter is optional and the default value is 5). If max-retries is set to 0, the container is automatically restarted an unlimited number of times.no
: Never restart the container
-
Enter the container:
docker exec -it 容器id /bin/bash
eg:
docker exec -it 00c1820f12ef /bin/bash
-
Enter mysql:
mysql -uroot -p
, then enter the password
3. Set up remote access users with different access rights
Here I re-ran a new mysql8 container
-
Enter the container
docker exec -it 788b531e4441 /bin/bash
-
Enter mysql
mysql -uroot -p
Then enter the password
-
Some simple operations to view the login usernames and corresponding matching hosts
# 列出所有数据库的列表 SHOW databases; #显示指定数据库的所有表,前提已经使用use选择了操作的数据库 USE mysql; SHOW TABLES; #查询用户表中可登录用户名和匹配的主机 SELECT User,Host from user;
Found a user
root
who can log in to the host%
, from any source. Since the password I set is very simple, we need to delete this user. Create other users with specific permissions to increase database security and allow root users to only log in locally on the server.123456
Delete this user:
DROP USER 'root'@'%';
-
Create a database for testing
datespase_test
CREATE DATABASE datespase_test;
-
Create user
ettian
, the host source that can log in is%
CREATE USER 'ettian'@'%' IDENTIFIED BY 'test_password';
test_password
Password for this user -
Grant
datespase_test
all permissions under the databasegrant all privileges on datespase_test.* to 'ettian'@'%';
-
Refresh permissions
flush privileges;
-
Use Navicat to connect, remember to open the corresponding port number in the server's firewall.
Since the local port number mapped by my container is 53307, I wrote it as 3306 in this picture, so the connection cannot be made. Edit the connection and change it to 53307.