windows下mysql的高可用方案

news2024/9/26 1:20:00

PS:理论上linux下也可以使用这种方案

图片

环境准备:

首先准备两台电脑,全部安装MySQL,然后分别查看一下ip地址,我的两个ip分别是:

192.168.25.134(简称134)

192.168.25.135(简称135)

配置:my.ini

一、配置两个MySQL的my.ini如下:

必須卸载mysqld后面

134 my.ini:

这里要看下原本里面有没有server-id,如果有的话去掉用下面的配置覆盖

# 开启二进制同步
log-bin=mysql-bin

# 设置ID,不能重复,可以使用IP最后一位数字 
server-id=1

# 需要同步的数据名称,多个数据库则要重复设置: bin-do-db,bin-ignore-db为互斥关系, 只需设置其中一项即可
replicate-do-db=twinthings

# 自增长字段初始值为1
auto-increment-offset=2

# 自增长字段增量值
auto-increment-increment=2

# 跳过所有复制的错误
slave-skip-errors=all

135 my.ini:

# 开启二进制同步
log-bin=mysql-bin

# 设置ID,不能重复,可以使用IP最后一位数字 
server-id=2

# 需要同步的数据名称,多个数据库则要重复设置: bin-do-db,bin-ignore-db为互斥关系, 只需设置其中一项即可
replicate-do-db=twinthings

# 自增长字段初始值为1
auto-increment-offset=2

# 自增长字段增量值
auto-increment-increment=2

# 跳过所有复制的错误
slave-skip-errors=all

重启两台数据库服务:

创建账号

登录134数据库 为135创建一个用户可以远程登录:

  create user 'backup'@'192.168.25.135' identified by 'backup'; 创建账号

图片

grant all privileges on *.* to 'backup'@'192.168.25.135' with grant option; //授权
ALTER USER 'backup'@'192.168.25.135' IDENTIFIED WITH mysql_native_password BY 'backup';
 flush privileges;  //刷新权限

登录134数据库 为135创建一个用户可以远程登录:

 create user 'backup'@'192.168.25.134' identified by 'backup'; 
 
 grant all privileges on *.* to 'backup'@'192.168.25.134' with grant option; 
 
ALTER USER 'backup'@'192.168.25.134' IDENTIFIED WITH mysql_native_password BY 'backup';

图片

flush privileges;

测试能否访问:

如果不能访问请看下防火墙

在134测试登录135的数据库:

mysql -h192.168.25.135 -ubackup -p -P3333   //登录135的数据库

继续连接,发现可以连接成功,并且可以查看数据

图片

show databases;  //查看数据库

图片

同理,在主二的数据库可以访问主一的数据库看下:

mysql -h192.168.10.51 -uroot1 -p    //登录主一的数据库
show databases;  //查看数据库

图片

互告bin-log信息

登录本地的mysql

主主同步还有主从同步都是基于binlog的原理进行,相当于我们打开了这个开关,每次操作数据库都会产生一个binlog日志,然后把binlog日志进行发给对方进行执行,这样数据就保持同步了

登录134数据库:

查看日志: show master status;

图片

设置同步:

CHANGE MASTER TO master_host = '192.168.25.135',
master_port = 3333,
master_user = 'backup',
master_password = 'backup',
master_log_file = 'DESKTOP-VKJ9MIL-bin.000005',
master_log_pos = 156

图片

备注: master_log_file与File值一致, master_log_pos与Position值一致

开始同步: start slave;

图片

查看同步情况:

show slave status\G;

当看到了两个yes,即:Slave_IO_Running: Yes

Slave_SQL_Running: Yes

说明已经配置成功了,但是这里有一个显示为no,明显有问题

图片

解决办法:

stop slave;
reset slave;
start slave;

然后我们继续查看同步情况;

show slave status\G;

图片

登录135数据库,同上操作:

查看日志: show master status;

图片

设置同步:

