PostgreSQL
- 安裝并初始化PostgreSQL
安裝postgresql-server
yum install postgresql-server postgresql-contrib
檢查安裝所在路勁
[root@172-12-0-100 ~]# rpm -ql postgresql-server | grep setup
/usr/bin/postgresql-setup
初始化數(shù)據(jù)庫
postgresql-setup initdb
開啟服務(wù)并設(shè)置為自啟動
systemctl start postgresql
systemctl enable postgresql
查看當前版本并修改用戶密碼
[root@172-12-0-100 share]# psql --version
psql (PostgreSQL) 9.2.24
[root@172-12-0-100 share]# su - postgres
-bash-4.2$ psql -U postgres
psql (9.2.24)
Type "help" for help.
postgres=# ALTER USER postgres WITH PASSword '123456';
postgres-#
postgres-# q
-bash-4.2$
開啟遠程訪問
缺省時,postgresql只接受主機本身發(fā)起的連接
[root@172-12-0-100 data]# netstat -anlp | grep 5432
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 20737/postgres
tcp6 0 0 ::1:5432 :::* LISTEN 20737/postgres
修改監(jiān)聽地址
vim /var/lib/pgsql/data/postgresql.conf
修改#listen_addresses = 'localhost' 為 listen_addresses='*'
配置文件的路徑可通過如下方式查找

配置信任連接
vim /var/lib/pgsql/data/pg_hba.conf

重啟服務(wù)
systemctl restart postgresql
檢查接口監(jiān)聽情況

設(shè)置防火墻,允許用戶連接
firewall-cmd --permanent --add-port=5432/tcp
firewall-cmd –reload
創(chuàng)建新用戶和測試用數(shù)據(jù)庫
[root@172-12-0-100 share]# su - postgres
-bash-4.2$ psql -U postgres
psql (9.2.24)
Type "help" for help.
postgres=#
創(chuàng)建用戶:
postgres=# create user admin with password '12345678';
CREATE ROLE
創(chuàng)建測試用數(shù)據(jù)庫并指定擁有者,將數(shù)據(jù)庫所有權(quán)限賦予該擁有者
postgres=# create database testdb owner admin;
CREATE DATABASE
postgres=#
postgres=# grant all on database testdb to admin;
GRANT
查看數(shù)據(jù)庫(l)

退出(q)
postgres=# q
-bash-4.2$ exit
Logout
使用Python嘗試遠程連接postgresql
Python連接postgresql時,需要先安裝psycopg2
pip install psycopg2
寫一個python腳本測試數(shù)據(jù)庫的連接情況
## 導入psycopg2包
import psycopg2
## 連接到一個給定的數(shù)據(jù)庫
conn = psycopg2.connect(database="testdb", user="admin",
password="12345678", host="10.10.11.250", port="5432")
## 建立游標,用來執(zhí)行數(shù)據(jù)庫操作
cursor = conn.cursor()
## 執(zhí)行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()
## 執(zhí)行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')
## 關(guān)閉游標
cursor.close()
## 關(guān)閉數(shù)據(jù)庫連接
conn.close()
MySQL
安裝并初始化mysql數(shù)據(jù)庫
當使用yum install mysql時,會直接安裝MariaDB。MySQL先后被Sun, Oracle收購, MySQL之父的Michael以他女兒Maria的名字開始了MySQL的另外一個衍生版本:MariaDB.
yum install mariadb-server
查看服務(wù)啟動情況和端口監(jiān)聽情況
systemctl status mariadb

監(jiān)聽端口

也可以通過執(zhí)行mysql后,通過show global variables like 'port'進行查詢

修改監(jiān)聽端口
在配置文件/etc/my.cnf文件中,直接增加一行port=3309,重啟服務(wù),即可將端口由缺省的3306修改為3309

重新加載服務(wù)
systemctl restart mariadb
一些配置說明
[mysqld]
skip-networking=1 #不監(jiān)聽任何網(wǎng)絡(luò)接口(即netstat看不到3306端口)
datadir=/var/lib/mysql #數(shù)據(jù)庫存放目錄
創(chuàng)建測試數(shù)據(jù)庫
[root@172-12-0-100 ~]# mysql
MariaDB [(none)]> create database testdb;
--創(chuàng)建可遠程登錄的用戶
create user 'admin'@'%' identified by '12345678';
---授權(quán)該用戶對testdb的所有權(quán)限
grant select,insert,delete,update,create,drop on testdb.* to 'admin'@'%' identified by '12345678';
flush privileges;--立即啟用修改
如果創(chuàng)建錯了,可刪除該用戶
DROPUSER'username'@'host';
放開防火墻端口:
firewall-cmd --permanent --add-port=3306/tcp
firewall-cmd --reload
通過python遠程訪問Mysql數(shù)據(jù)庫
pip install pymysql
腳本示例:
# 導入pymysql模塊
import pymysql
# 連接database
conn = pymysql.connect(host="10.10.11.250", user="admin", password="12345678",database="testdb",charset="utf8")
# 得到一個可以執(zhí)行SQL語句的光標對象
cursor = conn.cursor()
# 創(chuàng)建表格
sql = """
CREATE TABLE USER1 (
id INT auto_increment PRIMARY KEY ,
name CHAR(10) NOT NULL UNIQUE,
age TINYINT NOT NULL
)ENGINE=innodb DEFAULT CHARSET=utf8;
"""
# 執(zhí)行SQL語句
cursor.execute(sql)
#插入數(shù)據(jù)
sql = "INSERT INTO USER1(name, age) VALUES (%s, %s);"
username = "Moses"
age = 180
# 執(zhí)行SQL語句
cursor.execute(sql, [username, age])
# 提交事務(wù)
conn.commit()
username = "Joshua"
age = 120
cursor.execute(sql, [username, age])
conn.commit()
#查詢數(shù)據(jù)
sql = "SELECT * from USER1;"
cursor.execute(sql)
results = cursor.fetchall()
print(results)
# 關(guān)閉光標對象
cursor.close()
# 關(guān)閉數(shù)據(jù)庫連接
conn.close()