Rootop 服务器运维与web架构

2017-11-23
发表者 Venus
centos7 yum安装postgresql已关闭评论

centos7 yum安装postgresql

[root@docker-remote1 ~]# yum install postgresql-server postgresql -y
[root@docker-remote1 ~]# service postgresql initdb # 初始化
[root@docker-remote1 ~]# service postgresql start
[root@docker-remote1 ~]# chkconfig postgresql on

# 默认不能用root进入pg的命令行,安装会创建一个用户 postgres
su 切换为 postgres 用户
执行 psql 可以进入pg的命令行

# 查看数据库
postgres=# \l     #通过\?可以看支持命令

开启远程登陆:

[root@docker-remote2 data]# cat /var/lib/pgsql/data/pg_hba.conf | grep -v "#" | grep -v "^$"
local all all peer
host all all 127.0.0.1/32 ident
host all all ::1/128 ident
# 所有库,所有用户,从所有地址登陆
host all all 0.0.0.0/0 md5

修改监听地址等

[root@docker-remote2 data]# cat postgresql.conf | grep -v -E "#|^$"
# 监听所有地址
listen_addresses = '*' # 可以放开远程连接
log_timezone = 'PRC'
datestyle = 'iso, mdy'
timezone = 'PRC'
default_text_search_config = 'pg_catalog.english'

一些搜索来的操作命令:

# 切换到 postgres 用户
[root@docker-remote2 ~]# su postgres

# 创建用户aaa ,密码 aaa
postgres=# create user aaa with password ‘aaa’;
CREATE ROLE

# 创建数据库 venus
postgres=# create database venus;
CREATE DATABASE

# 把 venus 库赋给用户 aaa 权限
postgres=# grant all privileges on database venus to aaa;

# 把venus2库属主赋给aaa
postgres=# create database venus2 owner aaa;
CREATE DATABASE

2017-11-23
发表者 Venus
postgresql-9.6 同步复制已关闭评论

postgresql-9.6 同步复制

参考:http://blog.csdn.net/baiyinqiqi/article/details/47951687

主配置:

同步模式基于异步模式添加一个参数即可。

异步配置文档:https://www.rootop.org/pages/3928.html

先说一个其中比较重要的参数synchronous_commit,同步流复制模式需要打开这个参数。
synchronous_commit = on #默认是注释的,此功能默认是开启的。从写完以后告诉主完成才算插入成功

这个参数对性能影响还是比较大的(毕竟等于写2台机器),根据实际情况可考虑关闭,在关键数据更新时在事务中将其暂时性打开,保证关键数据不会因意外停机而丢失。

使用同步流复制模式,那么master会等待同步slave返回事务状态后,才会完成当前事务。所以如果slave停掉,那么master的事务会一直等待下去。
追加一行参数:
[root@localhost ~]# vi /var/lib/pgsql/9.6/data/postgresql.conf
synchronous_standby_names = ‘standby1’ # 这个值用来控制同步的从机应用名,这个值从从机查

从配置:
[root@localhost ~]# cat /var/lib/pgsql/9.6/data/recovery.conf
standby_mode = ‘on’
primary_conninfo = ‘user=rep password=rep host=192.168.10.50 port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres application_name=standby1

在 primary_conninfo 配置项段加了一段 application_name=standby1
standby1 需要追加到主服务器的synchronous_standby_names配置中。

重启主从。
在主上写数据测试->同步到从
停掉从机,再在主写数据就会卡主。直到从机恢复。

2017-11-23
发表者 Venus
postgresql-9.6 异步复制已关闭评论

postgresql-9.6 异步复制

参考:http://blog.csdn.net/wzyzzu/article/details/53331206

官网:https://www.postgresql.org/download/linux/redhat/

环境:
master   192.168.10.50    centos7
slave       192.168.10.51    centos7

主:
安装,根据官网文档安装
[root@localhost ~]# yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-redhat96-9.6-3.noarch.rpm
[root@localhost ~]# yum install -y postgresql96-server postgresql96
[root@localhost ~]# /usr/pgsql-9.6/bin/postgresql96-setup initdb  #初始化数据库
[root@localhost ~]# systemctl enable postgresql-9.6
[root@localhost ~]# systemctl start postgresql-9.6
[root@localhost ~]# netstat -tnlp
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 557/sshd
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 2272/postmaster
tcp6 0 0 :::22 :::* LISTEN 557/sshd
tcp6 0 0 ::1:5432 :::* LISTEN 2272/postmaster

