windows下mysql的高可用方案

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";```

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值