Appearance
MYSQL 入门
Docker 安装MySQL 8
拉取镜像
bash
docker pull mysql:8.0
创建挂载文件
bash
mkdir -p /data/mysql8/mysql/conf.d
mkdir -p /data/mysql8/logs
mkdir -p /data/mysql8/data
touch /data/mysql8/mysql/my.cnf
touch /data/mysql8/mysql/conf.d/mysql.cnf
touch /data/mysql8/mysql/conf.d/docker.cnf
# 授权
chmod -R 777 /data/mysql8
mysql/my.cnf
bash
# Copyright (c) 2017, 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]
character-set-server=utf8
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Custom config should go here
!includedir /etc/mysql/conf.d/
mysql/conf.d/mysql.cnf
bash
# Copyright (c) 2015, 2021, Oracle and/or its affiliates.
#
# 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 Client configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysql]
default-character-set=utf8
mysql/conf.d/docker.cnf
bash
[mysqld]
skip-host-cache
skip-name-resolve
创建容器并运行
bash
docker run -d --restart=on-failure:3 \
--net bridge --privileged=true \
--name mysql8 -p 34567:3306 \
-e MYSQL_ROOT_PASSWORD=123456 \
-v /data/mysql8/mysql:/etc/mysql \
-v /data/mysql8/logs:/logs \
-v /data/mysql8/data:/var/lib/mysql \
-v /etc/localtime:/etc/localtime mysql:8.0
- --restart=on-failure:3 失败后重启,重试3次
- --net bridge 连接到网络bridge
- --privileged=true 授权给root
- --name mysql8 容器名字命名为mysql8
- -p 34567:3306 把端口映射到宿主机器的34567
- -e MYSQL_ROOT_PASSWORD=123456 初始密码设置为123456
- -v 挂载相关卷
- mysql:8.0 使用镜像mysql:8.0创建容器
查看日志
bash
docker logs -f mysql8
修改密码
bash
# 进入容器
docker exec -it mysql8 /bin/bash
# 进入MySQL
mysql -u root -p
bash
# 如果提示首次登录修改密码,就使用
ALTER USER USER() IDENTIFIED BY 'Admin2022!';
# 如果未提示,则使用下面方式
use mysql;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
ALTER USER 'root'@'%' IDENTIFIED BY 'new_password';
# 刷新权限
FLUSH PRIVILEGES;
exit;
exit
本地连接
ip:34567
root
密码为修改后密码