简体   繁体   English

用Python保护MySQL连接

[英]Secure MySQL Connection in Python

I have a MYSQL database with users table, and I want to make a python application which allows me to login to that database with the IP, pass, username and everything hidden. 我有一个带有users表的MYSQL数据库,我想制作一个python应用程序,该应用程序允许我使用IP,密码,用户名和所有隐藏内容登录该数据库。 The thing is, the only IP which is allowed to connect to that mysql database, is the server itself (localhost). 问题是,唯一允许连接到该mysql数据库的IP是服务器本身(本地主机)。

How do I make a connection to that database from a user's computer, and also be able to retrieve data from it securely? 如何从用户的计算机建立到该数据库的连接,又能够安全地从中检索数据? Can I build some PHP script on the server that is able to take parameters and retrieve data to that user? 我可以在服务器上构建一些PHP脚本来获取参数并向该用户检索数据吗?

You should not make a connection from the user's computer. 您不应从用户的计算机建立连接。 By default, most database configurations are done to allow only requests from the same server ( localhost ) to access the database. 默认情况下,大多数数据库配置都已完成,仅允许来自同一服务器( localhost )的请求访问数据库。
What you will need is this: 您将需要的是:

  • A server side script such as Python, PHP, Perl, Ruby, etc to access the database. 服务器端脚本,例如Python,PHP,Perl,Ruby等,用于访问数据库。 The script will be on the server, and as such, it will access the database locally 该脚本将在服务器上,因此它将在本地访问数据库
  • Send a web request from the user's computer using Python, Perl, or any programming language to the server side script as described above. 如上所述,使用Python,Perl或任何编程语言将来自用户计算机的Web请求发送到服务器端脚本。
  • So, the application on the user's computer sends a request to the script on the server. 因此,用户计算机上的应用程序将请求发送到服务器上的脚本。 The script connects to the database locally, accesses the data, and sends it back to the application. 该脚本在本地连接到数据库,访问数据,然后将其发送回应用程序。 The application can then use the data as needed. 然后,应用程序可以根据需要使用数据。

That is basically, what you are trying to achieve. 基本上,这就是您要实现的目标。

Hope the explanation is clear and it helps. 希望解释清楚并有所帮助。

You can use below code into your python script to connect your application with remote mysql database. 您可以在Python脚本中使用以下代码将应用程序与远程mysql数据库连接。

import MySQLdb
myDB = MySQLdb.connect(host="x.x.x.x", port=3306, user="**********", passwd="*****************")

You need to install mysql-python to connect your python application with mysql database. 您需要安装mysql-python才能将python应用程序与mysql数据库连接。

As i understood you are able to connect only with "server itself (localhost)" so to connect from any ip do this: mysql> CREATE USER 'myname'@'%.mydomain.com' IDENTIFIED BY 'mypass'; 据我了解,您只能与“服务器本身(本地主机)”连接,以便从任何IP连接:mysql> CREATE USER'myname'@'%.mydomain.com'IDENTIFIED BY'mypass'; I agree with @Daniel no PHP script needed... 我同意@Daniel,不需要PHP脚本...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM