Docker builds MySQL

1. Obtain the image

Docker pull mysql: 5.7Copy the code

2. Run the image

2.1 No Directory Mapping

Docker run -itd -p Host port :3306 -e MYSQL_ROOT_PASSWORD= ID of the password mirrorCopy the code

  • -p 3306:3306 Maps port 3306 of a container to port 3306 of a host
  • -e MYSQL_ROOT_PASSWORD=123456 To set environment variables, the user name is root, and the password is 123456

Data cannot be persisted without a directory mapping, and the container will have to run away

2.2 Mapping Directory

Docker run -itd -p Host port :3306 -v Host directory :/var/lib/mysql -e MYSQL_ROOT_PASSWORD= ID of the password imageCopy the code

D:/volume/mysql :/ var/lib/mysql :/volume/mysql


3. Remote connection

Into the container

Docker exec it < container id/ container name > bashCopy the code

The login

mysql -u root -p
Copy the code

Allow remote Login

Grant all PRIVILEGES on *.* to 'root'@'%' IDENTIFIED by 'MYSQL '; flush privileges;Copy the code

Test: Create a new database Test

This can be seen on the host if directory mapping is set

4. Frequently Asked Questions

4.1 Chinese garbled characters

Enter the container and execute

echo "character-set-server=utf8" >> /etc/mysql/mysql.conf.d/mysqld.cnf

service mysql restart
Copy the code

4.2 Reducing Memory Usage

Into the container

vim /etc/mysql/mysql.conf.d/mysqld.cnf
Copy the code

Append to mysqld.cnf:

performance_schema_max_table_instances=400

table_definition_cache=400

table_open_cache=256
Copy the code

Restart the container or MySQL service