【问题标题】:How to use MySQL with Python in a client server setup如何在客户端服务器设置中使用 MySQL 和 Python
【发布时间】:2020-07-14 18:48:35
【问题描述】:

我想开发一个客户端-服务器风格的 python 项目。 我的 Python 代码将部署在客户端机器上,MySQL 数据库部署在服务器上,因此所有连接都需要通过网络连接。

我知道如何在 MySQL 中使用 Python 以及创建表和查询等基础知识,但是我想学习如何通过网络协议将我的 python 项目连接到数据库。

我在 Google 或 YouTube 上进行了搜索,但只找到了使用本地数据库连接的资源。

我在哪里可以找到解释如何通过网络连接到 MySQL 的 Python 资源(文章、博客、示例、教程或视频)?

【问题讨论】:

    标签: python mysql server


    【解决方案1】:

    这似乎是一个很好的指南。

    https://pynative.com/python-mysql-tutorial/

    我很快尝试了 PyMySql。有用。唯一的问题是它处于测试阶段。 https://pymysql.readthedocs.io/en/latest/user/examples.html

    所以我建议您尝试 MySQL 连接器 Python 最初设置会有些痛苦,但它来自供应商。所以你可以更好地信任它。

    或者https://pypi.org/project/mysqlclient/

    import pymysql.cursors
    import pymysql
    
    try:
        conn = pymysql.connect(host='10.1.1.1 (remote server ip or hostname)',
                                user='db_user_name',
                                password='db_password',
                                db='name_of_db',
                                cursorclass=pymysql.cursors.DictCursor)
    
        sql = "select * from users"
    
        with conn.cursor() as cursor:
            # Read a single record
            sql = "select * from users"
            cursor.execute(sql)
            result = cursor.fetchall()
            print(result)
    except Exception as inst:
        print(str(inst))
    finally:
        conn.close()
    

    【讨论】:

      【解决方案2】:

      你有运行 MySQL 数据库的服务器和一个 python 客户端应用程序,它需要来自数据库的数据?

      我认为通常客户端软件从不直接连接到数据库(出于安全原因)。相反,客户端与服务器应用程序通信(可能通过 REST-API)。

      How i meant it

      【讨论】:

        猜你喜欢
        • 2012-07-28
        • 2012-05-26
        • 2013-03-09
        • 1970-01-01
        • 2014-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-24
        相关资源
        最近更新 更多