CHANGE MASTER TO master_host = '192.168.25.134',
master_port = 3333,
master_user = 'backup',
master_password = 'backup',
master_log_file = 'DESKTOP-VKJ9MIL-bin.000005',
master_log_pos = 156

图片

备注: master_log_file与File值一致, master_log_pos与Position值一致

开始同步: start slave;

图片

查看同步情况:

show slave status\G;

当看到了两个yes,即:Slave_IO_Running: Yes

Slave_SQL_Running: Yes

说明已经配置成功了,但是这里有一个显示为no,明显有问题

图片

解决办法:

stop slave;
reset slave;
start slave;

然后我们继续查看同步情况;

show slave status\G;

图片

灰常完美

出现下列问题,如果在134上登录数据库出现这个问题,可以在134远程登录135的数据库,并执行下列语句,再查询

stop slave;

start slave;

图片

图片

参看文档:

https://blog.csdn.net/qq_41256238/article/details/106944541

https://blog.csdn.net/qq_36756682/article/details/109647266

创建连接账号密码:

为本地数据库创建连接账号密码

在134和135上执行下列语句

create user 'backup666'@'192.168.25.1' identified by 'backup666'; 
 
 grant all privileges on *.* to 'backup666'@'192.168.25.1' with grant option; 

ALTER USER 'backup666'@'192.168.25.1' IDENTIFIED WITH mysql_native_password BY 'backup666';
flush privileges;  //刷新权限

nginx:配置:

–stream这个是主要模块,

nginx -V

目前配置的是被动健康检查,主动安全检查



#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8087;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
#下面就是需要修改配置的ip
stream {
        upstream mysql {
        server 192.168.25.134:3333 max_fails=1 fail_timeout=10s;
        server 192.168.25.135:3333 max_fails=1 fail_timeout=10s;
}
server {
        listen       3333;
        proxy_pass mysql;
}
}