# 切换到 postgres 用户,创建复制账号。这里给了超级权限
[root@localhost ~]# su postgres
bash-4.2$ psql

# 用户名 rep 密码 rep
postgres=# create user rep superuser password ‘rep’;
CREATE ROLE

# 查看用户
postgres=# \du
List of roles
Role name | Attributes | Member of
———–+————————————————————+———–
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
rep | Superuser | {}

# 退出
postgres=# \q
bash-4.2$ exit

# 修改 postgresql.conf 配置文件
[root@localhost ~]# vi /var/lib/pgsql/9.6/data/postgresql.conf
# 监听地址,否则无法远程连接
listen_addresses = ‘*’

# 这个是设置主为wal的主机
wal_level = hot_standby

# 设置最多有几个流复制连接,有几个从就设置几个
max_wal_senders = 1

# 设置一个尽量大的值,以防止主库生成WAL日志太快,日志还没有来得及传送到standby就被覆盖 一个WAL日志文件是16M
wal_keep_segments = 64

# 设置流复制主机发送数据的超时时间
wal_sender_timeout = 120s

# 从库的max_connections必须要大于主库的
max_connections = 100

保存,退出。

修改 pg_hba.conf 配置文件,追加一行
[root@localhost ~]# vi /var/lib/pgsql/9.6/data/pg_hba.conf
host replication rep 192.168.10.51/32 md5
允许rep用户从ip 192.168.10.51 来获取wal日志

重启服务
[root@localhost ~]# systemctl restart postgresql-9.6

从库:
安装,注意没有初始化这步
[root@localhost ~]# yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-redhat96-9.6-3.noarch.rpm
[root@localhost ~]# yum install -y postgresql96-server postgresql96
[root@localhost ~]# systemctl enable postgresql-9.6

用pg_basebackup命令行工具在从库上生成基础备份,并生成 recovery.conf 配置文件。可以省去手动配置。

[root@localhost ~]# pg_basebackup -F p -R --progress -D /var/lib/pgsql/9.6/data -h 192.168.10.50 -p 5432 -U rep --password
Password: 输入密码
22826/22826 kB (100%), 1/1 tablespace
NOTICE: WAL archiving is not enabled; you must ensure that all required WAL segments are copied through other means to complete the backup

参数说明(可以通过pg_basebackup –help进行查看)
-h指定连接的数据库的主机名或IP地址
-U指定连接的用户名,此处是主库创建的rep用户
-F指定了输出的格式,支持p(原样输出)或者t(tar格式输出)
-x表示备份开始后,启动另一个流复制连接从主库接收WAL日志。
-P表示允许在备份过程中实时的打印备份的进度。
-R表示会在备份结束后自动生成recovery.conf文件,避免手动创建。
-D指定把备份写到哪个目录,注意做基础备份之前从库的数据目录 /var/lib/pgsql/9.6/data 下需要是空的,或者手动清空。

这样在 /var/lib/pgsql/9.6/data 目录下,可以省去配置 recovery.conf 步骤

编辑从库配置文件
[root@localhost ~]# vi /var/lib/pgsql/9.6/data/postgresql.conf

# 说明这台机器不仅用于数据归档,也用于数据查询
hot_standby = on

# 最大连接数,大于主
max_connections = 1000

# 数据流备份的最大延迟时间
max_standby_streaming_delay = 30s

# 多久向主报告一次从的状态,当然从每次数据复制都会向主报告状态,这里只是设置最长的间隔时间
wal_receiver_status_interval = 10s

# 如果有错误的数据复制,是否向主进行反馈
hot_standby_feedback = off

保存,退出。

修改属主,否则启动服务会失败
[root@localhost ~]# chown -R postgres:postgres /var/lib/pgsql/9.6/data

启动服务
[root@localhost ~]# systemctl start postgresql-9.6

