linux(centos6) 下安装 postgresql-9.3.1.tar.gz

一、 环境

centos6.0 minimal
postgresql-9.3.1.tar.gz

二、准备工作

  • 1将虚拟机网卡使用NAT模式,方便上网
  • 2关闭防火墙,可以查看02.centos常用操作.md

三、先安装 make, gcc ,gcc-c++,readline-devel ,zlib-devel 。如果已安装,可以忽略

这些都是依赖。
[root@linhp local]# yum -y install gcc

[root@linhp local]# yum -y install gcc-c++

[root@linhp local]# yum -y install readline-devel

[root@linhp local]# yum -y install zlib-devel

[root@linhp postgresql-9.3.1]# yum -y install make

四、开始安装

4.1 解压 tar -zvxf postgresql-9.3.1.tar.gz

我的postgresql-9.3.1.tar.gz 安装包放在 /usr/local 下

[root@linhp local]# pwd
/usr/local
[root@linhp local]# tar -zvxf postgresql-9.3.1.tar.gz
...解压过程
[root@linhp local]# cd postgresql-9.3.1
[root@linhp postgresql-9.3.1]# 

4.2 创建linux用户及密码

我的密码设置为123456

[root@linhp postgresql-9.3.1]# adduser postgres
[root@linhp postgresql-9.3.1]# passwd postgres
Changing password for user postgres.
New password: 
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@linhp postgresql-9.3.1]# 

4.3 开始安装

[root@linhp postgresql-9.3.1]# pwd
/usr/local/postgresql-9.3.1
// 配置
[root@linhp postgresql-9.3.1]# ./configure --prefix=/usr/local/postgresql
// 编译
[root@linhp postgresql-9.3.1]# make
//安装
[root@linhp postgresql-9.3.1]# make install

4.4 配置环境变量

在环境变量 /etc/profile 最后加入
PATH=$PATH:/usr/local/postgresql/bin

[root@linhp postgresql]# cd /usr/local/postgresql
[root@linhp postgresql]# ls
bin  include  lib  share
[root@linhp postgresql]# vi /etc/profile 
在文件最后加入:PATH=$PATH:/usr/local/postgresql/bin

然后刷新环境变量,即时生效
[root@linhp postgresql]# source /etc/profile
输出环境变量,查看是否设置成功
[root@linhp postgresql]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/java/jdk1.8.0_91/bin:/opt/java/jdk1.8.0_91/jre/bin:/root/bin:/opt/java/jdk1.8.0_91/bin:/opt/java/jdk1.8.0_91/jre/bin:/usr/local/postgresql/bin

4.5 初始化数据库

