温馨提示×

如何在Ubuntu上配置Python数据库

小樊
41
2025-12-18 12:12:19
栏目: 编程语言

在 Ubuntu 上配置 Python 数据库

一 准备与通用步骤

  • 更新系统并安装基础工具:
    • sudo apt update && sudo apt install -y python3 python3-pip
  • 建议使用虚拟环境隔离依赖:
    • python3 -m venv .venv && source .venv/bin/activate
  • 安装数据库驱动(按需选择):
    • MySQL:pip install mysql-connector-python 或 pip install PyMySQL
    • PostgreSQL:pip install psycopg2-binary
    • SQLite:Python 标准库自带,无需安装
  • 启动数据库服务(若使用本地数据库):
    • MySQL:sudo systemctl start mysql && sudo systemctl enable mysql
    • PostgreSQL:sudo systemctl start postgresql && sudo systemctl enable postgresql
  • 安全与账户(示例):
    • MySQL:运行 sudo mysql_secure_installation,并使用 mysql -u root -p 登录后创建数据库与用户
    • PostgreSQL:sudo -u postgres psql 后执行 CREATE DATABASE/USER/GRANT 等语句

二 按数据库类型的配置与示例

  • MySQL
    • 安装与初始化:sudo apt install -y mysql-server;sudo mysql_secure_installation
    • 创建库与用户(示例):
      • CREATE DATABASE mydb CHARACTER SET utf8mb4;
      • CREATE USER ‘myuser’@‘localhost’ IDENTIFIED BY ‘mypass’;
      • GRANT ALL PRIVILEGES ON mydb.* TO ‘myuser’@‘localhost’;
      • FLUSH PRIVILEGES;
    • Python 连接示例(使用 mysql-connector-python):
      • import mysql.connector
      • conn = mysql.connector.connect(host=‘localhost’, user=‘myuser’, password=‘mypass’, database=‘mydb’)
      • cur = conn.cursor(); cur.execute(‘SELECT 1’); print(cur.fetchone()); cur.close(); conn.close()
  • PostgreSQL
    • 安装与初始化:sudo apt install -y postgresql postgresql-contrib
    • 创建库与用户(示例):
      • sudo -u postgres psql
      • CREATE DATABASE mydb;
      • CREATE USER myuser WITH ENCRYPTED PASSWORD ‘mypass’;
      • GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
    • Python 连接示例(使用 psycopg2-binary):
      • import psycopg2
      • conn = psycopg2.connect(dbname=‘mydb’, user=‘myuser’, password=‘mypass’, host=‘localhost’)
      • cur = conn.cursor(); cur.execute(‘SELECT 1’); print(cur.fetchone()); cur.close(); conn.close()
  • SQLite
    • 特点:零配置、文件型数据库,无需安装服务器
    • Python 连接示例:
      • import sqlite3
      • conn = sqlite3.connect(‘mydb.db’)
      • cur = conn.cursor(); cur.execute(‘CREATE TABLE IF NOT EXISTS t(id INTEGER PRIMARY KEY, name TEXT)’)
      • cur.execute(“INSERT INTO t(name) VALUES (?)”, (‘Alice’,)); conn.commit()
      • cur.execute(‘SELECT * FROM t’); print(cur.fetchall()); conn.close()

三 远程连接与安全加固

  • 启用远程访问(示例)
    • MySQL:编辑 /etc/mysql/mysql.conf.d/mysqld.cnf,设置 bind-address = 0.0.0.0;重启 sudo systemctl restart mysql
    • PostgreSQL:编辑 postgresql.conf 设置 listen_addresses = ‘*’;在 pg_hba.conf 添加规则 host all all 0.0.0.0/0 md5;重启 sudo systemctl restart postgresql
  • 防火墙放行端口
    • sudo ufw allow 3306/tcp
    • sudo ufw allow 5432/tcp
  • 安全建议
    • 使用强密码、限制来源 IP、为应用创建最小权限专用账户
    • 启用 SSL/TLS 加密传输(生产环境强烈建议)
    • 定期更新系统与数据库软件,做好备份与监控

四 常见问题排查

  • 服务未启动:systemctl status mysql/postgresql 查看状态与日志,必要时 sudo systemctl restart
  • 连接被拒绝或超时:核对 host/port、是否监听 0.0.0.0、云服务器安全组/本机防火墙是否放行
  • 权限不足:检查数据库用户是否对目标库有足够权限(GRANT 语句)
  • 客户端库安装失败:优先使用二进制包(如 psycopg2-binary)、确认虚拟环境与 pip 对应 Python 版本、必要时安装系统依赖(libpq-dev/mysql-client 等)

0