docker安装mysql

643 阅读1分钟

docker mysql

mysql 安装

docker 安装mysql,本地访问

docker run -itd --name docker_mysql_01 -p 13306:3306 -e MYSQL_ROOT_PASSWORD=root mysql

本地端口 13306 映射容器中的3306,通过 127.0.0.1:13306 访问;

给容器起一个名字,docker_mysql_01, 用于本地管理docker;

进入容器内部:

docker exec -it docker_mysql_01 bash  

容器内部访问mysql

mysql -uroot -proot
​
mysql> show databases;

查看日志

docker logs docker_mysql_01

mysql配置修改

容器内部修改mysql 配置

官方文档:

hub.docker.com/_/mysql

Using a custom MySQL configuration file

The default configuration for MySQL can be found in /etc/mysql/my.cnf, which may !includedir additional directories such as /etc/mysql/conf.d or /etc/mysql/mysql.conf.d. Please inspect the relevant files and directories within the mysql image itself for more details.

If /my/custom/config-file.cnf is the path and name of your custom configuration file, you can start your mysql container like this (note that only the directory path of the custom config file is used in this command):

$ docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

This will start a new container some-mysql where the MySQL instance uses the combined startup settings from /etc/mysql/my.cnf and /etc/mysql/conf.d/config-file.cnf, with settings from the latter taking precedence.

Configuration without a cnf file

Many configuration options can be passed as flags to mysqld. This will give you the flexibility to customize the container without needing a cnf file. For example, if you want to change the default encoding and collation for all tables to use UTF-8 (utf8mb4) just run the following:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

If you would like to see a complete list of available options, just run:

$ docker run -it --rm mysql:tag --verbose --help

挂载本地文件

docker run --name docker_mysql_01 -v /opt/docker_my/mysql01:/etc/mysql/conf.d -p 13306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql

报错

docker: Error response from daemon: Mounts denied: 
The path /opt/docker_my/mysql01 is not shared from the host and is not known to Docker.
You can configure shared paths from Docker -> Preferences... -> Resources -> File Sharing.
See https://docs.docker.com/desktop/mac for more info.

处理:

image.png

重启docker后,再次执行命令;

查看容器内部mysql配置文件

 mysql --help | grep my.cnf
bash-4.4#  mysql --help | grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf 

docker 内部 mysql 配置

cat /etc/my.cnf

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
​
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock
​
!includedir /etc/mysql/conf.d/

查看变量

docker run -it --rm mysql --verbose --help

验证变量配置

/opt/docker_my/mysql01/my.cnf

[mysqld]
max_connections=222
mysqlx-max-connections=500

重启docker

show variables like '%max_connections%';

image.png