Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Master: 192.168.1.139
- Slave: 192.168.1.140
- Install:
- yum localinstall http://yum.postgresql.org/9.4/redhat/rhel-6-x86_64/pgdg-centos94-9.4-1.noarch.rpm
- yum install postgresql94-server postgresql94-contrib postgresql94-devel repmgr94
- yum clean expire-cache
- All server:
- [root@postgre1 ~]# service postgresql-9.4 initdb
- Initializing database: [ OK ]
- [root@postgre1 ~]# service postgresql-9.4 start
- Starting postgresql-9.4 service: [ OK ]
- [root@postgre1 ~]# chkconfig postgresql-9.4 on
- passwd postgres
- New password:
- BAD PASSWORD: it is too simplistic/systematic
- BAD PASSWORD: is too simple
- Retype new password:
- passwd: all authentication tokens updated successfully.
- -bash-4.1$ psql
- psql (9.4.2)
- Type "help" for help.
- postgres=# \password
- Enter new password:
- Enter it again:
- postgres=# CREATE ROLE repmgr_usr LOGIN SUPERUSER;
- CREATE ROLE
- postgres=# CREATE DATABASE repmgr_db OWNER repmgr_usr;
- CREATE DATABASE
- postgres=# \q
- -bash-4.1$ pg_restore -U postgres -d repmgr_db /tmp/dvdrental.tar
- [ -f /etc/profile ] && source /etc/profile
- PGDATA=/var/lib/pgsql/9.4/data
- export PGDATA
- # If you want to customize your settings,
- # Use the file below. This is not overridden
- # by the RPMS.
- [ -f /var/lib/pgsql/.pgsql_profile ] && source /var/lib/pgsql/.pgsql_profile
- PG_BINDIR=/usr/pgsql-9.4/bin
- export PG_BINDIR
- export PATH=$PATH:$PG_BINDIR
- -bash-4.1$ echo $PGDATA
- /var/lib/pgsql/9.4/data
- -bash-4.1$ cd $PGDATA
- -bash-4.1$ ls
- base pg_ident.conf pg_replslot pg_subtrans postgresql.auto.conf
- global pg_log pg_serial pg_tblspc postgresql.conf
- pg_clog pg_logical pg_snapshots pg_twophase postmaster.opts
- pg_dynshmem pg_multixact pg_stat PG_VERSION postmaster.pid
- pg_hba.conf pg_notify pg_stat_tmp pg_xlog
- Master config:
- -bash-4.1$ vi pg_hba.conf
- # TYPE DATABASE USER ADDRESS METHOD
- # "local" is for Unix domain socket connections only
- local all all peer
- # IPv4 local connections:
- host all all 192.168.1.0/24 trust
- # IPv6 local connections:
- host all all ::1/128 ident
- # Allow replication connections from localhost, by a user with the
- # replication privilege.
- #local replication postgres peer
- host replication repmgr_usr 192.168.1.0/24 trust
- host repmgr_db repmgr_usr 192.168.1.0/24 trust
- #host replication postgres ::1/128 ident
- -bash-4.1$ vi postgresql.conf
- listen_addresses = 'localhost,192.168.1.133'
- wal_level = hot_standby
- archive_mode = on
- archive_command = 'cd .'
- max_wal_senders = 3
- hot_standby = on
- chown -hR postgres /etc/repmgr/
- [root@postgre1 ~]# service postgresql-9.4 restart
- Stopping postgresql-9.4 service: [ OK ]
- Starting postgresql-9.4 service: [ OK ]
- Slave config:
- [root@postgre2 ~]# service postgresql-9.4 stop
- Stopping postgresql-9.4 service: [ OK ]
- [root@postgre2 ~]# su - postgres
- -bash-4.1$ echo $PGDATA
- /var/lib/pgsql/9.4/data
- -bash-4.1$ cd $PGDATA
- -bash-4.1$ ls
- base pg_ident.conf pg_replslot pg_subtrans postgresql.auto.conf
- global pg_log pg_serial pg_tblspc postgresql.conf
- pg_clog pg_logical pg_snapshots pg_twophase postmaster.opts
- pg_dynshmem pg_multixact pg_stat PG_VERSION
- pg_hba.conf pg_notify pg_stat_tmp pg_xlog
- -bash-4.1$ vi pg_hba.conf
- # TYPE DATABASE USER ADDRESS METHOD
- # "local" is for Unix domain socket connections only
- local all all peer
- # IPv4 local connections:
- host all all 192.168.1.0/24 md5
- # IPv6 local connections:
- host all all ::1/128 ident
- # Allow replication connections from localhost, by a user with the
- # replication privilege.
- #local replication postgres peer
- host replication rep 192.168.1.133 md5
- #host replication postgres ::1/128 ident
- -bash-4.1$ vi /var/lib/pgsql/9.4/data/postgresql.conf
- listen_addresses = 'localhost,192.168.1.134'
- wal_level = hot_standby
- archive_mode = on
- archive_command = 'cd .'
- max_wal_senders = 3
- hot_standby = on
- Replication :
- Tao ssh key
- -bash-4.1$ ssh-keygen -t rsa
- Generating public/private rsa key pair.
- Enter file in which to save the key (/var/lib/pgsql/.ssh/id_rsa):
- Created directory '/var/lib/pgsql/.ssh'.
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in /var/lib/pgsql/.ssh/id_rsa.
- Your public key has been saved in /var/lib/pgsql/.ssh/id_rsa.pub.
- The key fingerprint is:
- 62:10:61:bc:e5:7a:30:da:19:89:90:03:5c:2c:e6:d7 postgres@postgre1.test
- The key's randomart image is:
- +--[ RSA 2048]----+
- |+.oo+. |
- |++ o... |
- |oo..o= |
- | ...*E. |
- | .o *o S |
- | . +... |
- | . |
- | |
- | |
- +-----------------+
- -bash-4.1$ ssh-copy-id postgres@192.168.1.134
- The authenticity of host '192.168.1.134 (192.168.1.134)' can't be established.
- RSA key fingerprint is b9:f4:80:5b:d8:47:19:94:3e:90:03:f2:d6:b3:e3:47.
- Are you sure you want to continue connecting (yes/no)? yes
- Warning: Permanently added '192.168.1.134' (RSA) to the list of known hosts.
- postgres@192.168.1.134's password:
- Now try logging into the machine, with "ssh 'postgres@192.168.1.134'", and check in:
- .ssh/authorized_keys
- to make sure we haven't added extra keys that you weren't expecting.
- Master:
- [root@postgre1 ~]# su - postgres
- -bash-4.1$ psql -c "select pg_start_backup('initial_backup');"
- pg_start_backup
- -----------------
- 0/2000028
- (1 row)
- rsync -cva --inplace --exclude=*pg_xlog* $PGDATA/ 192.168.1.134:$PGDATA/
- ...
- sent 135726 bytes received 4444 bytes 13349.52 bytes/sec
- total size is 21178953 speedup is 151.09
- -bash-4.1$ psql -c "select pg_stop_backup();"
- NOTICE: pg_stop_backup complete, all required WAL segments have been archived
- pg_stop_backup
- ----------------
- 0/2000128
- (1 row)
- Slave:
- -bash-4.1$ vi /var/lib/pgsql/9.4/data/recovery.conf
- standby_mode = 'on'
- primary_conninfo = 'host=192.168.1.133 port=5432 user=rep password=rep'
- trigger_file = '/tmp/postgresql.trigger.5432'
- [root@postgre2 ~]# touch /tmp/postgresql.trigger.5432
- [root@postgre2 ~]# chmod -R 777 /tmp/postgresql.trigger.5432
- [root@postgre2 ~]# service postgresql-9.4 start
- Starting postgresql-9.4 service: [ OK ]
- vi /var/lib/pgsql/9.4/data/pg_log/postgresql-Fri.log
- < 2015-05-22 16:59:04.809 ICT >LOG: entering standby mode
- < 2015-05-22 16:59:04.814 ICT >LOG: started streaming WAL from primary at 0/4000000 on timeline 1
- < 2015-05-22 16:59:04.861 ICT >LOG: redo starts at 0/4000028
- < 2015-05-22 16:59:04.863 ICT >LOG: consistent recovery state reached at 0/4000128
- < 2015-05-22 16:59:04.863 ICT >LOG: database system is ready to accept read only connections
- Check:
- Master:
- [root@postgre1 ~]# ps ax |grep sender
- 1622 ? Ss 0:00 postgres: wal sender process rep 192.168.1.134(52249) streaming 0/5000138
- 1659 pts/0 S+ 0:00 grep sender
- Slave:
- [root@postgre2 ~]# ps ax |grep receiver
- 2028 ? Ss 0:00 postgres: wal receiver process streaming 0/5000138
- 2064 pts/0 S+ 0:00 grep receiver
- Tạo mới db trên Master:
- -bash-4.1$ psql
- psql (9.4.1)
- Type "help" for help.
- ALTER USER Postgres WITH PASSWORD '<newpassword>';
- psql (9.4.1)
- Type "help" for help.
- postgres=# \l
- List of databases
- Name | Owner | Encoding | Collate | Ctype | Access privileg
- es
- -----------+----------+----------+-------------+-------------+------------------
- -----
- dvdrental | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
- template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
- +
- | | | | | postgres=CTc/post
- gres
- template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
- +
- | | | | | postgres=CTc/post
- gres
- testdb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
- (5 rows)
- Slave list:
- [root@postgre2 ~]# su - postgres
- -bash-4.1$ psql
- psql (9.4.1)
- Type "help" for help.
- postgres=# \l
- List of databases
- Name | Owner | Encoding | Collate | Ctype | Access privileg
- es
- -----------+----------+----------+-------------+-------------+------------------
- -----
- dvdrental | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
- template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
- +
- | | | | | postgres=CTc/post
- gres
- template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
- +
- | | | | | postgres=CTc/post
- gres
- testdb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
- (5 rows)
- Master check:
- -bash-4.1$ psql
- psql (9.4.1)
- Type "help" for help.
- postgres=# \c testdb
- You are now connected to database "testdb" as user "postgres".
- testdb=# CREATE TABLE rep_test (test varchar(40));
- CREATE TABLE
- testdb=# INSERT INTO rep_test VALUES ('data one');
- INSERT 0 1
- testdb=# INSERT INTO rep_test VALUES ('some more words');
- INSERT 0 1
- testdb=# INSERT INTO rep_test VALUES ('lalala');
- INSERT 0 1
- testdb=# INSERT INTO rep_test VALUES ('hello there');
- INSERT 0 1
- testdb=# INSERT INTO rep_test VALUES ('blahblah');
- INSERT 0 1
- testdb=# \d
- List of relations
- Schema | Name | Type | Owner
- --------+----------+-------+----------
- public | rep_test | table | postgres
- (1 row)
- Slave:
- -bash-4.1$ psql
- psql (9.4.1)
- Type "help" for help.
- postgres=# \c testdb
- You are now connected to database "testdb" as user "postgres".
- testdb=# select * from rep_test;
- test
- -----------------
- data one
- some more words
- lalala
- hello there
- blahblah
- (5 rows)
- postgres=# select * from pg_stat_replication;
- pid | usesysid | usename | application_name | client_addr | client_hostname
- | client_port | backend_start | backend_xmin | state | sent_
- location | write_location | flush_location | replay_location | sync_priority | s
- ync_state
- ------+----------+---------+------------------+---------------+-----------------
- +-------------+-------------------------------+--------------+-----------+------
- ---------+----------------+----------------+-----------------+---------------+--
- ----------
- 1905 | 16384 | rep | walreceiver | 192.168.1.134 |
- | 52250 | 2015-05-22 17:21:48.802372+07 | | streaming | 0/601
- 31E8 | 0/60131E8 | 0/60131E8 | 0/60131E8 | 0 | a
- sync
- (1 row)
- Master = false, slave = true
- postgres=# select pg_is_in_recovery();
- pg_is_in_recovery
- -------------------
- f
- (1 row)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement