开始部署之前,建议先按照Ubuntu切换到国内镜像源操作一下,能省下大把宝贵时间!
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - sudo apt-get update sudo apt-get install postgresql-9.6 # 自行选择合适版本 ## 更多参考 https://www.postgresql.org/download/linux/ubuntu/
sudo vim /etc/postgresql/9.6/main/postgresql.conf listen_addresses = '*' max_connections = 1000 logging_collector = on ## 更多参考 https://www.postgresql.org/docs/current/static/runtime-config.html sudo vim /etc/postgresql/9.6/main/pg_hba.conf host all all 0.0.0.0/0 md5 ## 更多参考 https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html sudo service postgresql restart
sudo -u postgres psql # ALTER USER postgres WITH PASSWORD 'postgres'; # \q exit
搭建集群不是必须的,但是如果是生产环境,建议还是操作一下,毕竟数据重于泰山!
下面以简单的一主一从抛砖引玉一下
主机 | ip |
---|---|
Master 节点 | 10.10.10.10 |
Slave 节点 | 10.10.10.9 |
Master
节点和Slave
节点分别按照第一步安装并配置完成postgres
后,开始搭建集群。
sudo vi /etc/postgresql/9.6/main/postgresql.conf listen_addresses = '*' wal_level = hot_standby archive_mode = on archive_command = 'test ! -f /var/lib/postgresql/9.6/archive/%f && cp %p /var/lib/postgresql/9.6/archive/%f' max_wal_senders = 16 wal_keep_segments = 100 hot_standby = on logging_collector = on ## 更多参考 https://www.postgresql.org/docs/current/static/runtime-config.html sudo vi /etc/postgresql/9.6/main/pg_hba.conf host all all 10.0.0.0/8 md5 host replication repuser 10.0.0.0/8 md5 ## 更多参考 https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html sudo -upostgres mkdir /var/lib/postgresql/9.6/archive sudo chmod 0700 /var/lib/postgresql/9.6/archive sudo service postgresql restart
repuser
slave
节点就是通过repuser
进行复制操作的。
sudo -upostgres createuser --replication repuser sudo -upostgres psql postgres=# \password repuser <password> ## 更多参考 https://www.postgresql.org/docs/current/static/user-manag.html
sudo service postgresql stop
sudo -upostgres vi /var/lib/postgresql/.pgpass 10.10.10.10:5432:*:repuser:<password> 127.0.0.1:5432:*:repuser:<password> sudo chmod 0600 /var/lib/postgresql/.pgpass sudo mv /var/lib/postgresql/9.6/main /var/lib/postgresql/9.6/main.bak sudo -upostgres pg_basebackup -D /var/lib/postgresql/9.6/main -F p -X stream -v -R -h 10.10.10.10 -p 5432 -U repuser
sudo vi /var/lib/postgresql/9.6/main/recovery.conf standby_mode = 'on' primary_conninfo = 'user=repuser host=10.10.10.10 port=5432' trigger_file = 'failover.now' ## 更多参考 https://www.postgresql.org/docs/current/static/recovery-config.html sudo vi /etc/postgresql/9.6/main/postgresql.conf hot_standby = on
sudo service postgresql start sudo service postgresql status ... Active: active (exited) sudo -upostgres psql psql (9.6.12) ...
在master
节点进行增删改操作,对照看slave
节点是否能够从master
节点复制操作
sudo service postgresql start sudo service postgresql status sudo service postgresql restart
更多常用命令,可以查看我的另一篇文章:阿晨的运维笔记 | Postgres常用命令
后面我还会讲一下Docker
和Kubernetes
部署PostgreSQL
的方法,关注我,第一时间可以收到推送哦!
我是阿晨,在技术的道路上我们一起砥砺前行!