Mysql 是全球最流行的开源关系型数据库之一,由于其具有高性能、成熟可靠、高适应性、易用性而得到广泛应用。
方法一
使用官方镜像
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=yourpassword -d mysql:5.7
添加远程连接权限
docker exec -it mysql mysql -uroot -pyourpassword
use mysql
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;
flush privileges;
方法二
使用 docker-compose
在指定目录下添加 docker-compose.yml 和 my.cnf 文件,执行 docker-compose up -d
docker-compose.yml 文件内容
version: '3.7'
services:
db:
image: mysql:5.7
container_name: mysql
ports:
- "3306:3306"
volumes:
- ./data:/var/lib/mysql
- ./my.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
restart: always
environment:
MYSQL_ROOT_PASSWORD: yourpassword
TZ: Asia/Shanghai
my.cnf 文件内容
# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
group_concat_max_len = 102400
max_allowed_packet = 1500M
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4