PostgreSQL
- 安装并初始化PostgreSQL
yum install postgresql-server postgresql-contrib检查安装所在路劲[root@172-12-0-100 ~]# rpm -ql postgresql-server | grep setup/usr/bin/postgresql-setup初始化数据库postgresql-setup initdb开启服务并设置为自启动systemctl start postgresqlsystemctl enable postgresql查看当前版本并修改用户密码[root@172-12-0-100 share]# psql --versionpsql (PostgreSQL) 9.2.24[root@172-12-0-100 share]# su - postgres-bash-4.2$ psql -U postgrespsql (9.2.24)Type "help" for help.postgres=# ALTER USER postgres WITH PASSword '123456';postgres-#postgres-# q-bash-4.2$开启远程访问缺省时,postgresql只接受主机本身发起的连接
[root@172-12-0-100 data]# netstat -anlp | grep 5432tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 20737/postgrestcp6 0 0 ::1:5432 :::* LISTEN 20737/postgres修改监听地址vim /var/lib/pgsql/data/postgresql.conf修改#listen_addresses = 'localhost' 为 listen_addresses='*' 配置文件的路径可通过如下方式查找

文章插图
配置信任连接
vim /var/lib/pgsql/data/pg_hba.conf 
文章插图
重启服务
systemctl restart postgresql检查接口监听情况
文章插图
设置防火墙,允许用户连接
firewall-cmd --permanent --add-port=5432/tcpfirewall-cmd –reload 创建新用户和测试用数据库
[root@172-12-0-100 share]# su - postgres-bash-4.2$ psql -U postgrespsql (9.2.24)Type "help" for help.postgres=#创建用户:postgres=# create user admin with password '12345678';CREATE ROLE 创建测试用数据库并指定拥有者,将数据库所有权限赋予该拥有者
postgres=# create database testdb owner admin;CREATE DATABASEpostgres=#postgres=# grant all on database testdb to admin;GRANT查看数据库(l)
文章插图
退出(q)
postgres=# q
-bash-4.2$ exit
Logout
使用Python尝试远程连接postgresqlPython连接postgresql时,需要先安装psycopg2
pip install psycopg2写一个python脚本测试数据库的连接情况## 导入psycopg2包import psycopg2## 连接到一个给定的数据库conn = psycopg2.connect(database="testdb", user="admin", password="12345678", host="10.10.11.250", port="5432")## 建立游标,用来执行数据库操作cursor = conn.cursor()## 执行SQL命令cursor.execute("CREATE TABLE test_conn(id int, name text, addr text)")cursor.execute("INSERT INTO test_conn values(1,'Moses','Egypt')")cursor.execute("INSERT INTO test_conn values(2,'Joshua','Israel')")## 提交SQL命令conn.commit()## 执行SQL SELECT命令cursor.execute("select * from test_conn")## 获取SELECT返回的元组rows = cursor.fetchall()for row in rows: print('id = ',row[0], 'name = ', row[1], 'addr = ', row[2], 'n')## 关闭游标cursor.close()## 关闭数据库连接conn.close() MySQL安装并初始化mysql数据库
当使用yum install mysql时,会直接安装MariaDB 。MySQL先后被Sun, Oracle收购,MySQL之父的Michael以他女儿Maria的名字开始了MySQL的另外一个衍生版本:MariaDB.
yum install mariadb-server查看服务启动情况和端口监听情况systemctl status mariadb
推荐阅读
- Centos安装dnsmasq工具,解决局域网dns查询问题
- PostgreSQL的几种分布式架构对比
- Linux发行版之一CentOS的安装与网卡配置
- Docker 安装 Nginx、PHP、MySQL、Tomcat、Python、Redis、Apache
- MySQL 8.0:字符集从 utf8 转换成 utf8mb4
- 安装MySQL数据库
- 从 MySQL 迁移数据到 Oracle 中的全过程
- 从头带你捋一遍 MySQL 索引结构
- MySQL读写分离?MySQL主从复制原理?如何解决主从同步延时?
- CentOS 7集群间实现NFS文件共享
