首页 文章 精选 留言 我的

精选列表

搜索[高可用],共10000篇文章
优秀的个人博客,低调大师

企业级高可用Web架构之HAProxy+Keepalived

细数下来,算是东莞的项目的话,HAProxy+Keepalived我差不多也有三套在线上跑了,另外,这套Web方案也是我的一拍网的备份方案之一,目前也在测试,如果速度和稳定性够强劲的话,我也考虑将LVS+Keepalived换成HAProxy+Keepalived,关于HAProxy的语法和安装步骤请参考我的专题系列文章http://network.51cto.com/art/201101/241997.htm,另外,此篇文章跟刘天斯的不一样,我主要用其作为Web级别的负载均衡(七层应用)。 一、线上跑的HAProxy配置文件,代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 global log 127.0.0.1 local0 maxconn 65535 chroot /usr/local/haproxy uid 99 gid 99 daemon nbproc 8 pidfile /usr/local/haproxy/haproxy .pid #debug defaults log 127.0.0.1 local3 mode http option httplog option httpclose option dontlognull option forwardfor option redispatch retries 2 maxconn 2000 balance source stats uri /web-status contimeout 5000 clitimeout 50000 srvtimeout 50000 listen www.1paituan.com bind *:80 mode http option httplog log global option httpchk HEAD /index .jsp HTTP /1 .0 server web1 203.93.236.147:80 weight 5 check inter 2000 rise 2 fall 3 server web2 203.93.236.146:80 weight 3 check inter 2000 rise 2 fall 3 二、HAProxy的监控脚本我没有做,因为生产环境下HAProxy宕机的可能性微乎其乎,这个实施起来也简单,我们可以用如下命令来监控,判断是否返回了正常的200代码,命令下所示: 1 curl -s -- head http: //www .1paituan.com /index .jsp | awk '/HTTP/ {print $2}' 三、加上日志支持,代码如下: 1 2 3 4 5 6 7 8 vim /etc/syslog .conf 添加: local3.* /var/log/haproxy .log local0.* /var/log/haproxy .log vim /etc/sysconfig/syslog 修改: SYSLOGD_OPTIONS= "-r -m 0" service syslog restart 四、大家需要注意的几个地方是: 1)HAProyx采用的是balance source机制,它跟Nginx的ip_hash机制原理类似,是让客户机访问时始终访问后端的某一台真实的web服务器,这样让session就固定下来了; 2)option httpchk HEAD /index.jsp HTTP/1.0 是网页监控,如果HAProxy检测不到Web的根目录下没有index.jsp,就会产生503报错。 3)有网友说HAProxy必须采用listen 203.93.236.141:80这样类似的格式,这样其实不好,做集群时会导致从机启动不了,我们可以用bind *:80的方式代替。 4)HAProxy的并发监控和日志收集分析是下一步考虑的事情。 本文转自 抚琴煮酒 51CTO博客,原文链接:http://blog.51cto.com/yuhongchun/669491,如需转载请自行联系原作者

优秀的个人博客,低调大师

PostgreSQL 数据库HAProxy和PgBouncer配置高可用架构

