python连接数据库
python连接mysql数据库
- 连接mysql 数据仓库前先检查mysql数据配置文件 my.cnf
- 先找到配置文件
find / -name "配置文件名"
- 然后修改如下: 可让任何人访问
[mysqld]bind-address = 0.0.0.0
- 重新启动mysqld
以下是python客户端代码
import pymysql
connection = pymysql.connect(
host="host",
user="user",
password="password",
database="database"
)
with connection.cursor() as cursor:
cursor.execute("SELECT * FROM `data`")
results = cursor.fetchall()
for row in results:
print(row)
connection.close()
python连接mongodb数据库
- 连接mysql 数据仓库前先检查mongodb数据配置文件 mongod.conf mongodb.conf
- 先找到配置文件
find / -name "配置文件名"
- 然后修改如下: 可让任何人访问
bindIp: 0.0.0.0
- 重新启动mongodb
以下是python客户端代码
from pymongo import MongoClient
client = MongoClient('mongodb://110.42.168.247:27017/')
db = client['mydatabase']
collection = db['mycollection']
data = {'name': 'John', 'age': 30}
result = collection.insert_one(data)
print('插入成功,插入文档的ID为:', result.inserted_id)
query = {'name': 'John'}
result = collection.find(query)
for document in result:
print(document)
query = {'name': 'John'}
new_data = {'$set': {'age': 32}}
result = collection.update_one(query, new_data)
print('更新成功,修改的文档数量为:', result.modified_count)
query = {'name': 'John'}
result = collection.delete_one(query)
print('删除成功,删除的文档数量为:', result.deleted_count)
client.close()
python连接reids数据库
- 连接mysql 数据仓库前先检查redis数据配置文件
- 先找到配置文件
find / -name "redis.conf"
- 然后修改如下: 可让任何人访问
bind 0.0.0.0
- 关闭安全模式
protected-mode no
- 重新启动redis
- systemctl start redis
- systemctl status redis
- systemctl stop redis
以下是python客户端代码
import redis
r = redis.Redis(host='110.42.168.247', port=6379, db=0)
r.set('mykey', 'Hello Redis')
value = r.get('mykey')
value1 = r.get('jwd')
print(value)
print(value1)
r.close()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY