foreword
MySQL is the most popular open source database in the world, so this article will demonstrate how to install and configure MySQL on Docker.
environment
- CentOS 7
- Docker 20.10.10
Install
pull image
docker pull mysql
:
If you want to specify the version, add + after mysql 版本号
, for example:
docker pull mysql:8.0.16
Pull the latest version of MySQL directly here to
view the mirror
docker images
Create and start MySQL container
Create data directory and configuration file
Create a directory and data directory for placing MySQL configuration files on the host in advance, and grant permissions to avoid startup failures when mounting external configuration and data:
Create a directory and data directory for placing MySQL configuration files
mkdir -p /mydata/mysql/
Set folder permissions
chmod -R 755 /mydata/mysql
The first number represents the permissions of the file owner, the second number represents the permissions of other users who belong to the same user group as the file owner, and the third number represents the permissions of other user groups.
There are three types of permissions: read (r=4), write (w=2), and execute (x=1).
In summary, there are also readable and executable (rx=5=4+1), readable and writable (rw=6=4+2), and readable and writable (rwx=7=4+2+1). Therefore, chmod
755 sets the user's permissions to:
1. The file owner can read, write and execute --7
2. Other users who belong to the same user group as the file owner can read and execute --5
3. Other user groups can read executable
Create my.cnf configuration file
mkdir -p /mydata/mysql/conf
touch /mydata/mysql/conf/my.cnf
Edit the my.cnf configuration file
vi /mydata/mysql/conf/my.cnf
Add the following configuration content
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
secure_file_priv=/var/lib/mysql
remind
Whether you use my configuration or not, if your installation is a new version of MySQL, be sure to copy this sentence:
secure_file_priv=/var/lib/mysql
When the MySQL container is created and started for the first time, MySQL will access the /var/lib/mysql
folder. If there is no permission, it will not be able to start. After using docker ps, the mysql container cannot be seen running secure_file_priv
. /var/lib/mysql
and read and write /var/lib/mysql directory
Failed to access directory for --secure-file-priv. Please make sure that directory exists and is accessible by MySQL Server. Suppliedvalue : /var/lib/mysql-
files Make sure the directory exists and is accessible by the MySQL server. Provided value: /var/lib/mysql file
- The value of secure_file_priv is null, indicating that mysqld is not allowed to import | export
- The value of secure_file_priv is /tmp/, which means that the import|export of mysqld can only occur in the /tmp/ directory
- The value of secure_file_priv is empty, indicating that the import|export of mysqld is not restricted
Create and start MySQL container command
sudo docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:latest
Parameter Description:
-p 3306:3306
: map port 3306 of the container to port 3306 of the host--name mysql
: Define the container name as mysql-v /mydata/mysql/log:/var/log/mysql
: Mount MySQL's log folder to the host-v /mydata/mysql/data:/var/lib/mysql
: Mount the MySQL data folder to the host-v /mydata/mysql/conf:/etc/mysql
: Mount MySQL's configuration folder to the host-e MYSQL_ROOT_PASSWORD=root
: Initialize the root user password-d mysql:latest
: Select the image build container whose MySQL version is latest
View running containers
docker ps
Enter into the MySQL container to configure
enter command
docker exec -it 容器id ./bin/bash
connect to MySQL
Here, because the MySQL
default password we set by ourselves is, root
the p
following isroot
mysql -uroot -proot
Change MySQL password
use mysql library
use mysql
Modify the access host and password, etc., and set it to be accessible to all hosts
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
注意:mysql_native_password,mysql8.x版本必须使用这种模式,否则navicate无法正确连接
test connection
Before the test, please ensure that the firewall is turned off. If it is a cloud server, remember to open the 3306 rule
Linux turn off firewall
# 关闭
systemctl stop firewalld
# 禁止开机启动防火墙
systemctl disable firewalld
Cloud service open port 3306
Use Navicat to test the connection
Use SQLyog to test the connection