🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### docker 官方仓库搜索mysql相应的版本 ##### 版本5.7 ``` https://hub.docker.com/_/mysql?tab=tags&page=1&name=5.7 ``` ##### 版本8.0 ``` https://hub.docker.com/_/mysql?tab=tags&page=1&name=8.0 ``` ### 拉取镜像 ##### 版本5.7 ``` docker pull mysql:5.7.38 ``` ##### 版本8.0 ``` docker pull mysql:8.0.31 ``` ### 创建容器,运行镜像 ##### 版本5.7 ``` docker run -d --name=mysql -p 3306:3306 -v /data/mysql/data:/var/lib/mysql -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.38 ``` ##### 版本8.0 配置文件 my.cnf ``` # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html [mysqld] server_id=1 log-bin=mysql-bin binlog-format=ROW binlog-do-db=test # # 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 sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION [client] socket=/var/run/mysqld/mysqld.sock !includedir /etc/mysql/conf.d/ ``` ``` docker run --name mysql --restart=always \ -v /data/mysql/conf.d:/etc/mysql/conf.d \ -v /data/mysql/data:/var/lib/mysql \ -v /data/mysql/logs:/var/log/mysql \ -p 3306:3306 \ -e MYSQL_ROOT_PASSWORD="123456" \ -e TZ=Asia/Shanghai \ -d mysql:8.0.30 \ --lower-case-table-names=1 ``` docker run --name mysql --restart=always \ -v /data/mysql/conf/my.cnf:/etc/my.cnf \ -v /data/mysql/data:/var/lib/mysql \ -v /data/mysql/logs:/var/log/mysql \ -p 3306:3306 \ -e MYSQL_ROOT_PASSWORD="123456" \ -e TZ=Asia/Shanghai \ -d mysql:8.0.31 \ --lower-case-table-names=1 ### 修改容器里的配置文件(可跳过) 由于docker容器中没有安装vim编辑器,需将配置文件拷贝出来,在宿主机进行编辑,命令如下 ``` docker cp mysql:/etc/my.cnf   ./ ``` 添加完成后再将文件拷贝回docker容器中 ``` docker cp ./my.cnf mysql:/etc ``` 重启mysql容器 ``` docker restart mysql ``` ##### 2022-06-04编辑