这两天正忙着研究postgresql,基本用法和其他数据库大同小异,先不自己讲解,今天主要想记录下postgresql的集群方案hot standby方
这两天正忙着研究postgresql,基本用法和其他数据库大同小异,先不自己讲解,今天主要想记录下postgresql的集群方案hot standby方式。
本人设置集群环境:
三台电脑:一台主数据库和两台从数据库
1. 安装Postgresql9
tar -zxvf postgresql-9.1.3.tar.gz #解压
cd postgresql-9.1.3
./configure --prefix /home/proxy_pg/pgsql #配置postgresql安装目录
这里需要安装基础的库(gcc、readline、zlib、) #可以不必理会,make时会提示
make #编译
make install #安装
2. 添加postgres账户
useradd postgres #添加账户
passwd postgres #之后输入密码
#之后进入postgres用户
su postgres
3. 初始化主数据库
mkdir data #在你想要存放数据的地方创建data文件夹
bin/initdb –D ../data/ #初始化数据库
修改data/postgresql.conf
port = 5433 #可以任意更改你想要的
wal_level = hot_standby
max_wal_senders = 30
修改data/pg_hba.conf
host replication all 0.0.0.0 0.0.0.0 trust
4. 启动主数据库
bin/pg_ctl start -D ../data/
5. 检测数据库是否启动成功
./psql -d postgres
psql (9.1.3)
Type "help" for help.
postgres=# #说明启动成功了
6. 基础备份
基本流程:在主数据库服务器上执行pg_start_backup(),复制data目录,在执行pg_stop_backup()。
./psql –d postgres
postgres=# select pg_start_backup(‘’);
#这姓这个方法后,所有请求在写日志之后不会再刷新到磁盘。直到执行pg_stop_backup()这个函数。
#下面需要拷贝一份data目录,并通过scp复制到子数据库中
cp –R data /data_bac
7. 创建从数据库(standby)
#通过scp方式拷贝data_bac目录到从数据库下(当然也可以通过其他方式)
scp -r data_bac/ postgres@192.168.30.199:/home/proxy_pg/pgsql/
#进入从数据库服务器,进入刚刚拷贝过来的data_bac目录下
cd data_bac
#修改postgres.conf
port = 5433 #改成你想的端口
hot_standby = on
#增加recovery.conf配置下连接的主数据库信息(ip、端口、用户)
standby_mode = 'on'
primary_conninfo = 'host=192.168.30.150 port=5433 user=postgres'
#删除pid文件
rm postmaster.pid
8. 启动从数据库
bin/pg_ctl start -D ../data_bac/
9. 停止主数据库基础备份
postgres=# select pg_stop_backup();
这里要注意的是:8和9步可以颠倒,因为先停止备份后,当从数据库启动会,会自动连接主数据库,,拉取服务器自基础备份后的事务日志,然后,对事物日志进行重演。
10. 测试
#为了方便查看数据库,我安装了pgAdminII
当在主数据库中创建一张表并插入三条数据后,观察从数据库:
#创建数据库lengzijian
./createdb lengzijian -p 5433 #主从库全部都会创建lengzijian数据库
#接下来可以创建表,并且插入字段。这里就不演示了。
11. 总结
这里只是做了初步的集群安装,postgresql的集群方案还有好多种,本人会演示所有方案,之后会对每一种方案进行检测,如果你有做好的实验或者方案,不放分享给我。

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.