注:使用Python3 连接 OceanBase数据库,可通过安装 PyMySQL驱动包来实现。
本次测试是在一台安装部署OBD的OceanBase 测试linux服务器上,通过python来远程操作OceanBase数据库。
一、Linux服务器通过Python3连接OceanBase数据库
1.1 安装python3.6
[root@obproxy-node ~]# mkdir /usr/local/python3
[root@obproxy-node ~]# cd /usr/local/python3
[root@obproxy-node ~]# wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
[root@obproxy-node ~]# tar -zxf Python-3.6.8.tgz
[root@obproxy-node ~]# cd /usr/local/python3/Python-3.6.8
-- 不加CFLAGS=-fPIC,会在升级3.0的时候遇到编译问题
[root@obproxy-node Python-3.6.8]# ./configure --prefix=/usr/local/python3 --enable-shared CFLAGS=-fPIC
[root@obproxy-node Python-3.6.8]# make && make install
[root@obproxy-node Python-3.6.8]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@obproxy-node Python-3.6.8]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
-- 设置环境变量
[root@obproxy-node Python-3.6.8]# cat >>/etc/profile<<EOF
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/python3/bin
EOF
[root@obproxy-node Python-3.6.8]# source /etc/profile
[root@obproxy-node Python-3.6.8]# cp /usr/local/python3/Python-3.6.8/libpython3.6m.so.1.0 /usr/lib64 -- 如果不操作该步骤,安装PyMySQL会报如下错误:
[root@obproxy-node ~]# cd /root/soft/PyMySQL-main
[root@obproxy-node PyMySQL-main]# python3 -m pip install PyMySQL
python3: error while loading shared libraries: libpython3.6m.so.1.0: cannot open shared object file: No such file or directory
1.2 安装PyMySQL
-- 登陆 https://github.com/PyMySQL/PyMySQL 下载 安装包 PyMySQL-main.zip 并上传至服务器
[root@obproxy-node ~]# unzip PyMySQL-main.zip
[root@obproxy-node ~]# cd PyMySQL-main
[root@obproxy-node PyMySQL-main]# python3 -m pip install PyMySQL
Collecting PyMySQL
Downloading https://files.pythonhosted.org/packages/4f/52/a115fe175028b058df353c5a3d5290b71514a83f67078a6482cff24d6137/PyMySQL-1.0.2-py3-none-any.whl (43kB)
100% |████████████████████████████████| 51kB 42kB/s
Installing collected packages: PyMySQL
Successfully installed PyMySQL-1.0.2
1.3 编辑python脚本连接OceanBase数据库
[root@obproxy-node PyMySQL-main]# su - admin
Last login: Mon Dec 19 16:43:57 CST 2022 on pts/0
-- 编辑python3ConnOceanBase.py脚本
[admin@obproxy-node python]$ cat python3ConnOceanBase.py
#!/bin/python3
import pymysql
conn = pymysql.connect(host="10.110.3.xxx", port=2883,
user="root@sys#xxx", passwd="ob@Passwd", db="oceanbase")
try:
with conn.cursor() as cur:
cur.execute('SELECT * FROM mysql.user;')
ans = cur.fetchall()
print(ans)
except Exception as err:
print(err)
finally:
conn.close()
-- 执行python
[admin@obproxy-node python]$ python3 python3ConnOceanBase.py
(('%', 'root', '*64b5bdb98225656e2771fc9516fc3a178ec6da86', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'N', 'N', 'Y', 'Y', 'Y', 'N', 'Y', 'Y', 'Y', 'Y', 'N', 'N', 'N', 'Y', 'Y', 'Y', 'Y', 'N', 'N', 'Y', 'N', 'N', 'N', '', '', '', '', 0, 0, 0, 0, 'ob_native_password', '', '', 'N'), ('%', 'ORAAUDITOR', '*9753e2cf9d2dcd5e13c052f581c310ac70c62723', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'N', 'N', 'Y', 'Y', 'Y', 'N', 'Y', 'Y', 'Y', 'Y', 'N', 'N', 'N', 'Y', 'Y', 'Y', 'Y', 'N', 'N', 'Y', 'N', 'N', 'N', '', '', '', '', 0, 0, 0, 0, 'ob_native_password', '', '', 'Y'), ('%', 'proxyro', '*64b5bdb98225656e2771fc9516fc3a178ec6da86', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', '', '', '', '', 0, 0, 0, 0, 'ob_native_password', '', '', 'N'), ('%', 'obuser1', '*db2baac4701f4e4e3e57d4a596744d7bdda3a7cc', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', '', '', '', '', 0, 0, 0, 0, 'ob_native_password', '', '', 'N'))
-- 可以正常查询OceanBase数据库上的表。