Миграция Postgres с 8.4 на 9.0
Решились потренироваться делать репликацию Master-Slave, которая реализована в версии 9.0
Значит так-с…
1. Обновляем Portage
emerge --sync |
2. Ставим новую БД
LINGUAS="ru" ACCEPT_KEYWORDS="~amd64" emerge -av /usr/portage/dev-db/postgresql-server/postgresql-server-9.0.1.ebuild |
3. Конфигурим
emerge --config =dev-db/postgresql-server-9.0.1 |
4. правим конфиг и запускаем на порту 6543, не выключая старый постгресс
5. производим миграцию БД
pg_dumpall -p 5432 | psql -d postgres -p 6543 |
6. стартуем новую БД на порту 5432 предварительно выключив старую
После чего устанавливаем Postgres на втором хосте, который будет использоваться как Slave
LINGUAS="ru" ACCEPT_KEYWORDS="~amd64" emerge -av /usr/portage/dev-db/postgresql-server/postgresql-server-9.0.1.ebuild |
Вносим изменения в Master, чтобы слэйв мог к нему подключится:
$ $EDITOR postgresql.conf listen_addresses = '10.0.xx.yy' $ $EDITOR pg_hba.conf # The standby server must have superuser access privileges. host replication postgres 10.0.xx.zz/22 trust |
Теперь добавляем опции в конфиг Мастера для самой репликации
$ $EDITOR postgresql.conf # To enable read-only queries on a standby server, wal_level must be set to # "hot_standby". But you can choose "archive" if you never connect to the # server in standby mode. wal_level = hot_standby # Set the maximum number of concurrent connections from the standby servers. max_wal_senders = 5 # To prevent the primary server from removing the WAL segments required for # the standby server before shipping them, set the minimum number of segments # retained in the pg_xlog directory. At least wal_keep_segments should be # larger than the number of segments generated between the beginning of # online-backup and the startup of streaming replication. If you enable WAL # archiving to an archive directory accessible from the standby, this may # not be necessary. wal_keep_segments = 32 # Enable WAL archiving on the primary to an archive directory accessible from # the standby. If wal_keep_segments is a high enough number to retain the WAL # segments required for the standby server, this may not be necessary. archive_mode = on archive_command = 'cp %p /var/archive/%f' |
Последняя команда необходима для архивирования данных которые были реплицированы.
Запускаем мастер
/etc/init.d/postgresql-9.0 start |
и начинаем сливать данные на slave
$ psql -c "SELECT pg_start_backup('label', true)" $ rsync -a /var/lib/postgresql/9.0/data/ 10.0.xx.zz:/var/lib/postgresql/9.0/data/ --exclude postmaster.pid $ psql -c "SELECT pg_stop_backup()" |
Вносим правки в конфиги слэйва
$ $EDITOR postgresql.conf
hot_standby = on |
Создаем файл для востановления
$ $EDITOR recovery.conf # Specifies whether to start the server as a standby. In streaming replication, # this parameter must to be set to on. standby_mode = 'on' # Specifies a connection string which is used for the standby server to connect # with the primary. primary_conninfo = 'host=10.0.xx.yy port=5432 user=postgres' # Specifies a trigger file whose presence should cause streaming replication to # end (i.e., failover). trigger_file = '/var/lib/postgres/trigger' # Specifies a command to load archive segments from the WAL archive. If # wal_keep_segments is a high enough number to retain the WAL segments # required for the standby server, this may not be necessary. But # a large workload can cause segments to be recycled before the standby # is fully synchronized, requiring you to start again from a new base backup. restore_command = 'cp /var/archive/%f "%p"' |
Ну и собственно запускаем Slave
/etc/init.d/postgresql-9.0 start |
Для проверки репликации использовал простой “скрипт”
cat test.sh #!/bin/bash # inserts to master psql <db> <user> < users_logs.sql #select from master psql -U <user> -h <master_IP> <db> -c "select count(*) from users_logs;" #select from slave psql -U <user> -h <slave_IP> <db> -c "select count(*) from users_logs;" |
В файле users_logs.sql находятся инсерты в таблицу с 8 полями, всего 7484 записей
результат:
Master count ------- 7484 (1 row) Slave count ------- 7468 (1 row) |
тоесть на слэйв данные приходят с небольшим опозданием.