创建目录

1
2
3
mkdir -p /www/mysql/conf
mkdir -p /www/mysql/data
mkdir -p /www/mysql/logs

/www/mysql/conf目录下新建my.cnf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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 as published by
# the Free Software Foundation; version 2 of the License.
#
# 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 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
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0


max_connections = 2000
max_user_connections = 1900
max_connect_errors = 100000
max_allowed_packet = 50M


[mysqld]
skip-name-resolve
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
lower_case_table_names=1

拉取镜像,并启动容器

1
docker run -itd --restart=always --name=mysql -p 3295:3306 -v /www/mysql/conf:/etc/mysql -v /www/mysql/logs:/var/log/mysql -v /www/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.21

进入容器

1
docker exec -it mysql /bin/bash 

登录MySQL

输入以下命令

1
mysql -uroot -p

输入创建并启动容器命令设置的123456

登陆成功后修改密码

1
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

123456就是root用户的登陆密码

允许远程连接

  • 新建用户允许远程连接
1
2
grant all on *.* to admin@'%' identified by '123456' with grant option;
flush privileges;
  • 现有用户下允许远程连接
1
2
3
use mysql;
update user set host='%' where user='root' and host='localhost';
flush privileges;
  • 查看用户
1
2
use mysql;
select host,user from user;

常用命令

mysql内

查看全局变量

1
show global variables;

查看端口

1
show global variables like 'port';

mysql外

查看配置文件路径

1
/usr/sbin/mysqld --verbose --help | grep -A 1 'Default options'