本文源库和目标库都是采用二进制tar包进行的安装,非rpm和源码编译方式安装。
采用的办法是编译安装数据库软件 拷贝数据目录的方式
迁移要求
由于Centos即将停止维护,客户强烈要求操作系统更改成Ubuntu18.04,需将Centos的PG14迁移至Ubuntu。考虑到数据目录小才1.8G,业务可以停止,随进行了源码编译安装数据库软件、数据目录拷贝方式实现异构迁移。
OS version | DB version | ip addr | ssh端口 | 安装方式 | |
源库 | Centos7.6 x86_64 | PG14.2 | 172.19.146.150 | 22 | 二进制tar包 |
目标库 | Ubuntu18.04 x86_64 | PG14.2 | 172.19.146.177 | 58595 | 未安装数据库软件 |
迁移步骤
迁移前准备(目标库)
配置离线镜像仓库
上传操作系统镜像ubuntu-18.04.5-server-amd64.iso至/opt
--查操作系统版本
root@ubuntu:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.5 LTS
Release: 18.04
Codename: bionic
--原文件备份
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak_`date +%F`
--创建挂载目录
mkdir -p /media/cdrom
--挂载镜像文件
mount -t iso9660 -o loop /opt/ubuntu-18.04.5-server-amd64.iso /media/cdrom
--向source.list中添加一个新的光盘源文件
apt-cdrom -m -d /media/cdrom add
--更新软件源
apt-get update
系统参数配置
sysctl.conf配置
--备份
cp /etc/sysctl.conf /etc/sysctl.conf_bak_20240120
--添加mysql方面的内容
cat >> /etc/sysctl.conf <<EOF
#add by postgres
#/proc/sys/kernel/优化
# 10000 connect remain:
kernel.sem = 250 162500 250 650
#notice: shall shmmax is base on 16GB, you may adjust it for your MEM
#TODO: open blow two paramenter may make error like this: can not fork xxxx, just reboot your computer ~
#for 2GB Mem:
kernel.shmall = 419430
kernel.shmmax = 171796918
#for 4GB Mem:
#kernel.shmall = 838860
#kernel.shmmax = 3435973836
#for 8GB Mem:
#kernel.shmall = 1677721
#kernel.shmmax = 6871947673
#for 16GB Mem:
#kernel.shmall = 3774873
#kernel.shmmax = 8589934592
#for 32GB Mem:
#kernel.shmall = 7549747
#kernel.shmmax = 17179869184
#for 64GB Mem:
#kernel.shmall = 15099494
#kernel.shmmax = 34359738368
#for 128GB Mem:
#kernel.shmall = 30198988
#kernel.shmmax = 68719476736
#for 256GB Mem:
#kernel.shmall = 60397977
#kernel.shmmax = 137438953472
#for 512GB Mem:
#kernel.shmall = 120795955
#kernel.shmmax = 274877906944
kernel.shmmni = 4096
vm.dirty_background_ratio=2
vm.dirty_ratio = 40
vm.overcommit_memory = 2
vm.overcommit_ratio = 90
vm.swappiness = 0
fs.aio-max-nr = 1048576
fs.file-max = 6815744
fs.nr_open = 20480000
# TCP端口使用范围
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 6000
# 记录的那些尚未收到客户端确认信息的连接请求的最大值
net.ipv4.tcp_max_syn_backlog = 65536
# 每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目
net.core.somaxconn=1024
net.core.netdev_max_backlog = 32768
net.core.wmem_default = 8388608
net.core.wmem_max = 1048576
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_syn_retries = 2
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_wmem = 8192 436600 873200
net.ipv4.tcp_rmem = 32768 436600 873200
net.ipv4.tcp_mem = 94500000 91500000 92700000
net.ipv4.tcp_max_orphans = 3276800
EOF
--生效
sysctl -p
limits.conf配置
--备份
cp /etc/security/limits.conf /etc/security/limits.conf_bak_20240120
--添加以下内容
cat >> /etc/security/limits.conf << "EOF"
#add by postgres
postgres soft nproc 16384
postgres hard nproc 16384
postgres soft nofile 65536
postgres hard nofile 65536
postgres soft stack 1024000
postgres hard stack 1024000
EOF
防火墙配置
默认防火墙是关闭状态
sudo ufw disable #禁用
如果启用防火墙需进行如下配置:
如果数据库端口和ssh端口不是5432和22,需视情况更改
sudo ufw enable #开启防火墙
sudo ufw allow 5432/tcp #仅tcp
sudo ufw allow 22/tcp #仅tcp
selinux配置
Ubuntu 默认未安装SELinux,省略设置。
root@HKSZF-ZW-172-19-146-175:/etc/selinux# getenforce
Command 'getenforce' not found, but can be installed with:
apt install selinux-utils
安装依赖包
依赖包下载:Launchpad 选择ubuntu
安装build-essential
--查看该软件包的依赖关系
y@ubuntu:~$ apt-cache depends build-essential
|Depends: libc6-dev
Depends: <libc-dev>
libc6-dev
Depends: gcc
Depends: g++
Depends: make
make-guile
Depends: dpkg-dev
--查看系统中libc相关已安装的软件包,
root@HKSZF-ZW-172-19-146-178:~# dpkg -l | grep libc
ii klibc-utils 2.0.4-9ubuntu2 amd64 small utilities built with klibc for early boot
ii libc-bin 2.27-3ubuntu1.2 amd64 GNU C Library: Binaries amd64 GNU C Library: Development binaries
发现无libc-dev
从1个可以访问外网的电脑上查找libc-dev,下载并上传至目标服务器,必须和2.27-3ubuntu1.2 版本相同不然报错
root@ubuntu:/etc/netplan# find / -name libc-dev*.deb
/media/cdrom/pool/main/g/glibc/libc-dev-bin_2.27-3ubuntu1.2_amd64.deb 将该文件下载并上传至目标服务器,必须和2.27-3ubuntu1.2 版本相同不然报错
--安装libc-dev
dpkg -i /media/cdrom/pool/main/g/glibc/libc-dev-bin_2.27-3ubuntu1.2_amd64.deb
--查看系统中libc6相关已安装的软件包,
root@HKSZF-ZW-172-19-146-178:~# dpkg -l | grep libc6
ii libc6:amd64 2.27-3ubuntu1.2 amd64 GNU C Library: Shared libraries
发现无libc6-dev
从1个可以访问外网的电脑上查找libc6-dev,下载并上传至目标服务器,必须和2.27-3ubuntu1.2 版本相同不然报错
root@ubuntu:~# dpkg -l | grep libc6
ii libc6:amd64 2.27-3ubuntu1.2 amd64 GNU C Library: Shared libraries
ii libc6-dev:amd64 2.27-3ubuntu1.2 amd64 GNU C Library: Development Libraries and Header Files
root@ubuntu:~# find / -name libc6-dev*.deb
/media/cdrom/pool/main/g/glibc/libc6-dev_2.27-3ubuntu1.2_amd64.deb 将该文件下载并上传至目标服务器,必须和2.27-3ubuntu1.2 版本相同不然报错
--安装libc6-dev
dpkg -i /opt//libc6-dev_2.27-3ubuntu1.2_amd64.deb
--安装build-essential
sudo apt-get install build-essential
安装readline
--查找
root@HKSZF-ZW-172-19-146-177:/topsoft/pgdb/pgsql# apt-cache search libreadline
libreadline5 - GNU readline and history libraries, run-time libraries
libreadline7 - GNU readline and history libraries, run-time libraries
--安装依赖包
dpkg -i /opt/libtinfo-dev_6.1-1ubuntu1.18.04.1_amd64
dpkg -i /opt/libtinfo5_6.1-1ubuntu1.18.04.1_amd64.deb
--安装readline
dpkg -i /opt/libreadline-dev_7.0-3_amd64.deb
安装zlib zlib-devel
在ubuntu软件源里zlib和zlib-devel叫做zlib1g zlib1g.dev
$ sudo apt-get install zlib1g
$ sudo apt-get install zlib1g.dev
如果存储库里没 下载并上传zlib1g-dev_1.2.11.dfsg-0ubuntu2_amd64.deb
--查找系统中已有相关zlib
root@ubuntu:/topsoft/pgdb/pgsql# dpkg -l | grep zlib
ii zlib1g:amd64 1:1.2.11.dfsg-0ubuntu2 amd64 compression library - runtime
root@ubuntu:/topsoft/pgdb/pgsql#
--安装
dpkg -i /opt/zlib1g-dev_1.2.11.dfsg-0ubuntu2_amd64.deb
请记住 g 前面的字母是数字 1,而不是小写的字母 L。很多人在输入命令时都会犯这个错误。
另一个包,zlib 1g-dev 是开发包。只有在你需要时才安装它,否则你应该使用 zlib 1g 包。
sudo apt install zlib1g-dev
你也可以 Zlib 网站 下载源代码并安装它。但是,除非你有充分的理由,否则我不推荐使用源代码方式来安装 zlib。例如,如果你需要最新或特定版本的 zlib,但该版本在发行版的仓库中不可用。
有趣的是,像安装 zlib 这样看似很小的东西可能会变得很麻烦,有两个原因:一个是不同的包名;另一个是包含“隐藏”数字 1,它与小写 L 混淆了。
安装openssl-devel
由于Launchpad中无1.1.1-1ubuntu2.1~18.04.6 版本的libssl-dev,所以下载了libssl和libssl-dev
--查看系统中已有的openssl包
root@ubuntu:/topsoft/pgdb# dpkg -l | grep openssl
ii libxmlsec1-openssl:amd64 1.2.25-1build1 amd64 Openssl engine for the XML security library
ii openssl 1.1.1-1ubuntu2.1~18.04.6 amd64 Secure Sockets Layer toolkit - cryptographic utility
ii python3-openssl 17.5.0-1ubuntu1 all Python 3 wrapper around the OpenSSL library
--下载1.1.1-1ubuntu2.1~18.04.6 该版本的openssl-devel
dpkg -i libssl1.1_1.1.1-1ubuntu2.1~18.04.23_amd64.deb
dpkg -i libssl-dev_1.1.1-1ubuntu2.1~18.04.23_amd64.deb
--安装后查看
root@ubuntu:/opt# dpkg -l | grep libssl
ii libssl-dev:amd64 1.1.1-1ubuntu2.1~18.04.23 amd64 Secure Sockets Layer toolkit - development files
ii libssl1.0.0:amd64 1.0.2n-1ubuntu5.3 amd64 Secure Sockets Layer toolkit - shared libraries
ii libssl1.1:amd64 1.1.1-1ubuntu2.1~18.04.23 amd64 Secure Sockets Layer toolkit - shared libraries
编译安装uuid(目标库可选)
上传uuid至/opt下
--源码编译安装uuid
tar -xvf /opt/uuid-1-6-2-tar.gz
cd /opt/uuid-1.6.2/
./configure
make
make install
创建用户
--创建新用户
sudo useradd -r -m -s /bin/bash postgres
--更改密码
sudo passwd postgres
创建目录(可选)
su - root
mkdir -p /topsoft/pgdb
编译安装pg数据库软件(目标库)
--解压
tar -xvf postgresql-14.9.tar.gz -C /topsoft/pgdb
--重命名
mv /topsoft/pgdb/postgresql-14.9 /topsoft/pgdb/pgsql
--编译安装pg数据库软件
cd /topsoft/pgdb/pgsql
./configure --prefix=/topsoft/pgdb/pgsql --with-pgport=5432 --with-uuid=ossp
停止数据库(源库)
--查看源库进程,确定安装目录和数据目录
[root@HKSZF-ZW-172-19-146-150 ~]# ps -ef | grep "postgres -D"
postgres 1063 1 0 2023 ? 00:01:17 /topsoft/pgdb/pgsql/bin/postgres -D /topsoft/pgdb/data
PG安装目录:/topsoft/pgdb/pgsql
PG数据文件目录:/topsoft/pgdb/data
--停止源数据库
pg_ctl stop -D /topsoft/pgdb/data
ps -ef | grep postgres 确保无进程
文件传输(源库到目标库)
文件传输包含以下文件:
- 数据目录
- 系统服务文件
我这儿是采用tar包二进制安装到了一个目录/topsoft下的/pgdb,如果目录是分开的请注意分开传输
--含安装目录+数据目录+配置文件传输
cd /topsoft
scp -P 58595 -r pgdb root@172.19.146.177:/topsoft/
--系统服务文件传输
scp -P 58595 /usr/lib/systemd/system/postgres.service root@172.19.146.177:/lib/systemd/system/
权限配置(新库)
chown -R postgres:postgres /topsoft/pgdb/
chmod -R 755 /topsoft/pgdb/
chmod -R 750 /topsoft/pgdb/data
系统服务文件配置
--配置系统服务链接
ln -s /lib/systemd/system/postgres.service /etc/systemd/system/postgres.service
chmod +x /lib/systemd/system/postgres.service
如果源库目录和新库目录一致,无需更改系统服务文件里的安装位置和配置文件位置,不然需要更改系统服务文件里的安装位置和配置文件位置和端口
[root@localhost ~]# cat /lib/systemd/system/postgres.service
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
Environment=PGPORT=5896
Environment=PGDATA=/topsoft/pgdb/data
OOMScoreAdjust=-1000
ExecStart=/topsoft/pgdb/pgsql/bin/pg_ctl start -D $PGDATA
ExecStop=/topsoft/pgdb/pgsql/bin/pg_ctl stop -D $PGDATA -s -m fast
ExecReload=/topsoft/pgdb/pgsql/bin/pg_ctl reload -D $PGDATA -s
TimeoutSec=300
[Install]
WantedBy=multi-user.target
启动数据库(目标库)
--启动数据库(目标库)
systemctl start postgres
systemctl status postgres
--配置数据库开机自启
systemctl enable postgres
配置环境变量
su - postgres
--备份源环境变量
cp .bashrc .bashrc_bak_20240120
--更改环境变量文件
vi .bashrc
末尾增加
export PATH=$PATH:/topsoft/pgdb/pgsql/bin
export PGHOME=/topsoft/pgdb/pgsql
export PGDATA=/topsoft/pgdb/data
export PGPORT=5432
export PGPASSWORD=top@123
export MANPATH=$PGHOME/share/man:$MANPATH
export LD_LIBRARY_PATH=$PGHOME/lib:/lib/
SCRIPTS_DIR=/topsoft/pgdb/scripts
export LANG="en_US.UTF-8"
export DATE=`date +"%Y%m%d%H%M"`
登录数据库
配置环境变量后新开一窗口登录数据库。
postgres@HKSZF-ZW-172-19-146-178:~$ psql
psql (14.9)
Type "help" for help.
postgres=#
迁移成功。
问题处理
编译报错error: C compiler cannot create executables
--问题描述
./configure --prefix=/topsoft/pgdb/pgsql --with-pgport=5432 --with-openssl --with-readline --with-uuid=ossp
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether NLS is wanted... no
checking for default port number... 5432
checking for block size... 8kB
checking for segment size... 1GB
checking for WAL block size... 8kB
checking for gcc... gcc
checking whether the C compiler works... no
configure: error: in `/topsoft/pgdb/pgsql':
configure: error: C compiler cannot create executables
See `config.log' for more details
root@HKSZF-ZW-172-19-146-177:/topsoft/pgdb/pgsql#
--解决办法
参照安装依赖包步骤安装build-essential
编译报错error: readline library not found
--问题描述
./configure --prefix=/topsoft/pgdb/pgsql --with-pgport=5432 --with-openssl --with-readline --with-uuid=ossp
checking for library containing readline... no
configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure. It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.
--解决办法
checking for library containing readline... no
configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure. It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.
root@HKSZF-ZW-172-19-146-177:/topsoft/pgdb/pgsql# apt-cache search libreadline
libreadline5 - GNU readline and history libraries, run-time libraries
libreadline7 - GNU readline and history libraries, run-time libraries
root@HKSZF-ZW-172-19-146-177:/topsoft/pgdb/pgsql# sudo apt install libreadline-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libreadline-dev
参照上面安装依赖包步骤中安装readline
编译报错error: library 'crypto'
--问题描述
./configure --prefix=/topsoft/pgdb/pgsql --with-pgport=5432 --with-openssl --with-readline --with-uuid=osspchecking for CRYPTO_new_ex_data in -lcrypto... no
configure: error: library 'crypto' is required for OpenSSL
--解决办法
参照安装依赖包中安装openssl-dev
编译报错 error: library 'ossp-uuid' or 'uuid' is required
--问题描述
root@HKSZF-ZW-172-19-146-177:/topsoft/pgdb/pgsql# ./configure --prefix=/topsoft/pgdb/pgsql --with-pgport=5432 --with-uuid=ossp
checking for library containing readline... -lreadline
checking for inflate in -lz... yes
checking for uuid_export in -lossp-uuid... no
checking for uuid_export in -luuid... no
configure: error: library 'ossp-uuid' or 'uuid' is required for OSSP UUID
--解决办法
参照上面安装依赖包步骤里的安装uuid
编译报错Command 'gmake' not found
由于ubuntu上取消了gmake(GUNmake)的而全部使用make代替。所以此问题的解决方式是有两种:
1、makefile中 gmake用make代替;
2、创建一个make的gmake连接:sudo ln -s /usr/bin/make /usr/bin/gmake.
--问题描述
root@HKSZF-ZW-172-19-146-177:/topsoft/pgdb/pgsql# ./configure --prefix=/topsoft/pgdb/pgsql --with-pgport=5432 --with-uuid=ossp
root@ubuntu:/topsoft/pgdb/pgsql# gmake
Command 'gmake' not found, but there are 14 similar ones.
--解决办法
make替换gmake
psql登录数据库提示"/tmp/.s.PGSQL.5896" failed
问题描述
root@HKSZF-ZW-172-19-146-177:~# su - postgres
postgres@HKSZF-ZW-172-19-146-177:~$ psql
psql: /lib/x86_64-linux-gnu/libcrypto.so.10: no version information available (required by /topsoft/pgdb/pgsql/lib/libpq.so.5)
psql: /lib/x86_64-linux-gnu/libssl.so.10: no version information available (required by /topsoft/pgdb/pgsql/lib/libpq.so.5)
psql: error: connection to server on socket "/tmp/.s.PGSQL.5896" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
解决办法
检查pgsql应用和端口是否正确,如pgsql运行和5432端口运行OK,则检查环境变量是否设置错误
--配置环境变量
su - postgres
--备份源环境变量
cp .bashrc .bashrc_bak_20240120
--更改环境变量文件
vi .bashrc
末尾增加
export PATH=$PATH:/topsoft/pgdb/pgsql/bin
export PGHOME=/topsoft/pgdb/pgsql
export PGDATA=/topsoft/pgdb/data
export PGPORT=5432
export PGPASSWORD=top@123
export MANPATH=$PGHOME/share/man:$MANPATH
export LD_LIBRARY_PATH=$PGHOME/lib:/lib/
SCRIPTS_DIR=/pgdb/scripts
export LANG="en_US.UTF-8"
export DATE=`date +"%Y%m%d%H%M"`
参考链接:
启库报错链接:https://www.cnblogs.com/hufulinblog/p/10124001.html