gpt4 book ai didi

mysql - 无法从 Django 连接到远程 MySQL 数据库

转载 作者:可可西里 更新时间:2023-11-01 08:20:00 25 4
gpt4 key购买 nike

我在从 Django 连接到远程 MySQL 服务器时遇到问题。该设置使用多个数据库:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'ldatabase', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'luser',
'PASSWORD': 'lpass',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
},
'physics': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'rdatabase', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'ruser',
'PASSWORD': 'rpass',
'HOST': 'hostname', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '3306', # Set to empty string for default.
}
}

默认数据库在本地主机(又名客户端)机器上运行,物理在远程服务器上。

我可以成功连接到远程 MySQL 服务器:

> mysql -u ruser -prpass -h hostname
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 34714
Server version: 5.5.29 MySQL Community Server (GPL)
...

甚至来自 python

> python
Python 2.7.3 (default, Aug 9 2012, 17:23:57)
[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2
...
>>> import MySQLdb
>>> con = MySQLdb.connect(host='hostname', port=3306, user='ruser', passwd='rpass', db='rdatabase')
>>> cursor = con.cursor()
>>> cursor.execute('select * from rdatabase.labs')
425L

但是来自 Django 的连接失败:

Exception Value: (2003, "Can't connect to MySQL server on 'hostname' (13)")

事实上,我已经在我的笔记本电脑上测试了这个设置:笔记本电脑是客户端,远程 MySQL 服务器具有相同的设置。它工作正常。

请帮忙解决这个问题,因为我已经没有想法了。也许在我缺少的客户端机器上有某种隐藏的 Django 设置?

在下面找到客户端和服务器配置。

谢谢。

客户端设置

> python
Python 2.7.3 (default, Aug 9 2012, 17:23:57)
[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2
...

> pip list
Django (1.5.1)
MySQL-python (1.2.4)
...

> uname --all
Linux <hostname> 3.8.9-200.fc18.x86_64 #1 SMP Fri Apr 26 12:50:07 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

> mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 31
Server version: 5.5.31 MySQL Community Server (GPL)

服务器设置

> uname --all
Linux <hostname> 3.6.10-2.fc16.x86_64 #1 SMP Tue Dec 11 18:55:03 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

> mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 34712
Server version: 5.5.29 MySQL Community Server (GPL)

> cat /etc/mysql.cnf
[mysqld]
datadir =/var/lib/mysql
socket =/var/lib/mysql/mysql.sock
#user =mysql

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#default-character-set=utf8
max_allowed_packet = 1048576000
[mysqld_safe]
log-error =/var/log/mysqld.log
pid-file =/var/run/mysqld/mysqld.pid

[client]
#default-character-set=utf8

最佳答案

问题是 SELinux 在 Fedora 18 中默认阻止 Apache 脚本连接到网络数据库,如下所述:

Weird MySQL Python mod_wsgi Can't connect to MySQL server on 'localhost' (49) problem

解决方案是通过以下方式启用 httpd_can_network_connect:

sudo setsebool -P httpd_can_network_connect_db on

关于mysql - 无法从 Django 连接到远程 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16527611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com