首先需要进行以下准备工作:
- 需要有一台安装了 Python 环境的机器,并安装 MySQL 的 Python 库;
- 需要有一个跳板机和一个 MySQL 数据库服务器,可以通过 SSH 连接上跳板机。
下面是 Python 通过跳板机连接 MySQL 数据库的步骤:
- 导入必要的库
import pymysql
import paramiko
- 配置跳板机和目标服务器信息
jump_host = 'jump_host_ip'
jump_port = 22
jump_user = 'jump_username'
jump_password = 'jump_password'
db_host = 'db_host_ip'
db_port = 3306
db_user = 'db_username'
db_password = 'db_password'
- 创建 SSH Client 和 SSH Transport,连接到跳板机,并获取到一个 SSH Channel
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(jump_host, jump_port, jump_user, jump_password)
transport = ssh.get_transport()
channel = transport.open_channel('direct-tcpip', (db_host, db_port), ('localhost', 0))
- 创建 MySQL 连接并连接数据库
database = pymysql.connect(host=db_host, port=db_port, user=db_user, password=db_password, db='database_name', charset='utf8', cursorclass=pymysql.cursors.DictCursor, conv=pymysql.converters.conversions)
这里需要注意的是,需要将连接主机设置为「localhost」,端口设置为「0」,因为已经通过 SSH 隧道连接到了目标数据库服务器,即主机地址和端口号转发到了跳板机。
- 完成操作后断开连接
transport.close()
ssh.close()
示例一:
假设跳板机地址为 192.168.1.10
,MySQL 服务器地址为 192.168.1.20
,账号密码为 root/root,数据库名为 testdb。
import pymysql
import paramiko
jump_host = '192.168.1.10'
jump_port = 22
jump_user = 'root'
jump_password = 'jump_password'
db_host = '192.168.1.20'
db_port = 3306
db_user = 'root'
db_password = 'db_password'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(jump_host, jump_port, jump_user, jump_password)
transport = ssh.get_transport()
channel = transport.open_channel('direct-tcpip', (db_host, db_port), ('localhost', 0))
database = pymysql.connect(host='localhost', port=0, user=db_user, password=db_password, db='testdb', charset='utf8', cursorclass=pymysql.cursors.DictCursor, conv=pymysql.converters.conversions)
cursor = database.cursor()
cursor.execute("show databases")
result = cursor.fetchall()
print(result)
transport.close()
ssh.close()
执行结果为:
[{'Database': 'information_schema'}, {'Database': 'mysql'}, {'Database': 'performance_schema'}, {'Database': 'sys'}, {'Database': 'testdb'}]
示例二:
假设跳板机地址为 192.168.1.10
,MySQL 服务器地址为 192.168.1.30
,账号密码为 root/root,数据库名为 testdb2。
import pymysql
import paramiko
jump_host = '192.168.1.10'
jump_port = 22
jump_user = 'root'
jump_password = 'jump_password'
db_host = '192.168.1.30'
db_port = 3306
db_user = 'root'
db_password = 'db_password'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(jump_host, jump_port, jump_user, jump_password)
transport = ssh.get_transport()
channel = transport.open_channel('direct-tcpip', (db_host, db_port), ('localhost', 0))
database = pymysql.connect(host='localhost', port=0, user=db_user, password=db_password, db='testdb2', charset='utf8', cursorclass=pymysql.cursors.DictCursor, conv=pymysql.converters.conversions)
cursor = database.cursor()
cursor.execute("show tables")
result = cursor.fetchall()
print(result)
transport.close()
ssh.close()
执行结果为:
[{'Tables_in_testdb2': 'user'}, {'Tables_in_testdb2': 'post'}, {'Tables_in_testdb2': 'comment'}]
通过代码示例可以看到,使用 Python 通过跳板机连接到 MySQL 数据库十分简单,只需要借助 paramiko 库即可。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python如何通过跳板机连接MySQL - Python技术站