开放端口规划:

  • mysql-develop:3407
  • mysql-test: 3408
  • mysql-release: 3409

ps:

1.不推荐使用默认端口-3306,建议自定义端口

2.如果采用阿里云服务器,在安全组开放端口

3.自建服务器依据实际情况打开防火墙开放端口[各个系统防火墙不一样,操作有所不同],譬如:

Centos7 环境-防火墙[firewall-cmd]:

firewall-cmd --zone=public --add-port=3407/tcp --permanent
firewall-cmd --zone=public --add-port=3408/tcp --permanent
firewall-cmd --zone=public --add-port=3409/tcp --permanent

4.防火墙[firewall-cmd]常用操作

(1)设置开机启用防火墙:systemctl enable firewalld.service
(2)设置开机禁用防火墙:systemctl disable firewalld.service
(3)启动防火墙:systemctl start firewalld
(4)关闭防火墙:systemctl stop firewalld
(5)检查防火墙状态:systemctl status firewalld
二、使用firewall-cmd配置端口
(1)查看防火墙状态:firewall-cmd --state
(2)重新加载配置:firewall-cmd --reload
(3)查看开放的端口:firewall-cmd --list-ports
(4)开启防火墙端口:firewall-cmd --zone=public --add-port=9200/tcp --permanent
  命令含义:
  –zone #作用域
  –add-port=9200/tcp #添加端口,格式为:端口/通讯协议
  –permanent #永久生效,没有此参数重启后失效
  注意:添加端口后,必须用命令firewall-cmd --reload重新加载一遍才会生效
    firewall-cmd --zone=public --add-port=9200/tcp --permanent
(5)关闭防火墙端口:firewall-cmd --zone=public --remove-port=9200/tcp --permanent

查找镜像:docker search mysql

docker search mysql

拉取镜像:docker pull mysql

docker pull mysql

ps:如果不是自建仓库镜像,一般从https://hub.docker.com/拉取官方镜像:

docker pull mysql:5.7 # 拉取mysql 5.7

docker pull mysql # 拉取最新版mysql镜像

部署mysql服务:

1.简单命令实例:[主要使用Docker原生命令部署]

docker run -itd -p 3306:3306 --restart always --name mysql-server   -e MYSQL_ROOT_PASSWORD=db-password -e MYSQL_USER=db-username  mysql:tag

2.使用docker-compose 部署实例:使用docker-compose搭建

docker-compose.yml文件进行部署可从,github和码云等云仓库git clone 然后修改执行[docker-compose up -d]部署:

docker-compose.yml 配置实例:

version: '2'
services:
db:
image: 'mysql/mysql-server:tag'
restart: always
container_name: mysql-server
environment:
MYSQL_USER: username
MYSQL_PASSWORD: password
MYSQL_DATABASE: database
MYSQL_ROOT_PASSWORD: password
ports:
- 'server-port[自定义端口]: container-port[默认3306]'

3.使用Docker Portainer可视化界面自建进行部署

Mysql8.0 数据库配置

基于Docker安装的数据库安装完成之后,只能在本地登录,需要进行授权远程访问连接操作。

  • 1.创建用户和授权
# 创建自定义myql用户-username 和密码-pssword
create user 'username'@'%' identified by 'pssword';
>ps:create user 'developer'@'%' identified by '123456Abc@2019'; # 对自定义用户进行授权操作
grant all privileges on *.* to 'username'@'%' with grant option;
>ps:grant all privileges on *.* to 'developer'@'%' with grant option; # 刷新操作权限[切记此点]
flush privileges;

进入[root@mysql-develop]容器:

root@mysql-develop:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.18 MySQL Community Server - GPL Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> create user 'developer'@'%' identified by '123456Abc@2019';
Query OK, 0 rows affected (0.01 sec) mysql> grant all privileges on *.* to 'developer'@'%' with grant option;
Query OK, 0 rows affected (0.00 sec) mysql>
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

如图:

ps:

1.mysql8.0数据操作授权之前得先自定义创建用户,否则无法授权远程登录访问

2.mysql8.0授权无法使用mysql5.7方式:

grant all privileges on . to 'developer'@'%' identified by '123456Abc@2019';

请使用:grant all privileges on . to 'developer'@'%' with grant option;

第一种:grant all privileges on . to 'developer'@'%' identified by '123456Abc@2019' with grant option;

mysql> use mysql
Database changed
mysql> grant all privileges on *.* to 'developer'@'%' identified by '123456Abc@2019' with grant option;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by '123456Abc@2019' with grant option' at line 1

第二种:grant all privileges on . to 'developer'@'%' identified by 123456Abc@2019';

mysql> use mysql;
Database changed
mysql> grant all privileges on *.* to 'developer'@'%' identified by '123456Abc@2019';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by '123456Abc@2019 at line 1
mysql>

3.一定而且必须进行刷新权限操作,否则无法生效,甚至无法授权远程访问

2.mysql8.0远程访问链接[root 和developer]

在 mysql 数据库的 user 表中查看当前用户的相关信息:

mysql> use mysql
Database changed
mysql> select host, user, authentication_string, plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host | user | authentication_string | plugin |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| % | developer | *F286F2787D69B007CFDE83C115325B2A6FF0B6D2 | caching_sha2_password |
| % | root | *F286F2787D69B007CFDE83C115325B2A6FF0B6D2 | caching_sha2_password |
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
_Oo8xLxsqwEOxEkY1i7kToF8VbktysFDQuevvwYqsK61Qi7 | caching_sha2_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
6 rows in set (0.00 sec)
mysql>

root 用户:

mysql> use mysql;
Database changed
mysql> GRANT ALL ON *.* TO 'root'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456Abc@2019;
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

developer用户:

mysql> use mysql;
Database changed
mysql> GRANT ALL ON *.* TO 'developer'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER 'developer'@'%' IDENTIFIED WITH mysql_native_password BY '123456Abc@2019';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>

修改加密规则:

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> ALTER USER 'root'@'%' IDENTIFIED BY '123456Abc@2019' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.01 sec) mysql> ALTER USER 'developer'@'%' IDENTIFIED BY '123456Abc@2019' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.01 sec) mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec) mysql>

设置完成需要再次验证用户权限信息:

mysql> use mysql
Database changed
mysql> select host, user, authentication_string, plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host | user | authentication_string | plugin |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| % | developer | *F286F2787D69B007CFDE83C115325B2A6FF0B6D2 | mysql_native_password |
| % | root | *F286F2787D69B007CFDE83C115325B2A6FF0B6D2 | mysql_native_password |
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
_Oo8xLxsqwEOxEkY1i7kToF8VbktysFDQuevvwYqsK61Qi7 | caching_sha2_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
6 rows in set (0.00 sec) mysql>

到此,Navicat测试连接msql:

ps[注意事项]:

1.mysql8.0版本加密规则插件的plugin 已经换为caching_sha2_password,而之前的版本的加密规则是mysql_native_password,经过实测已经不适用于Navicat 12以下版本,可依据自身情况升级客户端到Navicat 12+,否则会报2059 或者1251 错误。

[Question-01].Navicat 2059错误:

[Question-02].Navicat 1251错误:

2.鉴于第一条的情况,可以将caching_sha2_password修改为mysql_native_password做一个兼容,低版本也可适用。

3.修改加密规则,使得密码长期有效。

完整sql记录:

mysql> use mysql
mysql> create user 'developer'@'%' identified by '123456Abc@2019';
Query OK, 0 rows affected (0.01 sec) mysql> grant all privileges on *.* to 'developer'@'%' with grant option;
Query OK, 0 rows affected (0.01 sec) mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec) mysql> GRANT ALL ON *.* TO 'root'@'%';
Query OK, 0 rows affected (0.00 sec) mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456Abc@2019';
Query OK, 0 rows affected (0.01 sec) mysql> GRANT ALL ON *.* TO 'developer'@'%';
Query OK, 0 rows affected (0.00 sec) mysql> ALTER USER 'developer'@'%' IDENTIFIED WITH mysql_native_password BY '123456Abc@2019';
Query OK, 0 rows affected (0.01 sec) mysql> ALTER USER 'root'@'%' IDENTIFIED BY 'GuangDian@2019' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.01 sec) mysql> ALTER USER 'developer'@'%' IDENTIFIED BY 'GuangDian@2019' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.01 sec) mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec) mysql>

3套mysql环境:

mysql-develop:

IP:192.168.0.1

Port:3407

Username:root/developer

password:123456Abc@2019

mysql-test:

IP:192.168.0.2

Port:3408

Username:root/developer

password:123456Abc@2019

mysql-release:

IP:192.168.0.3

Port:3409

Username:root/developer

password:123456Abc@2019

数据文件迁移操作

1.基于mysqldump+docker cp 命令进行操作

  • 方式1:直接在宿主机器进行数据备份
docker exec -it docker-id[容器实际部署id] mysqldump -u root -p passowrd --databases dbA dbB > /root/all-databases-backup.sql
  • 方式2:先进入到docker在执行mysqldump,然后再将导出的sql拷贝到宿主
#进入docker
docker exec -it docker-id[容器实际部署id] /bin/bash
#可选的
source /etc/profile
#执行导出命令
mysqldump -u username -p password --databases dbA dbB > /root/all-databases-backup.sql
#拷贝到宿主机器
#退出Docker,执行exit命令
exit
#此时,已经在宿主的环境,执行拷贝命令,将sql文件从docker红拷贝出来
docker cp docker-id[容器实际部署id]: /root/all-databases-backup.sql /root/all-databases-backup.sql

2.导入数据文件到容器

#拷贝备份的文件到docker中
docker cp /root/all-databases-backup.sql docker-id[容器实际部署id]:/root/all-databases-backup.sql
#先进入docker环境,然后导入到数据库
docker exec -it xxx /bin/bash
mysql -u username -p password < /root/all-databases-backup.sql

版权声明:本文为博主原创文章,遵循相关版权协议,如若转载或者分享请附上原文出处链接和链接来源。

