• <bdo id='uNbii'></bdo><ul id='uNbii'></ul>

  • <tfoot id='uNbii'></tfoot>
  • <small id='uNbii'></small><noframes id='uNbii'>

      <i id='uNbii'><tr id='uNbii'><dt id='uNbii'><q id='uNbii'><span id='uNbii'><b id='uNbii'><form id='uNbii'><ins id='uNbii'></ins><ul id='uNbii'></ul><sub id='uNbii'></sub></form><legend id='uNbii'></legend><bdo id='uNbii'><pre id='uNbii'><center id='uNbii'></center></pre></bdo></b><th id='uNbii'></th></span></q></dt></tr></i><div id='uNbii'><tfoot id='uNbii'></tfoot><dl id='uNbii'><fieldset id='uNbii'></fieldset></dl></div>
        <legend id='uNbii'><style id='uNbii'><dir id='uNbii'><q id='uNbii'></q></dir></style></legend>

        如何使用Python+SQLAlchemy远程连接MySQL数据库?

        时间:2024-04-16
          <legend id='CvNO3'><style id='CvNO3'><dir id='CvNO3'><q id='CvNO3'></q></dir></style></legend>
        1. <tfoot id='CvNO3'></tfoot>

            <bdo id='CvNO3'></bdo><ul id='CvNO3'></ul>
                <tbody id='CvNO3'></tbody>

                <small id='CvNO3'></small><noframes id='CvNO3'>

                  <i id='CvNO3'><tr id='CvNO3'><dt id='CvNO3'><q id='CvNO3'><span id='CvNO3'><b id='CvNO3'><form id='CvNO3'><ins id='CvNO3'></ins><ul id='CvNO3'></ul><sub id='CvNO3'></sub></form><legend id='CvNO3'></legend><bdo id='CvNO3'><pre id='CvNO3'><center id='CvNO3'></center></pre></bdo></b><th id='CvNO3'></th></span></q></dt></tr></i><div id='CvNO3'><tfoot id='CvNO3'></tfoot><dl id='CvNO3'><fieldset id='CvNO3'></fieldset></dl></div>
                1. 本文介绍了如何使用Python+SQLAlchemy远程连接MySQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我无法远程访问 MySQL.我使用SSH隧道,想用Python+SQLALchemy连接数据库MySQL.

                  当我在控制台中使用 MySQL 客户端并指定ptotocol=TCP"时,一切都很好!我使用命令:

                  mysql -h localhost —protocol=TCP -u USER -p

                  我可以通过 SSH 隧道访问远程数据库.

                  但是,当我想使用 Python+SQLAchemy 连接到数据库时,我找不到像 —protocol=TCP 这样的选项否则,我只能连接到本地 MySQL 数据库.请告诉我,有没有办法使用 SQLAlchemy 做到这一点.

                  解决方案

                  这个问题的经典答案是使用 127.0.0.1主机的 IP主机名而不是特殊名称"localhost.从文档:

                  <块引用>

                  [...] Unix 到 localhost 的连接默认使用 Unix 套接字文件进行

                  后来:

                  <块引用>

                  在 Unix 上,MySQL 程序特别对待主机名 localhost,与其他基于网络的程序相比,其方式可能与您期望的不同.对于到本地主机的连接,MySQL 程序尝试使用 Unix 套接字文件连接到本地服务器.即使提供了 --port 或 -P 选项来指定端口号,也会发生这种情况.为确保客户端与本地服务器建立 TCP/IP 连接,请使用 --host 或 -h 指定主机名值 127.0.0.1,或本地服务器的 IP 地址或名称.

                  <小时>

                  但是,这个简单的技巧在您的情况下似乎不起作用,因此您必须以某种方式强制使用 TCP 套接字.正如您自己解释的那样,在命令行上调用 mysql 时,您使用了 --protocol tcp 选项.

                  如此处所述,从 SQLAlchemy,您可以通过您的驱动程序的相关选项(如果有)作为 URL 选项使用 connect_args 关键字参数.

                  例如使用 PyMySQL,在我为此目的设置的测试系统(MariaDB 10.0.12、SQLAlchemy 0.9.8 和 PyMySQL 0.6.2)上,我得到了以下结果:<预><代码>>>>引擎 = 创建引擎("mysql+pymysql://sylvain:passwd@localhost/db?host=localhost?port=3306")#^^^^^^^^^^^^^^^^^^^^^^^^^^# 强制 TCP 套接字.注意`?`的两种用法# 通常 URL 选项应该使用 `?` 和 `&`# 在那之后.但这在这里不起作用(错误?)>>>conn = engine.connect()>>>conn.execute("SELECT host FROM INFORMATION_SCHEMA.PROCESSLIST WHERE ID = CONNECTION_ID()").fetchall()[('本地主机:54164',)]# 使用 127.0.0.1 而不是 localhost 的结果相同:>>>引擎 = 创建引擎("mysql+pymysql://sylvain:passwd@127.0.0.1/db?host=localhost?port=3306")>>>conn = engine.connect()>>>conn.execute("SELECT host FROM INFORMATION_SCHEMA.PROCESSLIST WHERE ID = CONNECTION_ID()").fetchall()[('本地主机:54164',)]# 或者,使用connect_args:>>>engine = create_engine("mysql+pymysql://sylvain:passwd@localhost/db",connect_args= dict(host='localhost', port=3306))>>>conn = engine.connect()>>>conn.execute("SELECT host FROM INFORMATION_SCHEMA.PROCESSLIST WHERE ID = CONNECTION_ID()").fetchall()[('本地主机:54353',)]

                  如您所见,两者都将使用 TCP 连接(我知道这是因为主机名后面的端口号).另一方面:

                  <预><代码>>>>引擎 = 创建引擎("mysql+pymysql://sylvain:passwd@localhost/db?unix_socket=/path/to/mysql.sock")#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^# 指定mysql.sock中的路径# `unix_socket` 选项将强制# UNIX 套接字的使用>>>conn = engine.connect()>>>conn.execute("SELECT host FROM INFORMATION_SCHEMA.PROCESSLIST WHERE ID = CONNECTION_ID()").fetchall()[('本地主机',)]# 使用 127.0.0.1 而不是 localhost 的结果相同:>>>引擎 = 创建引擎("mysql+pymysql://sylvain:passwd@127.0.0.1/db?unix_socket=/path/to/mysql.sock")>>>conn = engine.connect()>>>conn.execute("SELECT host FROM INFORMATION_SCHEMA.PROCESSLIST WHERE ID = CONNECTION_ID()").fetchall()[('本地主机',)]# 或者,使用connect_args:>>>engine = create_engine("mysql+pymysql://sylvain:passwd@localhost/db",connect_args= dict(unix_socket="/path/to/mysql.sock"))>>>conn = engine.connect()>>>conn.execute("SELECT host FROM INFORMATION_SCHEMA.PROCESSLIST WHERE ID = CONNECTION_ID()").fetchall()[('本地主机',)]

                  主机名后没有端口:这是一个 UNIX 套接字.

                  I am having difficulty accessing MySQL remotely. I use SSH tunnel and want to connect the database MySQL using Python+SQLALchemy.

                  When i use MySQL-client in my console and specify "ptotocol=TCP", then everything is fine! I use command:

                  mysql -h localhost —protocol=TCP -u USER -p
                  

                  I get access to remote database through SSH-tunnel.

                  However, when I want to connect to the database using the Python+SQLAchemy I can't find such option like —protocol=TCP Otherwise, i have only connect to local MySQL Databases. Tell me please, is there a way to do it using SQLAlchemy.

                  解决方案

                  The classic answer to this issue is to use 127.0.0.1 or the IP of the host or the host name instead of the "special name" localhost. From the documentation:

                  [...] connections on Unix to localhost are made using a Unix socket file by default

                  And later:

                  On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server.


                  However, this simple trick doesn't appear to work in your case, so you have to somehow force the use of a TCP socket. As you explained it yourself, when invoking mysql on the command line, you use the --protocol tcp option.

                  As explained here, from SQLAlchemy, you can pass the relevant options (if any) to your driver either as URL options or using the connect_args keyword argument.

                  For example using PyMySQL, on a test system I've setup for that purpose (MariaDB 10.0.12, SQLAlchemy 0.9.8 and PyMySQL 0.6.2) I got the following results:

                  >>> engine = create_engine(
                        "mysql+pymysql://sylvain:passwd@localhost/db?host=localhost?port=3306")
                  #                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
                  #                               Force TCP socket. Notice the two uses of `?`
                  #                               Normally URL options should use `?` and `&`  
                  #                               after that. But that doesn't work here (bug?)
                  >>> conn = engine.connect()
                  >>> conn.execute("SELECT host FROM INFORMATION_SCHEMA.PROCESSLIST WHERE ID = CONNECTION_ID()").fetchall()
                  [('localhost:54164',)]
                  
                  # Same result by using 127.0.0.1 instead of localhost: 
                  >>> engine = create_engine(
                        "mysql+pymysql://sylvain:passwd@127.0.0.1/db?host=localhost?port=3306")
                  >>> conn = engine.connect()
                  >>> conn.execute("SELECT host FROM INFORMATION_SCHEMA.PROCESSLIST WHERE ID = CONNECTION_ID()").fetchall()
                  [('localhost:54164',)]
                  
                  # Alternatively, using connect_args:
                  >>> engine = create_engine("mysql+pymysql://sylvain:passwd@localhost/db",
                                         connect_args= dict(host='localhost', port=3306))
                  >>> conn = engine.connect()
                  >>> conn.execute("SELECT host FROM INFORMATION_SCHEMA.PROCESSLIST WHERE ID = CONNECTION_ID()").fetchall()
                  [('localhost:54353',)]
                  

                  As you noticed, both will use a TCP connection (I know that because of the port number after the hostname). On the other hand:

                  >>> engine = create_engine(
                        "mysql+pymysql://sylvain:passwd@localhost/db?unix_socket=/path/to/mysql.sock")
                  #                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                  #                               Specify the path to mysql.sock in
                  #                               the `unix_socket` option will force
                  #                               usage of a UNIX socket
                  
                  >>> conn = engine.connect()
                  >>> conn.execute("SELECT host FROM INFORMATION_SCHEMA.PROCESSLIST WHERE ID = CONNECTION_ID()").fetchall()
                  [('localhost',)]
                  
                  # Same result by using 127.0.0.1 instead of localhost: 
                  >>> engine = create_engine(
                        "mysql+pymysql://sylvain:passwd@127.0.0.1/db?unix_socket=/path/to/mysql.sock")
                  >>> conn = engine.connect()
                  >>> conn.execute("SELECT host FROM INFORMATION_SCHEMA.PROCESSLIST WHERE ID = CONNECTION_ID()").fetchall()
                  [('localhost',)]
                  
                  # Alternatively, using connect_args:
                  >>> engine = create_engine("mysql+pymysql://sylvain:passwd@localhost/db",
                                         connect_args= dict(unix_socket="/path/to/mysql.sock"))
                  >>> conn = engine.connect()
                  >>> conn.execute("SELECT host FROM INFORMATION_SCHEMA.PROCESSLIST WHERE ID = CONNECTION_ID()").fetchall()
                  [('localhost',)]
                  

                  No port after the hostname: this is an UNIX socket.

                  这篇关于如何使用Python+SQLAlchemy远程连接MySQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:无法使用 Node.js 和 Tedious 连接到 SQL Server 下一篇:目标主体名称不正确.无法生成 SSPI 上下文

                  相关文章

                2. <legend id='TRPgO'><style id='TRPgO'><dir id='TRPgO'><q id='TRPgO'></q></dir></style></legend>
                  <i id='TRPgO'><tr id='TRPgO'><dt id='TRPgO'><q id='TRPgO'><span id='TRPgO'><b id='TRPgO'><form id='TRPgO'><ins id='TRPgO'></ins><ul id='TRPgO'></ul><sub id='TRPgO'></sub></form><legend id='TRPgO'></legend><bdo id='TRPgO'><pre id='TRPgO'><center id='TRPgO'></center></pre></bdo></b><th id='TRPgO'></th></span></q></dt></tr></i><div id='TRPgO'><tfoot id='TRPgO'></tfoot><dl id='TRPgO'><fieldset id='TRPgO'></fieldset></dl></div>
                    <bdo id='TRPgO'></bdo><ul id='TRPgO'></ul>

                  <small id='TRPgO'></small><noframes id='TRPgO'>

                  <tfoot id='TRPgO'></tfoot>