一.OS Packages CentOS 7.2 X64 pcre-devel openssl-devel zlib-devel 二.编译安装haproxy 1.编译安装 haproxy-1.7.1.tar.gz # make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 PREFIX=/usr/local/haproxy ...... ...... ...... oto_http.o src/raw_sock.o src/backend.o src/tcp_rules.o src/lb_chash.o src/lb_fwlc.o src/lb_fwrr.o src/lb_map.o src/lb_fas.o src/stream_interface.o src/stats.o src/proto_tcp.o src/applet.o src/session.o src/stream.o src/hdr_idx.o src/ev_select.o src/signal.o src/acl.o src/sample.o src/memory.o src/freq_ctr.o src/auth.o src/proto_udp.o src/compression.o src/payload.o src/hash.o src/pattern.o src/map.o src/namespace.o src/mailers.o src/dns.o src/vars.o src/filters.o src/flt_http_comp.o src/flt_trace.o src/flt_spoe.o src/cli.o src/ev_poll.o src/ev_epoll.o src/ssl_sock.o src/shctx.o ebtree/ebtree.o ebtree/eb32tree.o ebtree/eb64tree.o ebtree/ebmbtree.o ebtree/ebsttree.o ebtree/ebimtree.o ebtree/ebistree.o -lcrypt -lz -ldl -lssl -lcrypto -ldl -L/usr/lib -lpcreposix -lpcre gcc -Iinclude -Iebtree -Wall -O2 -g -fno-strict-aliasing -Wdeclaration-after-statement -DCONFIG_HAP_LINUX_SPLICE -DTPROXY -DCONFIG_HAP_LINUX_TPROXY -DCONFIG_HAP_CRYPT -DUSE_ZLIB -DENABLE_POLL -DENABLE_EPOLL -DUSE_CPU_AFFINITY -DASSUME_SPLICE_WORKS -DUSE_ACCEPT4 -DNETFILTER -DUSE_GETSOCKNAME -DUSE_OPENSSL -DUSE_SYSCALL_FUTEX -DUSE_PCRE -I/usr/include -DCONFIG_HAPROXY_VERSION=\"1.7.1\" -DCONFIG_HAPROXY_DATE=\"2016/12/13\" \ -DSBINDIR='"/usr/local/haproxy/sbin"' \ -c -o src/haproxy-systemd-wrapper.o src/haproxy-systemd-wrapper.c gcc -g -o haproxy-systemd-wrapper src/haproxy-systemd-wrapper.o -lcrypt -lz -ldl -lssl -lcrypto -ldl -L/usr/lib -lpcreposix -lpcre # # make install PREFIX=/usr/local/haproxy install -d "/usr/local/haproxy/sbin" install haproxy "/usr/local/haproxy/sbin" install -d "/usr/local/haproxy/share/man"/man1 install -m 644 doc/haproxy.1 "/usr/local/haproxy/share/man"/man1 install -d "/usr/local/haproxy/doc/haproxy" for x in configuration management architecture cookie-options lua WURFL-device-detection proxy-protocol linux-syn-cookies network-namespaces DeviceAtlas-device-detection 51Degrees-device-detection netscaler-client-ip-insertion-protocol close-options SPOE intro; do \ install -m 644 doc/$x.txt "/usr/local/haproxy/doc/haproxy" ; \ done # 2.配置文件 # pwd /usr/local/haproxy # mkdir etc # vi ha_proxy.conf global log 127.0.0.1 local0 info maxconn 40960 user haproxy group haproxy daemon nbproc 1 chroot /usr/local/haproxy pidfile /usr/local/haproxy/haproxy.pid defaults log global mode tcp option tcplog option dontlognull option redispatch retries 3 maxconn 2000 timeout connect 5s timeout client 360s timeout server 360s frontend postgres-front bind 192.168.199.201:5432 default_backend postgresql backend postgresql mode tcp option pgsql-check user haproxy balance roundrobin server postgresql_01 192.168.199.201:5430 weight 1 check inter 5s rise 2 fall 3 server postgresql_02 192.168.199.201:5431 weight 1 check inter 5s rise 2 fall 3 listen admin-stats mode http option httplog bind 0.0.0.0:8888 stats enable stats refresh 30s stats uri /dbs stats realm welcome login\ Haproxy stats auth admin:admin stats admin if TRUE # # 3.配置PATH变量 # vi /etc/profile unset -f pathmunge export PATH=/usr/local/haproxy/sbin:$PATH 三、安装pgbouncer 1.安装libevent libevent-2.0.22-stable.tar.gz # ./configure # make # make install ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5 2.安装pgbouncer # ./configure --prefix=/opt/pgbouncer/1.17.2 ...... ...... Results c-ares = no evdns = yes udns = no tls = yes # make # make install # cd /opt/pgbouncer/1.17.2/ # mkdir etc log # 配置文件 # cd /opt/pgbouncer/1.17.2/share/doc/pgbouncer # cp pgbouncer.ini userlist.txt /opt/pgbouncer/1.17.2/etc/ # cd /opt/pgbouncer/1.17.2/etc/ vi pgbouncer.ini [root@localhost etc]# more pgbouncer.ini ;; database name = connect string ;; ;; connect string params: ;; dbname= host= port= user= password= ;; client_encoding= datestyle= timezone= ;; pool_size= connect_query= [databases] ;; * indicate testdb, haproxy * = port=6432 dbname=testdb user=postgres password=111111 ; foodb over unix socket ;foodb = ; redirect bardb to bazdb on localhost ;bardb = host=localhost dbname=bazdb ; access to dest database will go with single user ;forcedb = host=127.0.0.1 port=300 user=baz password=foo client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1' ; use custom pool sizes ;nondefaultdb = pool_size=50 reserve_pool_size=10 ; fallback connect string ;* = host=testserver ;; Configuration section [pgbouncer] ;;; ;;; Administrative settings ;;; logfile = /opt/pgbouncer/1.17.2/log/pgbouncer.log pidfile = /opt/pgbouncer/1.17.2/bin/pgbouncer.pid ;;; ;;; Where to wait for clients ;;; ; ip address or * which means all ip-s listen_addr = * listen_port = 5430 ; unix socket is also used for -R. ; On debian it should be /var/run/postgresql ;unix_socket_dir = /tmp ;unix_socket_mode = 0777 ;unix_socket_group = ;;; ;;; TLS settings for accepring clients ;;; ;; disable, allow, require, verify-ca, verify-full ;client_tls_sslmode = disable ;; Path to file that contains trusted CA certs ;client_tls_ca_file = <system default> ;; Private key and cert to present to clients. ;; Required for accepting TLS connections from clients. ;client_tls_key_file = ;client_tls_cert_file = ;; fast, normal, secure, legacy, <ciphersuite string> ;client_tls_ciphers = fast ;; all, secure, tlsv1.0, tlsv1.1, tlsv1.2 ;client_tls_protocols = all ;; none, auto, legacy ;client_tls_dheparams = auto ;; none, auto, <curve name> ;client_tls_ecdhcurve = auto ;;; ;;; TLS settings for connecting to backend databases ;;; ;; disable, allow, require, verify-ca, verify-full ;server_tls_sslmode = disable ;; Path to that contains trusted CA certs ;server_tls_ca_file = <system default> ;; Private key and cert to present to backend. ;; Needed only if backend server require client cert. ;server_tls_key_file = ;server_tls_cert_file = ;; all, secure, tlsv1.0, tlsv1.1, tlsv1.2 ;server_tls_protocols = all ;; fast, normal, secure, legacy, <ciphersuite string> ;server_tls_ciphers = fast ;;; ;;; Authentication settings ;;; ; any, trust, plain, crypt, md5 auth_type = md5 ;auth_file = /8.0/main/global/pg_auth auth_file = /opt/pgbouncer/1.17.2/etc/userlist.txt ;; Path to HBA-style auth config ;auth_hba_file = ;; Query to use to fetch password from database. Result ;; must have 2 columns - username and password hash. ;auth_query = SELECT usename, passwd FROM pg_shadow WHERE usename=$1 ;;; ;;; Users allowed into database 'pgbouncer' ;;; ; comma-separated list of users, who are allowed to change settings ;admin_users = user2, someadmin, otheradmin admin_users = pgbadmin ; comma-separated list of users who are just allowed to use SHOW command ;stats_users = stats, root ;;; ;;; Pooler personality questions ;;; ; When server connection is released back to pool: ; session - after client disconnects ; transaction - after transaction finishes ; statement - after statement finishes pool_mode = transaction ; ; Query for cleaning connection immediately after releasing from client. ; No need to put ROLLBACK here, pgbouncer does not reuse connections ; where transaction is left open. ; ; Query for 8.3+: ; DISCARD ALL; ; ; Older versions: ; RESET ALL; SET SESSION AUTHORIZATION DEFAULT ; ; Empty if transaction pooling is in use. ; server_reset_query = ; Whether server_reset_query should run in all pooling modes. ; If it is off, server_reset_query is used only for session-pooling. ;server_reset_query_always = 0 ; ; Comma-separated list of parameters to ignore when given ; in startup packet. Newer JDBC versions require the ; extra_float_digits here. ; ;ignore_startup_parameters = extra_float_digits ; ; When taking idle server into use, this query is ran first. ; SELECT 1 ; server_check_query = select 1 ; If server was used more recently that this many seconds ago, ; skip the check query. Value 0 may or may not run in immediately. server_check_delay = 10 ;; Use <appname - host> as application_name on server. ;application_name_add_host = 0 ;;; ;;; Connection limits ;;; ; total number of clients that can connect max_client_conn = 10240 ; default pool size. 20 is good number when transaction pooling ; is in use, in session pooling it needs to be the number of ; max clients you want to handle at any moment default_pool_size = 100 ;; Minimum number of server connections to keep in pool. min_pool_size = 80 ; how many additional connection to allow in case of trouble ;reserve_pool_size = 5 ; if a clients needs to wait more than this many seconds, use reserve pool ;reserve_pool_timeout = 3 ; how many total connections to a single database to allow from all pools ;max_db_connections = 50 ;max_user_connections = 50 ; If off, then server connections are reused in LIFO manner ;server_round_robin = 0 ;;; ;;; Logging ;;; ;; Syslog settings ;syslog = 0 ;syslog_facility = daemon ;syslog_ident = pgbouncer ; log if client connects or server connection is made ;log_connections = 1 ; log if and why connection was closed ;log_disconnections = 1 ; log error messages pooler sends to clients ;log_pooler_errors = 1 ;; Period for writing aggregated stats into log. ;stats_period = 60 ;; Logging verbosity. Same as -v switch on command line. ;verbose=0 ;;; ;;; Timeouts ;;; ;; Close server connection if its been connected longer. server_lifetime = 300 ;; Close server connection if its not been used in this time. ;; Allows to clean unnecessary connections from pool after peak. ;server_idle_timeout = 60 ;; Cancel connection attempt if server does not answer takes longer. server_connect_timeout = 10 ;; If server login failed (server_connect_timeout or auth failure) ;; then wait this many second. server_login_retry = 3 ;; Dangerous. Server connection is closed if query does not return ;; in this time. Should be used to survive network problems, ;; _not_ as statement_timeout. (default: 0) ;query_timeout = 0 ;; Dangerous. Client connection is closed if the query is not assigned ;; to a server in this time. Should be used to limit the number of queued ;; queries in case of a database or network failure. (default: 120) ;query_wait_timeout = 120 ;; Dangerous. Client connection is closed if no activity in this time. ;; Should be used to survive network problems. (default: 0) ;client_idle_timeout = 0 ;; Disconnect clients who have not managed to log in after connecting ;; in this many seconds. ;client_login_timeout = 60 ;; Clean automatically created database entries (via "*") if they ;; stay unused in this many seconds. ; autodb_idle_timeout = 3600 ;; How long SUSPEND/-R waits for buffer flush before closing connection. ;suspend_timeout = 10 ;; Close connections which are in "IDLE in transaction" state longer than ;; this many seconds. idle_transaction_timeout = 300 ;;; ;;; Low-level tuning options ;;; ;; buffer for streaming packets ;pkt_buf = 4096 ;; man 2 listen listen_backlog = 2048 ;; Max number pkt_buf to process in one event loop. ;sbuf_loopcnt = 5 ;; Maximum Postgres protocol packet size. ;max_packet_size = 2147483647 ;; networking options, for info: man 7 tcp ;; Linux: notify program about new connection only if there ;; is also data received. (Seconds to wait.) ;; On Linux the default is 45, on other OS'es 0. ;tcp_defer_accept = 0 ;; In-kernel buffer size (Linux default: 4096) ;tcp_socket_buffer = 0 ;; whether tcp keepalive should be turned on (0/1) ;tcp_keepalive = 1 ;; following options are Linux-specific. ;; they also require tcp_keepalive=1 ;; count of keepaliva packets ;tcp_keepcnt = 0 ;; how long the connection can be idle, ;; before sending keepalive packets ;tcp_keepidle = 0 ;; The time between individual keepalive probes. ;tcp_keepintvl = 0 ;; DNS lookup caching time ;dns_max_ttl = 15 ;; DNS zone SOA lookup period ;dns_zone_check_period = 0 ;; DNS negative result caching time ;dns_nxdomain_ttl = 15 ;;; ;;; Random stuff ;;; ;; Hackish security feature. Helps against SQL-injection - when PQexec is disabled, ;; multi-statement cannot be made. ;disable_pqexec=0 ;; Config file to use for next RELOAD/SIGHUP. ;; By default contains config file from command line. ;conffile ;; Win32 service name to register as. job_name is alias for service_name, ;; used by some Skytools scripts. ;service_name = pgbouncer ;job_name = pgbouncer ;; Read additional config from the /etc/pgbouncer/pgbouncer-other.ini file ;%include /etc/pgbouncer/pgbouncer-other.ini # # vi userlist.txt "pgbadmin" "pgbouncer123" "haproxy" "haproxy" # 3.修改数据库访问端口'6432' # netstat -ltnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 844/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1572/master tcp 0 0 0.0.0.0:6432 0.0.0.0:* LISTEN 14974/postmaster # 设置PATH # vi /etc/profile export PATH=/opt/pgbouncer/1.17.2/bin:/usr/local/haproxy/sbin:$PATH 4.初始化数据库 建立数据库role haproxy postgres=# CREATE ROLE haproxy LOGIN ENCRYPTED PASSWORD 'md53b4da8c0b2ce93c288203e3d363895a6' SUPERUSER INHERIT CREATEDB CREATEROLE REPLICATION; 建立数据库 postgres=# \c postgres haproxy; postgres=# create database testdb; 建立表 testdb=# create table t1(id int, name varchar(20)); CREATE TABLE testdb=# 数据库访问控制 # vi pg_hba.conf host all all 192.168.199.0/24 trust 5.启动pgbouncer # chown postgres.postgres -R /opt/pgbouncer/ # su - postgres 1)pgbouncer01 $ pgbouncer -d /opt/pgbouncer/1.17.2/etc/pgbouncer.ini 2016-12-20 15:09:52.182 15013 LOG File descriptor limit: 1024 (H:4096), max_client_conn: 10240, max fds possible: 10350 $ 2) pgbouncer02 配置文件 ; ip address or * which means all ip-s listen_addr = * listen_port = 5431 ;;; ;;; Administrative settings ;;; logfile = /opt/pgbouncer/1.17.2/log/pgbouncer02.log pidfile = /opt/pgbouncer/1.17.2/bin/pgbouncer02.pid $ pgbouncer -d /opt/pgbouncer/1.17.2/etc/pgbouncer02.ini 2016-12-20 15:12:06.964 15020 LOG File descriptor limit: 1024 (H:4096), max_client_conn: 10240, max fds possible: 10350 $ 启动ha-proxy # haproxy -f /usr/local/haproxy/etc/ha_proxy.conf # netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:5430 0.0.0.0:* LISTEN 16497/pgbouncer tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 844/sshd tcp 0 0 0.0.0.0:5431 0.0.0.0:* LISTEN 16500/pgbouncer tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 16510/haproxy tcp 0 0 192.168.199.201:5432 0.0.0.0:* LISTEN 16510/haproxy tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1572/master tcp 0 0 0.0.0.0:6432 0.0.0.0:* LISTEN 14974/postmaster tcp6 0 0 :::5430 :::* LISTEN 16497/pgbouncer tcp6 0 0 :::22 :::* LISTEN 844/sshd tcp6 0 0 :::5431 :::* LISTEN 16500/pgbouncer tcp6 0 0 ::1:25 :::* LISTEN 1572/master tcp6 0 0 :::6432 :::* LISTEN 14974/postmaster # 使用pgadmin客户端访问DB:testdb, user:haproxy, password:haproxy,IP:192.168.199.201, port:5432 测试连接 insert into t1 select generate_series(1,10000),'name'; $ more test.conf SELECT id FROM t1 WHERE id = 50; pgbench -h 192.168.199.201 -U haproxy -c 25 -j 25 -M prepared -n -s 500 -T 60 testdb -f ./test.conf 本文转自 pgmia 51CTO博客,原文链接:http://blog.51cto.com/heyiyi/1884440

