1.Docker的安装
(1)yum 包更新到最新
sudo yum update
(2)安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
(3)设置yum源为阿里云
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
(4)安装docker
sudo yum install docker-ce
(5)安装后查看docker版本
docker -v
(6)设置镜像源为ustc(拉取镜像更快一下)
详细操作步骤:
编辑该文件:
vi /etc/docker/daemon.json
在该文件中输入如下内容:
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}
保存退出,重启虚拟机(若docker没启动不重启虚拟机也行)
2.Docker安装mysql容器
启动docker:
systemctl start docker
拉取mysql镜像:
docker pull mysql:8.0.26
创建my.cnf配置文件与data目录(目的:挂载)
cd /db/mysql8.0.26/
mkdir conf
mkdir data
mkdir log
cd /db/mysql8.0.26/conf/
vi my.cnf
在my.cnf输入:
# 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]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Custom config should go here
!includedir /etc/mysql/conf.d/
max_connections=1000
wait_timeout=120
interactive_timeout=300
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
创建并启动容器:
docker run -di --name=mysql8 -p 33306:3306 -v /db/mysql8.0.26/conf/my.cnf:/etc/mysql/my.cnf -v /db/mysql8.0.26/data:/var/lib/mysql -v /db/mysql8.0.26/log:/var/log/mysql -e MYSQL_ROOT_PASSWORD=root mysql:8.0.26
-p:宿主机:容器端口映射;33306:宿主机端口,3306:mysql端口;navicat连接时使用33306端口连接,
root:root用户密码
-v:挂载文件 宿主机目录:容器目录
这里碰到一个问题,使用group by语句时报错:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘intel_test.trending_hist.id’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
解决方案:https://www.jb51.net/article/251380.htm
3.设置MYSQL8远程访问权限
进入isee-mysql容器
docker exec -it isee-mysql /bin/bash
登录mysql:
mysql -uroot -proot
切换到mysql数据库:
use mysql;
在 mysql 数据库的 user 表中查看当前 root 用户的相关信息
select host, user, authentication_string, plugin from user;
查看表格中 root 用户的 host,默认应该显示的 localhost,只支持本地访问,不允许远程访问。
ps:创建用户
CREATE USER ‘user1’@‘%’ IDENTIFIED BY ‘password’;
mysql8设置远程连接:授权 root 用户的所有权限并设置远程访问
grant all privileges on *.* to 'root'@'%';
刷新权限
FLUSH PRIVILEGES;
再次执行
select host, user, authentication_string, plugin from user;
你会发现 root 用户的 host 已经变成 %,说明我们的修改已经成功,可以远程访问了。
navicat输入访问的 host 和密码,报 2059 错误,
这是因为 MySql8.0 版本 和 5.0 的加密规则不一样,而现在的可视化工具只支持旧的加密方式。
修改加密规则 :password 为你当前密码。
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
设置新密码:password 为你新设置的密码。
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
刷新权限
FLUSH PRIVILEGES;
最后使用navicat连接…
要是还不行,试一下开放端口,我们之前设置的宿主机连接mysql端口是33306,所以,开放33306端口:
firewall-cmd --add-port=33306/tcp --permanent;
要是还不行,直接把防火墙关了:
systemctl stop firewalld;
至此,我再用navicat连接是已经可以成功了的
PS:其实事后小编又重新新建了个mysql8容器,防火墙也打开了,之前的33306端口也给关闭了,mysql8远程访问权限也没修改,mysql8的密码加密方式也没改,但是照样连接成功!所以建议启动mysql8容器之后,关闭容器,重新启动虚拟机试一下,或许会有意想不到的结果。