上查看进程,确保有 wal sender process 字样
[root@localhost pg_xlog]# ps aux | grep postgre
postgres 4725 0.0 0.8 357580 15280 ? Ss 12:49 0:00 /usr/pgsql-9.6/bin/postmaster -D /var/lib/pgsql/9.6/data/
postgres 4727 0.0 0.0 210460 1560 ? Ss 12:49 0:00 postgres: logger process
postgres 4729 0.0 0.1 357580 3220 ? Ss 12:49 0:00 postgres: checkpointer process
postgres 4730 0.0 0.1 357580 2964 ? Ss 12:49 0:00 postgres: writer process
postgres 4731 0.0 0.3 357580 6128 ? Ss 12:49 0:00 postgres: wal writer process
postgres 4732 0.0 0.1 358004 2816 ? Ss 12:49 0:00 postgres: autovacuum launcher process
postgres 4733 0.0 0.1 212580 1916 ? Ss 12:49 0:00 postgres: stats collector process
postgres 4963 0.0 0.1 358376 3312 ? Ss 13:07 0:00 postgres: wal sender process rep 192.168.10.51(35022) streaming 0/3000220

上查看进程,有 wal receive 字样
[root@localhost ~]# ps aux | grep postgre
postgres 3195 0.0 1.4 375892 27360 ? Ss 13:07 0:00 /usr/pgsql-9.6/bin/postmaster -D /var/lib/pgsql/9.6/data/
postgres 3197 0.0 0.0 210460 1552 ? Ss 13:07 0:00 postgres: logger process
postgres 3198 0.0 0.1 375956 2400 ? Ss 13:07 0:00 postgres: startup process recovering 000000010000000000000003
postgres 3199 0.2 0.1 383044 3600 ? Ss 13:07 0:00 postgres: wal receiver process streaming 0/3000220
postgres 3200 0.0 0.1 375892 1916 ? Ss 13:07 0:00 postgres: checkpointer process
postgres 3201 0.0 0.1 375892 1924 ? Ss 13:07 0:00 postgres: writer process

这样主从复制就可以了。

登陆两台pg数据库,创建表,插入数据,都会同步到从库。

注意:

pg需要编辑 /var/lib/pgsql/9.6/data/pg_hba.conf 加一条 host all rep 0.0.0.0/0 md5 才能用rep远程登陆pg数据库。否则用户是无法远程登陆的。

2017-11-17
发表者 Venus
centos7下rabbitmq-3.6.14安装配置已关闭评论

centos7下rabbitmq-3.6.14安装配置

官网: http://www.rabbitmq.com/install-rpm.html

目前最新稳定版:rabbitmq-server-3.6.14
下载地址: https://dl.bintray.com/rabbitmq/rabbitmq-server-rpm/rabbitmq-server-3.6.14-1.el7.noarch.rpm

系统版本:centos7.3:
[root@localhost ~]# wget -c https://dl.bintray.com/rabbitmq/rabbitmq-server-rpm/rabbitmq-server-3.6.14-1.el7.noarch.rpm
[root@localhost ~]# rpm -ivh rabbitmq-server-3.6.14-1.el7.noarch.rpm
warning: rabbitmq-server-3.6.14-1.el7.noarch.rpm: Header V4 RSA/SHA512 Signature, key ID 6026dfca: NOKEY
error: Failed dependencies:
erlang >= R16B-03 is needed by rabbitmq-server-3.6.14-1.el7.noarch
socat is needed by rabbitmq-server-3.6.14-1.el7.noarch

安装epel源,否则可能没有erlang的包
[root@localhost ~]# yum install -y epel-*
[root@localhost ~]# yum install -y erlang socat
[root@localhost ~]# rpm -ivh rabbitmq-server-3.6.14-1.el7.noarch.rpm
安装完成。

启动服务:
[root@localhost ~]# rabbitmq-server &  # 停止用 rabbitmqctl stop
[root@localhost ~]# netstat -tnlp
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:4369 0.0.0.0:* LISTEN 14457/epmd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 555/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 747/master
tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 14893/beam.smp
tcp6 0 0 :::4369 :::* LISTEN 14457/epmd
tcp6 0 0 :::22 :::* LISTEN 555/sshd
tcp6 0 0 ::1:25 :::* LISTEN 747/master
tcp6 0 0 :::5672 :::* LISTEN 14893/beam.smp
可以看到 5672 端口启动了,mq的端口。