优秀的个人博客,低调大师

Mysql高可用复制原理及主从实例测试解析

一、Mysql复制简介 使用mysql复制功能可以将主数据的数据复制到多台从服务器上。默认情况下,采用异步传输方式,数据复制可以在各种不同的网路环境中进行。主从复制技术在企业生产中得到了广泛应用,它避免了数据库的单点故障,当一台服务器宕机,其他服务器一样可以提供稳定可靠的数据服务。 1 mysql 复制原理 Mysql复制功能是将数据分布在多个系统上,这种机制是通过将Mysql的某一台服务器(master)的数据复制到其它服务器(slaves)上来实现的。复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。主服务器将更新写入二进制日志文件,从服务器接收到这些更新的日志内容,就按照日志内容在本机执行一遍,由此实现主从数据复制。主服务器写入日志时也维护着文件的一个索引以跟踪日志循环,当一个从服务器连接主服务器时,它通知主从服务器在日志中读取的最后一次成功更新的位置,从服务器接收那个时间点起发生的任何更新,然后封锁并等待主服务器通知新的更新。 2 mysql 复制优点 高性能:通过将请求分配给多台不同的从服务器,提高性能及速度,在此种环境中,所有写操作必须在主服务器进行,读操作可以平均分配给多台从服务器。 数据安全:数据通过主从复制,把主服务器的数据复制的一份或者多份,即实现了数据的安全备份 数据分享:对于大型企业,各个地区的分公司也需要共享总部的数据资源,只需要用主从复制即可实现数据的共享,而且可以仅仅在需要时进行复制。 3 mysql 复制格式 基于语句的复制: 在主服务器上执行的SQL语句,在从服务器上执行同样的语句。MySQL默认采用基于语句的复制,效率比较高。 基于行的复制:把改变的内容复制过去,而不是把命令在从服务器上执行一遍。 混合类型的复制: 默认采用基于语句的复制,一旦发现基于语句的无法精确的复制时,就会采用基于行的复制。 4 mysql 复制步骤 整体上来说,复制有3个步骤: (1) master将改变记录到二进制日志(binary log)中(二进制日志事件,binary log events); (2) slave将master的binary log events拷贝到它的中继日志(relay log); (3) slave重做中继日志中的事件,将改变反映它自己的数据。 5 主从复制形式 一主一从 主主复制 一主多从---扩展系统读取的性能,因为读是在从库读取的; 多主一从---5.7开始支持 联级复制--- 6半同步复制 5.5版本集成到mysql,以插件的形式存在,需要单独安装 确保事务提交后binlog至少传输到一个从库 不保证从库应用完这个事务的binlog 性能有一定的降低,响应时间会更长 网络异常或从库宕机,卡主主库,直到超时或从库恢复 半同步原理图: 二、Mysql主从复制实例解析 首先了解数据库的主从复制部署注意要点 >>在主服务器上开启二进制日志,并设置唯一的服务器ID编号,设置后重启mysql生效。 >>在所有从服务器上设置唯一服务器ID编号,重启后生效。 >>在主服务器上创建让从服务器读取日志文件的账号,或者使用统一账户。 >>在进行数据复制前,需要记录主服务器上二进制日志的位置标记。 下面对整个主从复制过程详细解析 1 主服务器设置数据复制环境 在已安装或者使用过的数据库上创建数据库和数据表,演示如何对已经存在的数据进行数据同步备份 [root@master ~]# mysql -u root -p mysql> create database hr; mysql>usehr; mysql> create table employees( -> employee_id INT NOT NULL AUTO_INCREMENT, -> name char(20) NOT NULL, -> e_mailvarchar(50), -> PRIMARY KEY(employee_id)); mysql> INSERT INTO employees values ->(1,'TOM','tom@example.com'), ->(2,'Jerry','jerry@example.com'); mysql> exit 在主服务器上开启二进制日志并设置服务器编号,服务器唯一编号必须是1至232-1之间的整数,根据自己的实际情况进行设置。进行这些设置需要关闭MySQL数据库并编辑my.cnf或my.ini文件,并在[mysqld]设置段添加相应的配置选项。 [root@master ~]#vim /etc/my.cnf [mysqld] log-bin=Jacob-bin#启用二进制日志,并设置二进制日志文件前缀 server-id=25 #设置服务器编号 [root@master ~]#service mysqld restart [root@master ~]#service iptables stop 2 从服务器设置数据复制环境 在已安装或者使用过的数据库机器上设置,服务器ID必须设置为集群中唯一编号。对于从服务器而言,二进制日志功能无需开启,当然,根据业务需要也可以开启,比如主主复制中互为主从时需要都开启。 [root@slave ~]#vim /etc/my.cnf [mysqld] server-id=24 [root@slave ~]#service mysqld restart [root@slave ~]#service iptables stop 3 创建数据复制账号 主服务器上必须至少有一个账户供从服务器连接使用。这个账户必须具有REPLICATION SLAVE权限,你可以为不同的从服务器创建不同的账户与密码,也可以使用统一的账户与密码。使用CREATE USER语句创建用户,使用GRANT语句为账户赋权。 [root@master ~]# mysql -u root -p mysql> CREATE USER'slave'@'%'IDENTIFIED BY'123456'; mysql> GRANT REPLICATION SLAVE ON *.* TO'slave'@'%'; mysql> exit 4 读取主服务器二进制日志信息 在进行主从数据复制之前首先查看主服务器的二进制日志文件的基本信息,主服务器二进制文件名称及当前日志记录位置在对从服务器的设置中需要用到。 [root@master ~]# mysql -u root -p mysql> FLUSH TABLES WITH READ LOCK; mysql> SHOW MASTER STATUS; +--------------------------------+------------+---------------------+-------------------------+------- | File|Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +--------------------------------+------------+---------------------+-------------------------+------- | jacob-log.000001|1226||| +--------------------------------+------------+---------------------+--------------------------+------ mysql> UNLOCK TABLES; 5 对现有数据库备份并导入从服务器 如果在使用数据复制以前,数据库系统中已经存在数据资源,使用mysqldump工具对这些资料进行数据备份,在主服务器上使用该工具对数据备份后即可在从服务器上进行数据还原操作。当数据达到主从一致后,就可以使用数据复制功能进行自动从此同步操作。 [root@master ~]# mysqldump --all-databases --lock-all-tables > /tmp/dbdump.sql [root@master ~]# scp /tmp/dbdump.sql192.168.1.8:/tmp/ [root@slave ~]# mysql -u root -p < dbdump.sql 6配置从服务器连接主服务器进行数据复制 数据复制的关键操作是配置从服务器去连接主服务器进行数据复制,需要配置从服务器建立网络连接所有必要的信息。使用CHANGE MASTER TO语句即可完成该项工作,MASTER_HOST指定主服务器主机名或IP地址,MASTER_USER为主服务器上创建的拥有复制权限的账户名称,ASTER_PASSWORD为该账户的密码,MASTER_LOG_FILE指定主服务器二进制日志文件名称,MASTER_LOG_POS为主服务器二进制日志当前记录的位置。START SLAVE开启从服务器功能进行主从连接,SHOW SLAVE STATUS查看从服务器状态。 [root@slave ~]# mysql -u root -p mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.1.7', -> MASTER_USER='slave', -> MASTER_PASSWORD='123456', -> MASTER_LOG_FILE='jacob-log.000001', -> MASTER_LOG_POS=1226; mysql> START SLAVE; mysql> SHOW SLAVE STATUS\G; 7 数据同步测试验证 在主数据库上创建数据库及表,然后去从服务器查看是否同步。 [root@master ~]# mysql -u root -p mysql> create database test2; mysql>usetest2; mysql> create table t_table( ->name char(20), -> ageint, -> notevarchar(50)); mysql> INSERT INTO t_table values -> ('linda',23,'Beijing'), -> ('jerry',33,'shanghai'),; mysql> exit [root@slave ~]# mysql -u root -p mysql> select * from test2.t_table; mysql> exit 三、主从复制存在的问题及解决方法 mysql主从复制存在的问题: 主库宕机后,数据可能丢失 从库只有一个sql Thread,主库写压力大,复制很可能延时 解决方法: 半同步复制---解决数据丢失的问题 并行复制----解决从库复制延迟的问题 复制出错处理: 常见:1062(主键冲突),1032(记录不存在) 解决方法: 手动处理 跳过复制错误设置:set global sql_slave_skip_counter=1

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码