PostgreSQL 基於Docker的多執行個體安裝

來源:互聯網
上載者:User

標籤:

參考:https://hub.docker.com/_/postgres/

https://docs.docker.com/engine/installation/debian/

 

利用Docker進行多執行個體的安裝,相對於源碼安裝更簡單,更方便,隔離性也更好 。

安裝Docker

  1. 更新安裝源
sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609Dsudo touch /etc/apt/sources.list.d/docker.listsudo vim /etc/apt/sources.list.d/docker.list

根據系統寫入:

deb https://apt.dockerproject.org/repo debian-jessie main

  1. 安裝
sudo apt-get updatesudo apt-get install docker-engine
  1. 開啟服務
sudo service docker start
  1. 驗證
sudo docker run hello-world該命令會下載一個測試鏡像,並在容器內運行,列印出相關資訊,當看到類似如下資訊是表示運行正常。Hello from Docker.This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the    executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.

 

安裝PostgreSQL

  1. 下載最新官方PostgeSQL鏡像
sudo docker pull postgres
  1. 啟動 兩個鏡像,分別映射至宿主機5454及5455連接埠
sudo docker run --name cluster1 -p 5454:5432 -e POSTGRES_PASSWORD=postgres -d postgressudo docker run --name cluster2 -p 5455:5432 -e POSTGRES_PASSWORD=postgres -d postgres
  1. 查看運行中的容器及相關資訊
[email protected]:~$ sudo docker psCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES3bf3b533ba05        postgres            "/docker-entrypoint.s"   26 seconds ago      Up 24 seconds       0.0.0.0:5455->5432/tcp   cluster229b9ffeb6ac8        postgres            "/docker-entrypoint.s"   58 seconds ago      Up 56 seconds       0.0.0.0:5454->5432/tcp   cluster1[email protected]:~$ sudo docker inspect 3bf3b533ba05 |grep IPAddress        "SecondaryIPAddresses": null,        "IPAddress": "172.17.0.3",                "IPAddress": "172.17.0.3",[email protected]:~$ sudo docker inspect 29b9ffeb6ac8 |grep IPAddress        "SecondaryIPAddresses": null,        "IPAddress": "172.17.0.2",                "IPAddress": "172.17.0.2",

 

  1. 根據上一步中的IPAddress,進行串連測試
[email protected]:~$ psql -h172.17.0.2 -Upostgres -p5432Password for user postgres:psql (9.5.0)Type "help" for help. postgres=# \l                                 List of databases   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges  -----------+----------+----------+------------+------------+----------------------- postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +           |          |          |            |            | postgres=CTc/postgres template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +           |          |          |            |            | postgres=CTc/postgres(3 rows)[email protected]:~$ psql -h172.17.0.3 -Upostgres -p5432Password for user postgres:psql (9.5.0)Type "help" for help. postgres=# \l                                 List of databases   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges  -----------+----------+----------+------------+------------+----------------------- postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +           |          |          |            |            | postgres=CTc/postgres template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +           |          |          |            |            | postgres=CTc/postgres(3 rows)通過宿主機地址和連接埠串連資料庫[email protected]:~$ psql -h192.168.10.131 -p5454 -UpostgresPassword for user postgres:psql (9.5.0)Type "help" for help. postgres=# \l                                 List of databases   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges  -----------+----------+----------+------------+------------+----------------------- postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +           |          |          |            |            | postgres=CTc/postgres template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +           |          |          |            |            | postgres=CTc/postgres(3 rows) postgres=# \q[email protected]:~$ psql -h192.168.10.131 -p5455 -UpostgresPassword for user postgres:psql (9.5.0)Type "help" for help. postgres=# \l                                 List of databases   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges  -----------+----------+----------+------------+------------+----------------------- postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +           |          |          |            |            | postgres=CTc/postgres template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +           |          |          |            |            | postgres=CTc/postgres(3 rows)

查看容器內資料檔案、設定檔、網路授權檔案設定

sudo docker exec -it 3bf3b533ba05 /bin/bash所有postgresql設定檔和資料資料檔案位於:/var/lib/postgresql/data   

 

 

PostgreSQL 基於Docker的多執行個體安裝

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.