多语言时代
go net 绑定和监听时只能用 ip+端口(客户端自己做负载均匀,最简单的就是通过轮询)
upstream goserver{ server 127.0.0.1:9001; server 127.0.0.1:9002; server 127.0.0.1:9003;}server { listen 9090; server_name www.51golang.com ; location / { proxy_pass http://goserver; }}
MySQL主从相关
相关阅读
https://www.cnblogs.com/gl-developer/p/6170423.html MySQL主从复制(Master-Slave)实践 http://blog.csdn.net/Becivells/article/details/58332931 mysqlroute配置和使用 https://www.cnblogs.com/xuanzhi201111/p/5151666.html MySQL 5.7的多源复制 http://blog.csdn.net/leshami/article/details/50630691 MySQL GTID 主从复制
MySQL Router配置和使用
### MySQL Router使用了两个端口进行读写分离 默认端口号 作用 说明7001 默认的是读写端口 mode = read-write默认情况下第一台主数据库为写主库, 当第一台主数据库DOWN机后,第二台数据库被提升为主库, 如果第一台不出问题,第二台是不会被使用的。 可以作高可用使用7002 默认是读端口 如果设置以后,根据设置的列表进行轮询使用 注:默认情况下第一台主数据库为写主库,当第一台主数据库DOWN机后, 第二台数据库被提升为主库,稍后如果第一台主库被修复后, 那么默认仍然连接第二台为主库进行读写,不会自动切回到第一台主库。 ### 安装部署MySQL Router ## 这里使用二进制包安装 - 先解压下载好的文件到 /usr/local/ 下 tar xzvf mysql-router-2.1.4-linux-glibc2.12-x86-64bit.tar.gz -C /usr/local/cd /usr/local/mv mysql-router-2.1.4-linux-glibc2.12-x86-64bit mysql-router ## 创建 conf和log 目录 cd /usr/local/mysql-router mkdir conf mkdir log # 给 log 设置用户和组授权,没有mysql用户和组就先创建 chown mysql:mysql log ## 复制模版配置文件 cd /usr/local/mysql-router cp share/doc/mysqlrouter/sample_mysqlrouter.conf conf/mysqlrouter.conf ### 配置文件设置 cd /usr/local/mysql-router vim conf/mysqlrouter.conf[DEFAULT]# 日志存放目录 logging_folder = /usr/local/mysql-router/log # 插件存放目录 plugin_folder = /usr/local/mysql-router/lib/mysqlrouter # 配置文件存放目录 config_folder = /usr/local/mysql-router/conf # 运行目录 runtime_folder = /usr/local/mysql-router/run[logger]# 日志运行级别 level = INFO # 主节点故障转移配置[routing:basic_failover]# 写节点地址(这台机子的ip)bind_address=192.168.0.64# 写节点端口 bind_port = 7001# 连接超时时间 connect_timeout = 60# 最大连接数 max_connections = 2048# 模式,目前支持两种方式: read-write (读写) 和 read-only (读)mode = read-write # 主节点地址:默认情况下第一台主数据库为写主库, # 当第一台主数据库DOWN机后,第二台数据库被提升为主库 destinations = 192.168.0.61:3306,192.168.0.62:3306# 从节点负载均衡配置[routing:balancing]# 绑定的IP地址 bind_address=192.168.0.64# 监听的端口 bind_port = 7002# 连接超时时间 connect_timeout = 60# 最大连接数 max_connections = 2048# 模式:读 mode = read-only # 后端服务器地址,默认读进行----轮询 destinations = 192.168.0.62:3306,192.168.0.63:3306[keepalive]# 健康检查频率60s interval = 60### 启动服务/usr/local/mysql-router/bin/mysqlrouter -c /usr/local/mysql-router/conf/mysqlrouter.conf
MySQL主从同步配置.txt
# 主服务器IP:192.168.0.61# 从服务器IP:192.168.0.62### 1.修改主服务器的主数据库/etc/my.cf配置[mysqld]# 打开日志(主机需要打开)log-bin=mysql-bin # 服务器id server-id=61# 要给从库同步的数据库,有多个数据库,可重复此参数 binlog-do-db=test # 不给从库同步的数据库,有多个数据库,可重复此参数 binlog-ignore-db=mysql # 表示让从库跳过所有错误 slave-skip-errors=all # 表示主机每次提交事务的时候把二进制日志的内容同步到磁盘上 sync_binlog=1# 从库的中继日志 #relay_log=mysql-relay-bin #下面跟主主复制有关 # 从库的binlog是否记录来源于主库的操作记录,默认 off #log-slave-updates=on # 该服务器自增列的初始值 #auto-increment-offset=1# 该服务器自增列增量 #auto-increment-increment=2### 2.修改从服务器的从数据库/etc/my.cf配置[mysqld]# 服务器id server-id=62# 同步主库的数据库,有多个数据库,可重复此参数 replicate-do-db=test # 不同步主库数据库,有多个数据库,可重复此参数 replicate-ignore-db=mysql # 表示让从库跳过所有错误 slave-skip-errors=all # 表示主机每次提交事务的时候把二进制日志的内容同步到磁盘上 sync_binlog=1#下面跟主主复制有关 # 从库的binlog是否记录来源于主库的操作记录,默认 off #log-slave-updates=on # 该服务器自增列的初始值 #auto-increment-offset=2# 该服务器自增列增量 #auto-increment-increment=2# 从库的中继日志 #relay_log=mysql-relay-bin ## 注意:如果为多主的话注意设置 auto-increment-offset 和 auto-increment-increment # 如为双主的设置: # 服务器 61 自增列显示为:1,3,5,7,……(offset=1,increment=2) # 服务器 62 自增列显示为:2,4,6,8,……(offset=2,increment=2) ### 3.修改之后,重启MySQL主数据库和MySQL从数据库的服务 ### 4.配置主服务器的主数据库 # 主数据库授权同步账户 ( 用户: test 密码: 123456 )# 说明一下192.168.0.%,这个配置是指明 test 用户所在服务器,这里%是通配符, # 表示192.168.0.0-192.168.0.255的Server都可以以 test 用户登陆主服务器。当然你也可以指定固定Ip。 mysql> GRANT REPLICATION SLAVE ON *.* TO 'test'@'192.168.0.%' IDENTIFIED BY '123456';# 刷新权限 mysql> FLUSH PRIVILEGES;# 查看主服务状态 mysql> SHOW MASTER STATUS;# 主数据库锁表(禁止再插入数据以获取主数据库的的二进制日志坐标)mysql> FLUSH TABLES WITH READ LOCK;# 查看主服务状态,获取主数据库的的二进制日志坐标( Position )mysql> SHOW MASTER STATUS;+------------------+----------+---------------------------------------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+---------------------------------------------+------------------+-------------------+| mysql-bin.000001 | 154 | test | | |+------------------+----------+---------------------------------------------+------------------+-------------------+1 row in set (0.00 sec)# 解锁主数据的锁表操作 mysql> UNLOCK TABLES;### 在解锁前可以备份数据库 mysqldump ### 5.配置从服务器用于连接master服务器 # 配置和改变slave服务器用于连接master服务器的参数【注意,MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=154 是通过前面的主数据库SHOW MASTER STATUS;得到】 mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.61', MASTER_USER='test', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154;# 开启SLAVE同步 mysql> START SLAVE;# 查看下slave状态 mysql> SHOW SLAVE STATUS \G;*************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.0.61 Master_User: test Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 154 Relay_Log_File: iZu1xmovyagZ-relay-bin.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: test Replicate_Ignore_DB: Replicate_Do_Table:# 当Slave_IO_Running和Slave_SQL_Running都为Yes,才说明主从复制成功 ### 6.停止SLAVE同步 STOP SLAVE;### 7.撤销已经赋予给MySQL同步账户的权限 revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可: mysql> GRANT REPLICATION SLAVE ON *.* TO 'test'@'192.168.0.%' IDENTIFIED BY '123456';mysql> REVOKE REPLICATION SLAVE ON *.* FROM 'test'@'192.168.0.%';####### 配置MySQL GTID 主从复制 # 跟上面配置一样,只新增下面两项,主从都要[mysqld]gtid_mode=on #开启gtid模式 enforce_gtid_consistency=on #强制gtid一致性,开启后对于特定create table不被支持 #配置从服务器用于连接master服务器 - 不需要 MASTER_LOG_FILE, MASTER_LOG_POS 参数 mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.61', MASTER_USER='test', MASTER_PASSWORD='123456', MASTER_PORT=3306, MASTER_AUTO_POSITION = 1;
设置多台主 第一台蹦了, 用第二台, 第一台修好了 , 不会自动切换到第二台
http://blog.csdn.net/qq_33936481/article/details/73008730
http://www.lepus.cc/page/opensource
zabbix 监控
http://blog.51cto.com/wangwei007/1833332
https://www.zabbix.com/download?zabbix=3.4&os_distribution=centos&os_version=7&db=MySQL
Error connecting to database: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
http://blog.csdn.net/u011085172/article/details/72662940
--with-unix-socket-path=/usr/local/mysql/tmp/mysql.sock\
https://my.oschina.net/u/209161/blog/865056
[root@localhost ~]# vim /var/log/zabbix/zabbix_server.log
mysql> update user set host = '%' where user ='zabbix';
grant all privileges on . to zabbix@localhost identified by 'W#91rin567'
GRANT ALL PRIVILEGES ON . TO ‘username’@‘%’ IDENTIFIED BY 'password’
监控 Zabbix、Nagios、Open-Falcon
https://www.zabbix.com/documentation/3.4/zh/start
Zabbix Server
DBHost=192.168.1.81 本机Ip #不要用127.0.0.1
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
数据库连接失败
host, user password
zabbix Agent
grant all privileges on . to 'zabbix2'@'%' identified by '123456' --冲突
grant all privileges on . to zabbix2@localhost identified by '123456'
zabbix server、zabbix agent、zabbix proxy
3.10 高并发网络架构>[success] ### 主从复制
IP - 192.168.1.81 -- 主
[root@localhost ~]# vim /etc/my.cnf #[mysqld]#GTID server_id=81gtid_mode=on enforce_gtid_consistency=on #binlog log_bin=master-binlog log-slave-updates=1binlog_format=row #relay log skip_slave_start=1[root@localhost ~]# systemctl restart mysqld
mysql> use mysql;mysql> update user set host = '%' where user ='root';mysql> flush privileges;
IP - 192.168.1.82 -- 从
[root@localhost ~]# vim /etc/my.cnf #[mysqld]#GTID server_id=82gtid_mode=on enforce_gtid_consistency=on #binlog log-bin=slave-binlog log-slave-updates=1binlog_format=row #relay log skip_slave_start=1[root@localhost ~]# systemctl restart mysqld;
mysql> use mysql;mysql> update user set host = '%' where user ='root';mysql> flush privileges;
[root@localhost ~]# mysql -u root -p mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.81', MASTER_USER='root', MASTER_PASSWORD='123456', MASTER_PORT=3306, MASTER_AUTO_POSITION = 1;
mysql> start slave;
mysql> show slave status \G;
IP - 192.168.1.83 -- 从
[root@localhost ~]# vim /etc/my.cnf #[mysqld]#GTID server_id=83gtid_mode=on enforce_gtid_consistency=on #binlog log-bin=slave-binlog log-slave-updates=1binlog_format=row #relay log skip_slave_start=1relay_log_info_repository = TABLE master_info_repository = TABLE [root@localhost ~]# systemctl restart mysqld;
mysql> use mysql;mysql> update user set host = '%' where user ='root';mysql> flush privileges;
多源复制
[root@localhost ~]# mysql -u root -p mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.81', MASTER_USER='root', MASTER_PASSWORD='123456', MASTER_PORT=3306, MASTER_AUTO_POSITION = 1 FOR CHANNEL 'channel1'; mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.82', MASTER_USER='root', MASTER_PASSWORD='123456', MASTER_PORT=3306, MASTER_AUTO_POSITION = 1 FOR CHANNEL 'channel2';
mysql> start slave;
mysql> show slave status \G;
主主复制
IP - 192.168.1.81
[root@localhost ~]# mysql -u root -p mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.82', MASTER_USER='root', MASTER_PASSWORD='123456', MASTER_PORT=3306, MASTER_AUTO_POSITION = 1;
MySQL Router
[root@localhost local]# wget https://dev.mysql.com/get/Downloads/MySQL-Router/mysql-router-2.1.5-linux-glibc2.12-x86-64bit.tar.gz[root@localhost local]# tar -zxvf mysql-router-2.1.5-linux-glibc2.12-x86-64bit.tar.gz -C /usr/local/[root@localhost local]# cd /usr/local/[root@localhost local]# mv mysql-router-2.1.5-linux-glibc2.12-x86-64bit mysql-router[root@localhost local]# cd /usr/local/mysql-router[root@localhost local]# mkdir conf[root@localhost local]# mkdir log[root@localhost local]# chown mysql:mysql log ## 复制模版配置文件 cd /usr/local/mysql-router cp share/doc/mysqlrouter/sample_mysqlrouter.conf conf/mysqlrouter.conf ### 配置文件设置 cd /usr/local/mysql-router[root@localhost mysql-router]# cat > /usr/local/mysql-router/conf/mysqlrouter.conf << EOT[DEFAULT]# 日志存放目录 logging_folder = /usr/local/mysql-router/log # 插件存放目录 plugin_folder = /usr/local/mysql-router/lib/mysqlrouter # 配置文件存放目录 config_folder = /usr/local/mysql-router/conf # 运行目录 runtime_folder = /usr/local/mysql-router/run[logger]# 日志运行级别 level = INFO # 主节点故障转移配置[routing:basic_failover]# 写节点地址(这台机子的ip)bind_address=192.168.1.80# 写节点端口 bind_port = 7001# 连接超时时间 connect_timeout = 60# 最大连接数 max_connections = 2048# 模式,目前支持两种方式: read-write (读写) 和 read-only (读)mode = read-write # 主节点地址:默认情况下第一台主数据库为写主库, # 当第一台主数据库DOWN机后,第二台数据库被提升为主库 destinations = 192.168.1.81:3306,192.168.1.82:3306# 从节点负载均衡配置[routing:balancing]# 绑定的IP地址 bind_address=192.168.1.80# 监听的端口 bind_port = 7002# 连接超时时间 connect_timeout = 60# 最大连接数 max_connections = 2048# 模式:读 mode = read-only # 后端服务器地址,默认读进行----轮询 destinations = 192.168.1.83:3306,192.168.1.82:3306[keepalive]# 健康检查频率60s interval = 60EOT ### 启动服务/usr/local/mysql-router/bin/mysqlrouter -c /usr/local/mysql-router/conf/mysqlrouter.conf
测试
可能出现的错误
ERROR 1777 (HY000): CHANGE MASTER TO MASTER_AUTO_POSITION = 1 cannot be executed because @@GLOBAL.GTID_MODE = OFF.Slave is not configured or failed to initialize properly. You must at least set --server-id to enable either a master or a slave. Additional error messages can be found in the MySQL error log #修改server_uuid,确保值唯一[root@localhost ~]# vim /data/mysql/auto.cnf mysql> show variables like 'server_uuid';
cmake \-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \-DSYSCONFDIR=/etc \ -DMYSQL_DATADIR=/data/mysql \ -DWITH_BOOST=./boost \ -DDEFAULT_CHARSET=utf8mb4 \ -DDEFAULT_COLLATION=utf8mb4_general_ci \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \-WITH_DEBUG=1 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ #安装目录-DSYSCONFDIR=/etc \ #默认的my.cnf路径 -DMYSQL_DATADIR=/data/mysql \ #数据存放目录-DWITH_BOOST=./boost \ #Boost库位置-DDEFAULT_CHARSET=utf8mb4 \ #服务器字符集(默认字符编码) -DDEFAULT_COLLATION=utf8mb4_general_ci \ #默认排序规则-DWITH_INNOBASE_STORAGE_ENGINE=1 \ #安装innodb存储引擎-DENABLED_LOCAL_INFILE=1 \ #允许从本地导入数据 -WITH_DEBUG=1 #是否包括调试支持 cmake \ cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DMYSQL_DATADIR=/data/mysql -DWITH_BOOST=./boost -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -WITH_DEBUG=1 cmake . \-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \-DSYSCONFDIR=/etc \-DMYSQL_DATADIR=/data/mysql \-DWITH_BOOST=./boost \-DDEFAULT_CHARSET=utf8mb4 \-DDEFAULT_COLLATION=utf8mb4_general_ci \-DWITH_INNOBASE_STORAGE_ENGINE=1 \-DWITH_DEBUG=1cmake . \-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \-DMYSQL_DATADIR=/data/mysql \-DWITH_BOOST=./boost \-DSYSCONFDIR=/etc \-DWITH_INNOBASE_STORAGE_ENGINE=1 \-DDEFAULT_CHARSET=utf8mb4 \-DDEFAULT_COLLATION=utf8mb4_general_ci \-DENABLED_LOCAL_INFILE=1 \-DWITH_DEBUG=1