[root@linhp postgresql]# pwd  
/usr/local/postgresql
[root@linhp postgresql]# mkdir data
[root@linhp postgresql]# ls
bin  data  include  lib  share
[root@linhp postgresql]# chown postgres:postgres /usr/local/postgresql/data/
[root@linhp postgresql]# su postgres
[postgres@linhp postgresql]$ /usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data/```

4.6 复制并修改配置文件(修改存放数据目录)

切换到root
复制安装目录下的linux文件到/etc/init.d/中,并将linux名称重命名为postgresql
[root@linhp postgresql-9.3.1]# cp /usr/local/postgresql-9.3.1/contrib/start-scripts/linux /etc/init.d/postgresql
编辑复制出来的文件,修改下图中的位置即可
[root@linhp postgresql-9.3.1]# vi /etc/init.d/postgresql

image

[root@linhp postgresql-9.3.1]# chmod +x /etc/init.d/postgresql

4.7 启动数据库,和设置开机自启

[root@linhp postgresql-9.3.1]# /etc/init.d/postgresql start
Starting PostgreSQL: ok
[root@linhp postgresql-9.3.1]# chkconfig postgresql on
[root@linhp postgresql-9.3.1]# 

4.8 创建数据库操作的历史文件

创建文件,并授予postgres权限

[root@linhp postgresql-9.3.1]# touch /usr/local/postgresql/.pgsql_history
[root@linhp postgresql-9.3.1]# chown postgres:postgres /usr/local/postgresql/.pgsql_history 
[root@linhp postgresql-9.3.1]# 

4.9 测试数据库是否创建成功,并且连接数据库

使用 \q 即可退出数据库连接

[root@linhp postgresql-9.3.1]# su postgres
[postgres@linhp postgresql-9.3.1]$ createdb test
[postgres@linhp postgresql-9.3.1]$ psql test
psql (9.3.1)
Type "help" for help.

test=# \q
[postgres@linhp postgresql-9.3.1]$ 

到此,数据库已经安装成功。

五、修改数据库外网访问

Linux 修改PostgreSQL外部访问白名单

开放5432端口

一般linux中默认安装了firewall,若 firewalld 未安装,则先进行安装,安装请另查

启动firewall:
systemctl start firewalld # 启动
systemctl status firewalld #查看firewalld当前状态
systemctl enable firewalld # 开机启动
systemctl stop firewalld # 关闭
systemctl disable firewalld # 取消开机启动

查看开放端口命令:
firewall-cmd --list-ports

添加开放端口:
firewall-cmd --zone=public --add-port=5000/tcp --permanent  #对外网全部开放
firewall-cmd --zone=public --permanent --add-rich-rule 'rule family=ipv4 source address=192.168.0.1/24 port port=22 protocol=tcp accept' #添加端口开放
firewall-cmd --permanent --remove-rich-rule 'rule family=ipv4 source address=192.168.0.1/2 port port=5000 protocol=tcp accept' #删除端口开放

或使用简单的开放端口命令:
# 允许 tcp 5432 端口
sudo firewall-cmd --permanent --add-port=5432/tcp

reload防火墙,应用规则
sudo systemctl reload firewalld

主要修改两个配置文件

[root@linhp postgresql-9.3.1]# find / -name pg_hba.conf
[root@linhp postgresql-9.3.1]# find / -name postgresql.conf

5.1 先关闭数据库服务

关闭数据库服务
[postgres@linhp postgresql-9.3.1]$ su
Password: 
[root@linhp postgresql-9.3.1]# /etc/init.d/postgresql stop
Stopping PostgreSQL: ok
[root@linhp postgresql-9.3.1]# 

5.2 修改pg_hba.conf

将默认的

host all all 127.0.0.1/32 trust

修改为

host all all 192.168.1.1/32 trust #IP全匹配
或
host all all 192.168.1.1/24 trust #IP匹配前三段
或
host all all 0.0.0.0/0 trust #全部允许

5.3修改postgresql.conf

listen_addresses 默认是注释掉的,打开即可

修改默认的

listen_addresses = 'localhost'

listen_addresses = '*'

如果需要修改端口,可以修改 listen_addresses下一行

5.4 重启postgresql服务

重启postgresql命令:sudo /etc/init.d/postgresql restart
启动postgresql命令:sudo /etc/init.d/postgresql start

5.5 测试本地连接 ( 这里我修改了端口为5444)

[postgres@linhp postgresql-9.3.1]$ psql -h 127.0.0.1 -d postgres -U postgres -p 5444psql
psql (9.3.1)
Type "help" for help.

postgres=#
好的,你可以按照以下步骤在CentOS上安装mysql-8.tar.gz版本的MySQL: 1. 首先,下载mysql-8.tar.gz包,可以使用以下命令在终端或命令行工具中进行下载: ``` wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.25-linux-glibc2.17-x86_64.tar.gz ``` 注意:以上下载地址是mysql-8.0.25版本的下载地址,如果需要安装其他版本的MySQL,请修改下载地址。 2. 下载完成后,解压mysql-8.tar.gz包,可以使用以下命令进行解压: ``` tar -zxvf mysql-8.0.25-linux-glibc2.17-x86_64.tar.gz ``` 3. 将解压后的mysql-8.0.25-linux-glibc2.17-x86_64目录移动到/usr/local目录下: ``` mv mysql-8.0.25-linux-glibc2.17-x86_64 /usr/local/mysql ``` 4. 进入/usr/local/mysql目录,执行以下命令初始化MySQL: ``` cd /usr/local/mysql bin/mysqld --initialize --user=mysql ``` 5. 修改MySQL配置文件/etc/my.cnf,添加以下内容: ``` [mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysql/error.log pid-file=/var/run/mysql/mysql.pid ``` 注意:以上配置文件中的参数可以根据你的需要进行修改。 6. 创建MySQL运行所需的目录: ``` mkdir /var/run/mysql mkdir /var/log/mysql chown -R mysql:mysql /var/run/mysql chown -R mysql:mysql /var/log/mysql ``` 7. 启动MySQL服务: ``` bin/mysqld_safe --user=mysql & ``` 8. 设置root用户密码: ``` bin/mysql_secure_installation ``` 按照提示,设置root用户密码。 9. 远程连接MySQL: ``` bin/mysql -u root -p ``` 输入密码后,即可进入MySQL命令行界面。 至此,你已经成功在CentOS上安装了mysql-8.tar.gz版本的MySQL。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值