1 Dependence
Start the road to containerization.
2 Containerized MySQL
2.1 View MySQL image
docker search mysql
2.2 Specified version: 5.7.30
View through the official website: https://hub.docker.com/
docker pull mysql:5.7.30
2.3 path mount
The container path is mounted to the host machine.
- new host path
mkdir -p /home/xindaqi/mysql/data /home/xindaqi/mysql/logs /home/xindaqi/mysql/conf
serial number | path | describe |
---|---|---|
1 | /home/xindaqi/mysql/data | Data stored by MySQL |
2 | /home/xindaqi/mysql/logs | mysqllog |
3 | /home/xindaqi/mysql/conf | MySQL configuration |
- Add configuration file: my.cnf
/home/xindaqi/mysql/conf/my.cnf
[mysqld]
user=mysql
bind-address = 0.0.0.0
character-set-server=utf8
default_authentication_plugin=mysql_native_password
expire_logs_days=7
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
max_connections=1000
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
2.4 run
serial number | parameter | describe |
---|---|---|
1 | p | Service port mapping, host port: container port |
2 | name | service name |
3 | v | Path mount, host path: path inside the container |
4 | e | MySQL root account password |
- run in the foreground
docker run -p 3306:3306 \
--name mysql \
-v /home/xindaqi/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-v /home/xindaqi/mysql/logs:/var/log/mysql \
-v /home/xindaqi/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=my$123456 \
mysql:5.7.30
- Background process
docker run -p 3306:3306 \
--name mysql \
-v /home/xindaqi/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-v /home/xindaqi/mysql/logs:/var/log/mysql \
-v /home/xindaqi/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=my$123456 \
-d mysql:5.7.30
2.5 View container
docker container ls | grep mysql
2.6 Access to the container
# 格式
docker exec -it container_id /bin/bash
# 样例
docker exec -it 916f93180fb2 /bin/bash
2.7 Log in to MySQL: in Ubuntu
- View system IP
ifconfig
Use the network IP of enth0 to log in to MySQL.
- Login to MySQL
mysql -h 172.22.75.234 -uroot -pmy$123456
2.8 Create a new user
It is used for other services,
because when you log in as root, you cannot access MySQL, so create a new user.
# 新增用户:格式create user username@ip identified by password
create user 'mysql-username'@'%' identified by 'my$123456';
serial number | parameter | describe |
---|---|---|
1 | mysql-username | username |
2 | % | IP, where % means to allow all IP connections |
3 | my$123456 | password |
# 授权,格式:grant operation on database to username@ip identified by password
grant all on *.* to 'mysql-username'@'%' identified by 'my$123456';
serial number | parameter | describe |
---|---|---|
1 | all | All operations, individually configured operations: select, insert, update and delete |
2 | . | Represents all databases, a database and some tables can be specified separately, such as database-name.*, database-name.table-name |
3 | mysql-username | username |
4 | my$123456 | user password |
# 生效
flush privileges;
view users
select host, user from mysql.user;
3 FAQ
3.1 Why specify a password
Error message if no password is specified:
3.2 Configuration file
to be described.
[client]
port = 3306
socket = /usr/local/services/mysql/var/data/mysql.sock
[mysqld]
bind-address = 0.0.0.0
port = 3306
socket = /usr/local/services/mysql/var/data/mysql.sock
pid-file = /usr/local/services/mysql/var/logs/mysql.pid
character-set-server = utf8
basedir = /usr/local/services/mysql
datadir = /usr/local/services/mysql/var/data
skip-external-locking
skip-name-resolve
lower_case_table_names = 1
log-bin-trust-function-creators = 1
max_connections = 6000
max_user_connections = 6000
max_connect_errors = 4000
wait_timeout = 86400
interactive_timeout = 86400
table_open_cache = 512
max_allowed_packet = 32M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 8
thread_concurrency = 8
query_cache_size = 32M
#default-storage-engine = InnoDB
#sql_mode="STRICT_ALL_TABLES,NO_AUTO_CREATE_USER"
server-id = 1
log-short-format
log-error = /usr/local/services/mysql/var/logs/mysql.log
slow_query_log
long_query_time = 2
slow_query_log_file = /usr/local/services/mysql/var/logs/mysql-slow.log
log-bin = /usr/local/services/mysql/var/binlog/mysql-bin
log_bin_trust_function_creators=1
binlog_format = MIXED
expire_logs_days = 10
# INNODB Specific options
innodb_data_home_dir = /usr/local/services/mysql/var/data
innodb_log_group_home_dir = /usr/local/services/mysql/var/redolog
innodb_additional_mem_pool_size = 10M
innodb_buffer_pool_size = 4G
innodb_data_file_path = ibdata1:100M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 0
innodb_flush_method = O_DIRECT
innodb_log_buffer_size = 128M
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 50
innodb_file_per_table = 1
# MyISAM Specific options
key_buffer_size = 384M
read_buffer_size = 4M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 1G
myisam_repair_threads = 1
myisam_recover
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
default-character-set = utf8
no-auto-rehash
socket = /usr/local/services/mysql/var/data/mysql.sock
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout