###目前网络模式为:bridge
1.docker mysql:5.7的持久化存储及远程连接
1.下拉镜像
docker pull mysql/mysql-server:5.7
注:后面的mysql标签是版本号,可选择,有:5.5/5.6/5.7/8.02.在宿主机中相关目录,用于挂载容器的相关数据
mkdir -p /data/mysql/{conf,data}注1:conf目录
###重要的事情说三遍
###重要的事情说三遍
###重要的事情说三遍
必须提前上传my.cnf文件到/data/conf目录,并且它与window中的配置文件my.ini后缀名是不一样
必须提前上传my.cnf文件到/data/conf目录,并且它与window中的配置文件my.ini后缀名是不一样
必须提前上传my.cnf文件到/data/conf目录,并且它与window中的配置文件my.ini后缀名是不一样
详情见:资料/my.cnf注2:data目录
数据保存到宿主机中,下次需要的时候重启容器,原有数据都能被加载,不会丢失数据
my.cnf
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
##官方的配置
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
##下面为添加的自定义配置
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
# default: sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# modeified:
sql_mode= STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
max_allowed_packet=10M
default-time_zone='+8:00'
default_authentication_plugin=mysql_native_password
修改mysql允许Navicat远程连接
docker exec -it mysql /bin/bash #进入mysql5.7容器
mysql -u root -p; #登录mysql服务器,之后输入mysql码:123456 grant all privileges on *.* to root@'%' identified by '123456' with grant option; #给用户授权
flush privileges; #更新权限后,外部就可以使用native进行连接了
注1:数据库字符集查看
show variables like'character%';
两次退出才是真正的退出
docker run -d \
-e PREFER_HOST_MODE=ip \
-e MODE=standalone \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_MASTER_SERVICE_HOST=172.17.0.2 \
-e MYSQL_MASTER_SERVICE_PORT=3306 \
-e MYSQL_MASTER_SERVICE_USER=root \
-e MYSQL_MASTER_SERVICE_PASSWORD=123456 \
-e MYSQL_MASTER_SERVICE_DB_NAME=nacos_config \
-e MYSQL_SLAVE_SERVICE_HOST=172.17.0.2 \
-p 8848:8848 \
--name nacos \
--restart=always \
nacos/nacos-server:1.1.4
参数说明:
docker run -d \ #后台支行模式
-e PREFER_HOST_MODE=hostname \ #是否支持hostname,可选项: hostname/ip,默认值ip
-e MODE=standalone \ #cluster模式/standalone模式,可 选项:cluster/standalone,默认值cluster
-e SPRING_DATASOURCE_PLATFORM=mysql \ #standalone支持 mysql,可选项:mysql/empty,默认值empty
-e MYSQL_MASTER_SERVICE_HOST=172.17.0.2 \ #mysql主节点数据库IP,这有个坑:因为网络模式为bridge重启系统后IP会变导致nacos的再次启动失败
-e MYSQL_MASTER_SERVICE_PORT=3306 \ #mysql主节点端口
-e MYSQL_MASTER_SERVICE_USER=root \ #数据库用户名
-e MYSQL_MASTER_SERVICE_PASSWORD=123456 \ #数据库密码
-e MYSQL_MASTER_SERVICE_DB_NAME=nacos_config \ #mysql主节点数据库名
-e MYSQL_SLAVE_SERVICE_HOST=172.17.0.2 \ #mysql从节点数据库IP
-p 8848:8848 \ #主机端口映射
--name nacos \ #容器名
--restart=always \ #容器自动启动
nacos/nacos-server:1.1.4 #镜像名注1:由于没有配置mysql的主、从集群、本以为MYSQL_SLAVE_SERVICE_HOST可以不用配置,但通过查看nacos容器内的config/application.properties文件,
会发现这此值未设置默认值,所以会导致启动报错,容器启动不起来,这里可以与主数据库一致。还有一个解决方案就是
##删除从库的配置,将配置数据库数量改为1也行,后面例子就是这样做的
//-e MYSQL_SLAVE_SERVICE_HOST=172.17.0.2 \
-e MYSQL_DATABASE_NUM=1 \注2:相关命令
#停止并删除所有容器
docker stop nacos && docker rm nacos
#docker logs-查看docker容器日志
docker logs -f -t --tail=100 nacos
docker logs -f -t --tail=100 nacos
curl -X GET "http://192.168.190.130:8848/nacos/v1/cs/configs?dataId=test.yml&group=DEFAULT_GROUP"
docker rm -f $(docker ps -aq)
docker run \
--name mysql \
--net mynet \
--ip 192.168.0.11 \
-v /data/mysql/conf/my.cnf:/etc/my.cnf \
-v /data/mysql/data:/var/lib/mysql \
--privileged=true \
--restart=always \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql/mysql-server:5.7
注1:容器启动成功后,修改mysql允许root远程访问(同上)
挂载文件
创建集群节点1
docker run -d \
--net mynet \
--ip 192.168.0.21 \
-e PREFER_HOST_MODE=ip \
-e MODE=cluster \
-e NACOS_SERVERS="192.168.0.22:8848 192.168.0.23:8848" \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_MASTER_SERVICE_HOST=192.168.0.11 \
-e MYSQL_MASTER_SERVICE_PORT=3306 \
-e MYSQL_MASTER_SERVICE_USER=root \
-e MYSQL_MASTER_SERVICE_PASSWORD=123456 \
-e MYSQL_MASTER_SERVICE_DB_NAME=nacos_config \
-e MYSQL_DATABASE_NUM=1 \
-e NACOS_SERVER_PORT=8848 \
--name nacos01 \
--restart=always \
nacos/nacos-server:1.1.4
创建集群节点2
docker run -d \
--net mynet \
--ip 192.168.0.22 \
-e PREFER_HOST_MODE=ip \
-e MODE=cluster \
-e NACOS_SERVERS="192.168.0.21:8848 192.168.0.23:8848" \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_MASTER_SERVICE_HOST=192.168.0.11 \
-e MYSQL_MASTER_SERVICE_PORT=3306 \
-e MYSQL_MASTER_SERVICE_USER=root \
-e MYSQL_MASTER_SERVICE_PASSWORD=123456 \
-e MYSQL_MASTER_SERVICE_DB_NAME=nacos_config \
-e MYSQL_DATABASE_NUM=1 \
-e NACOS_SERVER_PORT=8848 \
--name nacos02 \
--restart=always \
nacos/nacos-server:1.1.4
创建集群节点3
docker run -d \
--net mynet \
--ip 192.168.0.23 \
-e PREFER_HOST_MODE=ip \
-e MODE=cluster \
-e NACOS_SERVERS="192.168.0.21:8848 192.168.0.22:8848" \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_MASTER_SERVICE_HOST=192.168.0.11 \
-e MYSQL_MASTER_SERVICE_PORT=3306 \
-e MYSQL_MASTER_SERVICE_USER=root \
-e MYSQL_MASTER_SERVICE_PASSWORD=123456 \
-e MYSQL_MASTER_SERVICE_DB_NAME=nacos_config \
-e MYSQL_DATABASE_NUM=1 \
-e NACOS_SERVER_PORT=8848 \
--name nacos03 \
--restart=always \
nacos/nacos-server:1.1.4
docker logs -f -t --tail=100 nacos01
curl http://192.168.0.21:8848/nacos/index.html
curl http://192.168.0.22:8848/nacos/index.html
curl http://192.168.0.23:8848/nacos/index.html
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
#服务器的集群
#upstream tomcats { #服务器集群名字
#TODO:172.17.0.3是docker容器的IP
#server 172.17.0.3:8080 weight=1;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。
#server 172.17.0.4:8080 weight=2;
#}
#新增1:nacos集群
upstream nacoses {
server 192.168.0.21:8848 weight=1;
server 192.168.0.22:8848 weight=1;
server 192.168.0.23:8848 weight=1;
}
#当前的Nginx的配置
server {
listen 80;#监听80端口,可以改成其他端口
server_name localhost;#当前服务的域名,没有域名可随便填写
#新增2:用于重定向至nacos集群
location /nacos {
proxy_pass http://nacoses;
}
location / {
root /usr/share/nginx/html/dist;#将要访问的网站的目录
try_files $uri $uri/ /index.html;#该句代码是为解决history路由不能跳转的问题,在vue-router官网有介绍
}
#location ^~/api/ {
#^~/api/表示匹配前缀是api的请求,proxy_pass的结尾有/, 则会把/api/*后面的路径直接拼接到后面,即移除api
#proxy_pass http://tomcats/;
#}
}
}