1. 程式人生 > >zabbix3.4資料庫遷移,將server和database分到不同的兩臺server

zabbix3.4資料庫遷移,將server和database分到不同的兩臺server

zabbix目前已經是很多公司採用的比較常見的一種系統監控工具,單從個人經驗來講,安裝zabbix應該將資料庫和zabbix-server分開安裝到不同兩臺機器,然後通過網路(最好是內網)連線訪問資料庫是一種比較好的方式。

將資料庫和zabbix-server分開安裝的好處是:降低zabbix-server的系統性能,不需要單獨的消耗機器資源來安裝databse。

系統環境:

zabbix-server:192.168.31.1

database: 192.168.31.2

database:postgresql

1、先備份資料庫:

在zabbix-server上,先停止zabbix-server服務,這樣停止主機對資料庫的寫入操作:

sudo service zabbix-server stop

通過sudo service zabbix-server status命令確認zabbix-server服務已經處於停止狀態。

2、備份資料庫:切換到postgres使用者,用pg_dump工具來對全庫進行一個備份。

pg_dump zabbix  -f  /home/ubuntu/pg_back

其中zabbix是資料庫名稱,pg_back是備份好的檔案,備份到ubuntu使用者的家目錄下。

備份好資料庫後,可以將zabbix-server服務啟動,這樣又可以儲存一部分資料到192.168.31.1這臺原來的主機上。

3、將備份好的資料庫檔案拷貝到192.168.31.2這臺主機上

sudo scp /home/ubuntu/pg_back [email protected]:/home/ubuntu

4、登陸192.168.31.2這臺主機,安裝postgres資料庫:

注意安裝資料庫的條件,在這裡我沒有安裝不同版本的資料庫,安裝了zabbix-server一模一樣的資料庫:postgres9.5

sudo apt-get update

sudo apt-get install postgresql-9.5

5、建立資料庫角色,賦值許可權相關

pg_dump命令只會備份資料庫的資料內容,但是不會備份資料庫角色和許可權相干的內容,包括資料表索引。如果要全量備份這些內容,可以使用pg_dumpall命令,但是此命令會比較耗時。

create user zabbix;

\password zabbix

grant all privileges on database zabbix to zabbix;

6、在192.168.31.2主機上恢復備份的資料庫檔案:

su postgres

psql -u zabbix -d zabbix -f  /home/ubuntu/pg_back

7、資料庫效能設定

調節資料庫連線數&監聽的地址型別:

# - Connection Settings -

listen_addresses = '*'                        # what IP address(es) to listen on;
                                            # comma-separated list of addresses;
                                            # defaults to 'localhost'; use '*' for all
                                                  # (change requires restart)
port = 5432                                        # (change requires restart)
max_connections = 600                               # (change requires restart)
#superuser_reserved_connections = 3                # (change requires restart)
unix_socket_directories = '/var/run/postgresql'    # comma-separated list of directories
                                                      # (change requires restart)
#unix_socket_group = ''                                # (change requires restart)
#unix_socket_permissions = 0777                        # begin with 0 to use octal notation
                                                           # (change requires restart)
#bonjour = off                                         # advertise server via Bonjour
                                                      # (change requires restart)
#bonjour_name = ''                                   # defaults to the computer name

修改完成後,重啟postgresql服務

sudo service postgresql restart

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

至此,資料庫的遷移基本上已經完成,接下來將zabbix-server的資料庫配置指向新安裝的資料庫端

8、修改zabbix-server的配置檔案,將資料庫指向新的database

### Option: DBHost

#    Database host name.

#    If set to localhost, socket is used for MySQL.

#    If set to empty string, socket is used for PostgreSQL.

#

# Mandatory: no

# Default:

DBHost=192.168.31.2



### Option: DBName

#    Database name.

#    For SQLite3 path to database file must be provided. DBUser and DBPassword are ignored.

#

# Mandatory: yes

# Default:

# DBName=



DBName=zabbix



### Option: DBSchema

#    Schema name. Used for IBM DB2 and PostgreSQL.

#

# Mandatory: no

# Default:

# DBSchema=



### Option: DBUser

#    Database user. Ignored for SQLite.

#

# Mandatory: no

# Default:

# DBUser=



DBUser=zabbix



### Option: DBPassword

#    Database password. Ignored for SQLite.

#    Comment this line if no password is used.

#

# Mandatory: no

# Default:

DBPassword=1qaz2wsx



### Option: DBSocket

#    Path to MySQL socket.

#

# Mandatory: no

# Default:

# DBSocket=/tmp/mysql.sock



### Option: DBPort

#    Database port when not using local socket. Ignored for SQLite.

#

# Mandatory: no

# Range: 1024-65535

# Default (for MySQL):

DBPort=5432

修改完成之後,重啟zabbix-server,重啟完成後,發現zabbix介面提示database is down,可是又發現192.168.31.2這臺新的server中的database確實有資料進來,並且已經192.168.31.1這臺機器已經連線到新的database中了。

到底是為什麼?所以,此處有坑。

後來查了一些資料,發現zabbix-server顯示的database資料是通過web來配置定義,配置檔案在如下位置。

sudo vim /etc/zabbix/web/zabbix.conf.php

配置以下內容:

// Zabbix GUI configuration file.

global $DB;



$DB['TYPE']     = 'POSTGRESQL';

$DB['SERVER']   = '192.168.31.2';

$DB['PORT']     = '0';

$DB['DATABASE'] = 'zabbix';

$DB['USER']     = 'zabbix';

$DB['PASSWORD'] = '1qaz2wsx';



// Schema name. Used for IBM DB2 and PostgreSQL.

$DB['SCHEMA'] = '';



$ZBX_SERVER      = 'localhost';

$ZBX_SERVER_PORT = '10051';

$ZBX_SERVER_NAME = '';



$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;

基於以上配置完成,重啟zabbix-server服務

sudo service zabbix-server restart

重啟完成後,發現zabbix介面已經指向新的database,此時將192.168.31.1這臺server上的database資料庫停止,也不會影響zabbix的介面顯示。

至此,zabbix資料庫和server分開到兩臺server上,資料庫遷移完成!