【问题标题】:How to connect MariaDB database on Synology NAS from SQLalchemy in python?如何从 Python 中的 SQLalchemy 连接 Synology NAS 上的 MariaDB 数据库?
【发布时间】:2022-10-02 17:31:02
【问题描述】:

我想使用 python SQLalchemy 连接到 Synology NAS 中的 MariaDB10 数据库。我安装了 PhpMyAdmin,并创建了一个名为 \"test\" 的数据库和一个名为 \"company\" 的随机表。我已经通过界面在表中插入了几行虚拟数据。这是它的快照。

我的代码是这样的:

# Module Imports
import sqlalchemy
import pymysql
from sqlalchemy.ext.declarative import declarative_base
import config_maria_us

# Define the MariaDB engine using MariaDB Connector/Python
user = \"username\"
passwd = \"password\"
host = \"192.168.1.111\"
db = \"test\"
port= \"3307\"

engine = sqlalchemy.create_engine(f\'mysql+pymysql://{user}:{passwd}@{host}:{port}/{db}\')

sql_df = engine.execute(\"SELECT * FROM company\" ).fetchall()

但这会返回一个错误:

OperationalError: (2003, \"Can\'t connect to MySQL server on \'192.168.1.111\' ([Errno 61] Connection refused)\")

因为这个page,所以我一直使用create_engine(\"mysql+pymysql:。它说要连接到 MariaDB 数据库,不需要更改数据库 URL。

我关注了这个page,并尝试通过brew install mariadb SQLAlchemy 安装mariadb SQLAlchemy。但它显示警告Warning: No available formula with the name \"sqlalchemy\". Did you mean sqlancer?

然后我当然用brew install mariadb-connector-c安装了MariaDB Connector/C(通过page)并用pip install PyMySQL安装了PyMySQL。其实一开始我尝试用brew install mariadb安装mariadb,但是加载一堆东西后,显示失败,

Error: Cannot install mariadb because conflicting formulae are installed.
  mysql: because mariadb, mysql, and percona install the same binaries

Please `brew unlink mysql` before continuing.

Unlinking removes a formula\'s symlinks from /opt/homebrew. You can
link the formula again after the install finishes. You can --force this
install, but the build may fail or cause obscure side effects in the
resulting software.

我没有继续安装它,因为我不知道如何在取消链接后“重新链接”MySQL。

差不多就这些了,谁能告诉我怎么办?通过运行 \"engine = ...\" 语法,看起来我至少到达了我的服务器,但它仍然无法连接为\'(pymysql.err.OperationalError) (2003, \"Can\'t connect to MySQL server\'

    标签: python sqlalchemy mariadb apple-m1 nas


    【解决方案1】:

    OP 可能由他/她自己解决,但以防其他人仍然面临类似问题。就我而言,按照以下步骤,我可以从 python 脚本访问托管在 NAS 中的 mariadb。

    1. 确保 MariaDB 开启 TCP/IP 连接

    2. 确保来自工作机器 IP 的用户名对数据库具有权限。您可以通过以下方式进行设置

      GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';

    【讨论】:

      猜你喜欢
      • 2021-12-29
      • 2019-03-06
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 2021-07-14
      • 2017-06-21
      相关资源
      最近更新 更多