另一个配置


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;
    #access_log      off;
    client_max_body_size 50m;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8088;
        server_name  192.168.16.20;

        #charset koi8-r;

        access_log  logs/host.access.log  main;
        #websocket相关配置
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header X-real-ip $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;

        location / {
            proxy_pass http://192.168.16.20:8087;
        }

        location ~ /ISAPI|SDK/ {
            if ($http_cookie ~ "webVideoCtrlProxy=(.+)") {
              proxy_pass http://$cookie_webVideoCtrlProxy;
              break;
            }
        }
        location ^~ /webSocketVideoCtrlProxy {
            #web socket
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;

            if ($http_cookie ~ "webVideoCtrlProxyWs=(.+)") {
                proxy_pass http://$cookie_webVideoCtrlProxyWs/$cookie_webVideoCtrlProxyWsChannel?$args;
                break;
            }
            if ($http_cookie ~ "webVideoCtrlProxyWss=(.+)") {
                proxy_pass http://$cookie_webVideoCtrlProxyWss/$cookie_webVideoCtrlProxyWsChannel?$args;
                break;
            }
        }
              #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # error_page  302  /50x.html;
        # location = /50x.html {
        #     root   html;
        # }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

     server {
        listen       8089;
        server_name  192.168.16.20;

        #charset koi8-r;

        access_log  logs/host.access.log  main;
        #websocket相关配置
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header X-real-ip $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;

        location / {
            root   "../webs";
            index  index.html index.htm;
        }

        location ~ /ISAPI|SDK/ {
            if ($http_cookie ~ "webVideoCtrlProxy=(.+)") {
              proxy_pass http://$cookie_webVideoCtrlProxy;
              break;
            }
        }
        location ^~ /webSocketVideoCtrlProxy {
            #web socket
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;

            if ($http_cookie ~ "webVideoCtrlProxyWs=(.+)") {
                proxy_pass http://$cookie_webVideoCtrlProxyWs/$cookie_webVideoCtrlProxyWsChannel?$args;
                break;
            }
            if ($http_cookie ~ "webVideoCtrlProxyWss=(.+)") {
                proxy_pass http://$cookie_webVideoCtrlProxyWss/$cookie_webVideoCtrlProxyWsChannel?$args;
                break;
            }
        }
              #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}
   
stream {
    upstream mysql {
        server 192.168.16.18:3306;
        server 192.168.16.19:3306 backup;
    

}

server {
        listen       3306;
        proxy_pass mysql;
        proxy_connect_timeout 5s;
}

}

项目配置:

这里修改为上面创建连接的账号密码

图片

PS:因为nginx已经监听了本机的3333端口,所以如果本机有冲突建议更改端口

启动nginx

双击ngxin即可,然后任务管理器查看进程

图片

启动项目:

图片

查看一下后台有没有报错 3079 - Multiple channels exist on the slave. Please provide channel name as an argument.

3079 - Multiple channels exist on the slave. Please provide channel name as an argument.

如果报错3079> 3079 - Multiple channels exist on the slave. Please provide channel name as an argument.

解决办法在后面要加一个channel参数:

master_port = 3306,

master_user = 'backup',

master_password = 'backup',

master_log_file = 'SERVER19-bin.000013',

master_log_pos = 1017381457 for channel "18";```

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1019133.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

亚马逊日本站养号测评需要哪些资源和注意点,如何确保账号的稳定性和纯净环境?

日本亚马Amazon.co.jp逊简称日亚,在日本国内亚马逊是アマゾン,日本亚马逊是美国亚马逊在日本成立的分公司,于2000年开通。 目前亚马逊日本站的情况是,流量大,产品少。有很多美国的卖家之间把亚马逊北美站的热卖产品加…

malloc是如何实现内存分配的?【深入理解】

文章目录 前言一、malloc实现原理概括?二、brk() 函数与mmap()函数三、mmap实现原理普通读写与mmap对比mmap内存映射实现过程mmap 的适用场景 前言 在C和C中,malloc函数是用于动态分配内存的常用函数。本文将深入探究malloc函数的内存分配实现机制&…

Linux 定时任务Crontab详解及常见问题解决

Linux 定时任务Crondtab简单了解 crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务 工具,并且会自动启动crond进程,cron…

Python 变量作用域

视频版教程 Python3零基础7天入门实战视频教程 在程序中定义一个变量时,这个变量是有作用范围的,变量的作用范围被称为它的作用域。根据定义变量的位置,变量分为两种。 局部变量。在函数中定义的变量,包括参数,都被称…

Django:四、Djiango如何连接使用MySQL数据库

一、安装数据库第三方插件 安装下载mysql第三方插件 pip install mysqlclient 二、创建MySQL数据库 ORM可以帮助我们做两件事: 创建、修改、删除数据库中的表(不用写SQL语句),但无法创建数据库操作表中的数据(不用…

长胜证券:煤价突破900元大关 GLP-1减重药进入集中获批期

上星期五,两市股指早盘震动上扬,午后回落走低。到收盘,沪指跌0.28%报3117.74点,深成指跌0.52%报10144.59点,创业板指涨跌0.45%报2002.73点,科创50指数涨0.71%;两市合计成交7217亿元,…

Linux系统调试篇——错误码介绍

文章目录 错误码错误码案例goto语句 错误码 在处理一些程序出错语句中,不管是用户空间还是内核空间,通常都会返回一个错误码。例如return -ERROR。 这些错误码是Linux内核定义的,它几乎包括了我们能想到的所有错误类型。 错误码的定义位于&…

GeoServer(配合Tomcat)安装与配置

GeoServer是什么? GeoServer是用于共享地理空间数据的开源服务器。专为互操作性而设计,它使用开放标准发布来自任何主要空间数据源的数据。GeoServer实现了行业标准的OGC协议,例如Web功能服务 (WFS),Web地图服务 (WMS) 和Web覆盖…

HTML5数据推送SSE原理及应用开发

JavaScript表达行为,CSS表达外观,注意HTML既表达结构(逻辑结构),又表达内容(数据本身)通常需要更新数据时,并不需要更新结构,正是这种不改变组织结构仅改变数据的诉求&am…

长胜证券:开盘竞价买卖技巧?

开盘竞价是股票生意进程中的一个重要环节,对于出资者来说,怎么在这个短暂的时间内下单买入或卖出股票,成为了检测出资者生意技巧的重要挑战。 一、认识开盘竞价 开盘竞价是指在股票商场开盘前,一切买进卖出单据的价格在必定的时间…

20个最佳实践提升Terraform工作流程|Part 1

Terraform 是管理基础设施及代码(IaC)最常用的工具之一,它能使我们安全且可预测地对基础设施应用更改。刚开始上手 Terraform 可能会感觉有些不容易,但很快就能对该工具有基本的了解,随之可以开始运行命令、创建和重构…

达观RPA实战-自定义控件创建excel表头

一、应用背景 工作中我们经常会对excel文件进行操作,比如获取表格数据后,需要在空白excel文件中先写入表头,在逐行写入数据。如果每次都需要在流程中进行表头的写入,流程会看起来很臃肿。此时,我们可以充分利用达观RPA中自定义控件来创建一个。后续涉及到写excel表数据,…

Sqilte3初步教程

文章目录 安装创建数据库创建和删除表插入行数据 安装 Windows下安装,首先到下载页面,下载Windows安装软件,一般是 sqlite-dll-win32-*.zip sqlite-tools-win32-*.zip下载之后将 其内容解压到同一个文件夹下,我把它们都放在了D…

儿童用白炽灯和护眼灯哪个好一点?适合儿童使用的台灯推荐

现今的近视已然成为普遍现象,而且有往低年龄段发展的趋势。对孩子来说,日常孩子在家里抹黑看书,晚上看手机不开灯等习惯,都会导致眼睛受损,继而引发近视。所以给孩子挑选一款合适的台灯还是很重要的!那么儿…

LeetCode 1159.市场分析2

数据准备 Create table If Not Exists Users (user_id int, join_date date, favorite_brand varchar(10)); Create table If Not Exists Orders (order_id int, order_date date, item_id int, buyer_id int, seller_id int); Create table If Not Exists Items (item_id int…

面试官问你前端性能优化时,他想问什么?

一直以来,前端性能优化都是面试过程中考察的热点题目。 相关的技术博客也层不出穷,我们总是能找到很多这样的文章, 从一个应用的各个层面开始分析,优化的种种手段,取得的种种效果。 往往篇幅越长,讲得越…

jQuery 框架学习笔记(基础)

What jQuery 是一种快速、简洁跨游览器的 JavaScript 函数库,其宗旨是“Write less, Do more”,它封装JavaScript常用的功能代码,提供一种简便的JavaScript设计模式,优化HTML文档操作、事件处理、动画设计和Ajax交互。 注意&…

浏览器清除所有断点

浏览器清除所有断点 问题分析 问题 谷歌浏览器清除所有断点 分析 在打断点的 Source 栏下 1、右键Breakpoints下的内容。 2、弹出选项,点击remove all breakpoints。

MySQL常用函数集锦 --- 字符串|数值|日期|流程函数总结

个人主页:兜里有颗棉花糖 欢迎 点赞👍 收藏✨ 留言✉ 加关注💓本文由 兜里有颗棉花糖 原创 收录于专栏【MySQL学习专栏】🎈 本专栏旨在分享学习MySQL的一点学习心得,欢迎大家在评论区讨论💌 目录 一、字符…

请问一下就是业务概念模型和业务逻辑模型有啥关系

请问一下就是业务概念模型和业务逻辑模型有啥关系? 业务概念模型和业务逻辑模型是业务建模的两个关键组成部分,两者密切相关但又有所不同。 1.业务概念模型:这是对业务术语、定义和关系的一种抽象表示。它是从业务专家那里获得的知识&#…