Linux系统环境基于Docker搭建Mysql数据库服务实战的更多相关文章

  1. 基于Docker搭建MySQL主从复制

    摘要: 本篇博文相对简单,因为是初次使用Docker,MySQL的主从复制之前也在Centos环境下搭建过,但是也忘的也差不多了,因此本次尝试在Docker中搭建. 本篇博文相对简单,因为是初次使用D ...

  2. 基于 Docker 搭建 MySQL 主从复制

    本篇博文相对简单,因为是初次使用Docker,MySQL的主从复制之前也在Centos环境下搭建过,但是也忘的也差不多了,因此本次尝试在Docker中搭建. 根据网上教程走还是踩了一些坑,不过所幸最终 ...

  3. 基于Docker搭建MySQL多源复制环境

    MySQL5.7在主从复制上面相对之前版本多了一些新特性,包括多源复制.基于组提交的并行复制.在线修改Replication Filter.GTID增强.半同步复制增强等. 多源复制:多源复制加入了一 ...

  4. Linux Centos7 基于Docker 搭建 Nexus私服搭建

    创建Blob Stores[本地文件存储目录,统一管理] 1.设置名称和工作路径: ps[注意事项]: 1.storage name:自定义名称 2.storage path:存储路径,默认[/nex ...

  5. 基于Docker的MySql

    MySQL Server安装教程 考虑到实际情况需要经常使用MySQL,为了方便大家能够快速基于Docker搭建MySQL这里以Linux下为例 进行说明,对于Windows用户来说直接通过查看官网H ...

  6. 基于Docker的Mysql主从复制搭建

    来源:https://www.cnblogs.com/songwenjie/p/9371422.html?tdsourcetag=s_pctim_aiomsg   为什么基于Docker搭建? 资源有 ...

  7. 基于Inception搭建MySQL SQL审核平台Yearing

    基于Inception搭建MySQL SQL审核平台Yearing Inception 1. Inceptionj简介 2. Inception安装 2.1 下载和编译 2.2 启动配置 Yearni ...

  8. [转帖]基于docker 搭建Prometheus+Grafana

    基于docker 搭建Prometheus+Grafana https://www.cnblogs.com/xiao987334176/p/9930517.html need good study 一 ...

  9. docker初识-docker安装、基于docker安装mysql及tomcat、基本命令

    一.docker是什么 用go语言开发,开源的应用容器引擎,容器性能开销极低 二.整体架构图 Docker 包括三个基本概念: 镜像(Image):Docker 镜像(Image),就相当于是一个 r ...

随机推荐

  1. 解决AndroidStudio 模拟器无网络连接

    更新 注意 Win10 要在cmd下打开, 也就是地址栏打cmd能成功 转载地址 https://blog.csdn.net/Bibifeng/article/details/81317037 最近写 ...

  2. 数据库课程设计:SQL Server + Express + node.js + ejs 论坛管理系统

    前言 这是一篇对数据库课程设计的总结,这不是教程也不是指导,只是我的经验之谈,其中可能有许多错误,请小心,不要被误导.祝愿你看了这篇文章后能做出更好的设计. 我对web开发并不熟悉,而我们的课程设计只 ...

  3. excel如何写宏?如何用按钮?

    注:本次测试版本 excel版本2019 写宏? 准备工作(使用宏的一切前提)===========文件-选项-自定义功能区 (勾选开发工具) 开始写宏=======右击sheet1--查看代码    ...

  4. Spring Boot系列(一):Spring Boot快速开始

    一.Spring Boot介绍 Spring Boot可以很容易的创建可直接运行的独立的基于Spring的应用程序. 功能特点: 创建独立的Spring应用程序: 直接嵌入Tomcat.Jetty等W ...

  5. JDK14-ZGC调研初探

    原创声明:作者:Arnold.zhao 博客园地址:https://www.cnblogs.com/zh94 背景 公司ElasticSearch准备进行升级,而ElasticSearch7以上则是已 ...

  6. 下拉菜单,下拉导航,JavaScript,html,jQuery的实现代码

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...

  7. selenium定位方法(二)

    selenium定位方法(二)  1.xpath定位:xpath是在XML中查找节点所在的路径的表达式 1)绝对路径的Xpath表达式 例:/html/body/div/div[1]/ul//li[3 ...

  8. Java GUI 图书管理系统

    01 概述 一款功能强大的图书馆管理系统,功能齐全,小白/大学生项目实训,学习的不二之选. 02 技术 此系统使用 java awt 实现.java.awt是一个软件包,包含用于创建用户界面和绘制图形 ...

  9. 2020,6招玩转 Appium 自动化测试

    Appium是个什么鬼 Appium是一个移动端的自动化框架,可用于测试原生应用,移动网页应用和混合型应用,且是跨平台的.可用于IOS和Android以及firefox的操作系统.原生的应用是指用an ...

  10. 力扣Leetcode 461. 汉明距离

    给你一个数组 arr ,请你将每个元素用它右边最大的元素替换,如果是最后一个元素,用 -1 替换. 完成所有替换操作后,请你返回这个数组. 示例: 输入:arr = [17,18,5,4,6,1] 输 ...