# 开启rabbitmq的web管理界面
[root@localhost ~]# rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
amqp_client
cowlib
cowboy
rabbitmq_web_dispatch
rabbitmq_management_agent
rabbitmq_management

Applying plugin configuration to rabbit@localhost… started 6 plugins.
[root@localhost ~]# netstat -tnlp
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:4369 0.0.0.0:* LISTEN 14457/epmd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 555/sshd
tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 14893/beam.smp
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 747/master
tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 14893/beam.smp
tcp6 0 0 :::4369 :::* LISTEN 14457/epmd
tcp6 0 0 :::22 :::* LISTEN 555/sshd
tcp6 0 0 ::1:25 :::* LISTEN 747/master
tcp6 0 0 :::5672 :::* LISTEN 14893/beam.smp
可以看到15672端口启动了(web界面)

在web登陆的时候,提示 User can only log in via localhost 。如图:

rabbitmq从3.3.0开始禁止使用guest权限通过除localhost外的访问。

# 允许远程登陆,默认没有这个文件
[root@localhost ~]# vi /etc/rabbitmq/rabbitmq.config

[{rabbit, [{loopback_users, []}]}].

注意后面有个点,重启服务。

2017-11-17
发表者 Venus
配置 haproxy 4层代理已关闭评论

配置 haproxy 4层代理

官网下载 http://www.haproxy.org/
目前最新稳定版: http://www.haproxy.org/download/1.7/src/haproxy-1.7.9.tar.gz

解压安装:

[root@localhost ~]# yum install -y make gcc gcc-c++
[root@localhost ~]# tar zxvf haproxy-1.7.9
[root@localhost ~]# cd haproxy-1.7.9
[root@localhost haproxy-1.7.9]# make TARGET=linux2628 PREFIX=/usr/local/haproxy
[root@localhost haproxy-1.7.9]# make TARGET=linux2628 PREFIX=/usr/local/haproxy install

linux2628 是for Linux内核大于 2.6.28, 3.x的,可以通过readme查看。

配置文件(百度了个现成的):

[root@localhost ~]# mkdir /usr/local/haproxy/conf
[root@localhost ~]# vi /usr/local/haproxy/conf/haproxy.cfg
###########全局配置#########
global
chroot /usr/local/haproxy
daemon
nbproc 1
group nobody
user nobody
pidfile /usr/local/haproxy/haproxy.pid
ulimit-n 65536
#spread-checks 5m
#stats timeout 5m
#stats maxconn 100

########默认配置############
defaults
mode tcp #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
retries 3 #两次连接失败就认为是服务器不可用,也可以通过后面设置
option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
maxconn 65535 #默认的最大连接数
timeout connect 5000ms #连接超时
timeout client 30000ms #客户端超时
timeout server 30000ms #服务器超时
#timeout check 2000 #心跳检测超时
log 127.0.0.1 local0 err #[err warning info debug]

########mysql代理配置#################
listen mysql
bind 0.0.0.0:3306
mode tcp # 4层代理
balance roundrobin
server s1 172.31.4.48:3306 weight 1 maxconn 60000 check inter 3s
server s2 172.31.4.49:3306 weight 1 maxconn 60000 check inter 3s
server s3 172.31.4.50:3306 weight 1 maxconn 60000 check inter 3s

########rabbitmq代理配置##############
listen rabbitmq
bind 0.0.0.0:5672
mode tcp # 4层代理
balance roundrobin
server s1 172.31.4.48:5672 weight 1 maxconn 60000 check inter 3s
server s2 172.31.4.49:5672 weight 1 maxconn 60000 check inter 3s
server s3 172.31.4.50:5672 weight 1 maxconn 60000 check inter 3s


########统计页面配置########
listen admin
bind 0.0.0.0:8888 #监听端口
mode http #http代理
option httplog #采用http日志格式
#log 127.0.0.1 local0 err
maxconn 10
stats refresh 3s #统计页面自动刷新时间
stats uri /stats #统计页面url
stats realm RootopHaproxy #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
stats hide-version #隐藏统计页面上HAProxy的版本信息

启动服务:

[root@localhost ~]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg

用浏览器访问 http://ip:8888/stats 就可以看到类似下面的监控图。