I have an application accessing a distant mysql base using PySQL and the following fonction
def db_query(username, password, host, db, query):
try:
db = pymysql.connect(host=host, user=username, passwd=password, db=db)
except pymysql.err.OperationalError as e:
errorLog = e
return [0, errorLog]
# you must create a Cursor object. It will let you execute all the query you need
cur = db.cursor()
cur.execute(query)
result = cur.fetchall()
cur.close()
db.close()
return result
I have recently update Django from 1.7.1 to 1.7.3. Before update code worked. Now, when I'm in production using WSGI and only then (not on local dev server, nor on server dev server) I get a (2003, "Can't connect to MySQL server on [server.address] ([Errno 13] Permission denied)")
I have updated pymysql to latest version available on pip.
I see that 1.7.3 correcting some security issue in WSGI, i tried to downgrade to 1.7.2 (only by doing pip install django==1.7.2 (not sure this is the good way)) and the issue is still present.
Any idea on what i could try to check ?
Thank you in advance for your help.
I think this is related to apache permission to access MySQL database. Can you try command:
setsebool -P httpd_can_network_connect_db 1
Using -P option makes the change persistent. Without this option, the boolean value would be reset to 0 at reboot.
Related
I am new to Python and mySQL and I am trying to get the following Python script running as a CGI on an hosted webserver:
#!/usr/bin/python3
import cgi
import sys
sys.path.append("PATH TO SIDE PACKAGES DUE TO MISSING ROOT ACCESS")
import mysql.connector
from mysql.connector import Error
print('Content-type: text/html') # the mime-type header.
print() # header must be separated from body by 1 empty line.
try:
connection_config_dict = {
'user': 'MY_USER',
'password': 'MY_PASSWORD',
'host': 'rdbms.strato.de',
'database': 'MY_DATABASE',
}
connection = mysql.connector.connect(**connection_config_dict)
if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = connection.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("Your connected to database: ", record)
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
Unfortunately, this results in the following error message:
Error while connecting to MySQL 2055: Lost connection to MySQL server at 'rdbms.strato.de:3306',
system error: 104 Connection reset by peer
I looked up older threads with the error message and while I could find both separately, I did not find them in this combo, therefore I hope there might be someone having an idea for a solution before I deal with the horrible customer service of my provider again. To me as a newbie it seems like the server declines the connection, even though I am coming from inside the network (running as a CGI on the server).
Some more information about the environment: The Python version is 3.8.1, mySQL server version 5.7, mysql_connector_python version 8.0.23. Since it is a hosted webserver I do not have root access, therefore I installed additional modules with pip --user via ssh and added the path to the script which works fine with other modules like requests. I am also certain that the mySQL login data is correct, since it works flawlessly with a PHP script.
Thanks in advance for helping out!
I just setup a VPS with a Mysql Database on it. I'm able to log into the database in the SSH terminal with mysql -uroot -p however, the python file shown below is unable to form a connection, and errors with Access denied for root#localhost (using password: YES). I'm confused as to why this is happening. I'm 100% sure my password is correct. I'm using mysql.connector
Python file:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="pwd",
database="db"
)
mycursor = mydb.cursor()
sql = "INSERT INTO test (tests) VALUES (%s)"
val = ("testing")
mycursor.execute(sql, val)
mydb.commit()
I'm a bit new to this DB and VPS stuff, so please bear with me if I don't understand on the first go.
Thanks!
In addition to #don's answer
I assume it has something to do with the dns not properly resolving localhost. I have experienced that before, but outside of just mapping localhost to 127.0.01 in the host's file,
This can also be fixed by changing a value in your /etc/mysql/my.cnf file.
It's likely your bind-address line is
bind-address = localhost
You can try changing it to
bind-address = 127.0.0.1
You can also set it to 0.0.0.0 but this will expose the connection to the outside world (inc. public IP), so this is highly not recommended but will solve the problem.
I have a LAMP server and then I installed MySQLdb for my Python scripts. Now I can't access the MySQL (from LAMP) from Python scripts because it isn't connecting to the MySQLdb, and also I can't access the MySQLdb with phpMyAdmin with (root root). I got "#2002 Cannot log in to the MySQL server" error. Is it possible to connect to one db with Python and phpMyAdmin?
Here is my Python code, which can't connect to the LAMP MySQL, but can connect to the MySQLdb:
db = MySQLdb.connect(host="localhost", port=3303, user="root", passwd="rootroot", db="test")
cursor = db.cursor()
sql = "CREATE TABLE TT(ID int NOT NULL AUTO_INCREMENT PRIMARY KEY)"
cursor.execute(sql)
db.commit()
db.close()
If you're getting #2002 Cannot log in to the MySQL server when logging in to phpmyadmin, then edit phpmyadmin/config.inc.php file and change:
$cfg['Servers'][$i]['host'] = 'localhost';
to:
$cfg['Servers'][$i]['host'] = '127.0.0.1';
You might want to visit this link:
http://blog.ryantremaine.com/2011/03/2002-cannot-log-in-to-mysql-server.html
UPDATE:
you wud not have configured your php.ini well and so that it cannot connect to mysql server.
Wrong path for the mysql.sock
mysql.sock is the instance of the mysql, so first you have to find where does it place at.
You may find it at "/tmp/mysql.sock" or "/var/mysql/mysql.sock".
Go to your php.ini and make sure the value for "pdo_mysql.default_socket", "mysql.default_socket", "mysqli.default_socket" is the right path.
Then restart your web server and try again.
ELSE
Try this:
Go to config.inc.php and check for the following line:
$cfg['Servers'][$i]['user'] = 'YOUR USER NAME IS HERE';
$cfg['Servers'][$i]['password'] = 'AND YOU PASSWORD IS HERE';
Check whether the user name and password that you gave is present or not
Hi i have a requirement where i need to connect to remote mysql server. My application shall be running on local machine and my mysql will be running on remote server.I have tried the following code:
DB = 'gts'
DB_HOST = 'ps95074.dreamhost.com'
DB_USER = 'root'
DB_PASSWORD = 'dbadminpassword'
conn = MySQLdb.Connection(db=DB, host=DB_HOST, user=DB_USER,passwd=DB_PASSWORD)
cursor = conn.cursor()
But i am getting the following error
OperationalError: (2005, "Unknown MySQL server host 'ps95074.dreamhost.com' (1)")
Instead if i use
DB_HOST='localhost'
Everything works fine. How can same be possible with remote host.Any help shall be appreciated.
Check your firewall. That server is online and available from any machines:
> mysql -h ps95074.dreamhost.com
ERROR 1045 (28000): Access denied for user 'myuser'#'myhost' (using password: NO)
However, even if you can connect chances are good that your database user only allows local connections.
Update: I just tried it again and now it also fails using the commandline client. So clearly something is wrong with your server.
There doesn't seem to be any great instructions for setting this up. Does anyone have any good instructions? I am a linux noob so be gentle. I did see another post that is similar, but no real answer.
I have a couple of problems.
FreeTDS doesn't "seem" to be working. I am trying to connect and I get the following message using the "tsql" command: "Default database being set to databaseName
There was a problem connecting to the server" but it doesn't mention what the problem is.
The error I get when I try to connect using pyodbc is: "pyodbc.Error: ('08S01', '[08S01] [unixODBC][FreeTDS][SQL Server]Unable to connect: Adaptive Server is unavailable or does not exist (20009) (SQLDriverConnectW)')"
I tried something similar with pymssql, but I ran into similar issues. I keep getting errors that I can't connect, but it doesn't tell me why.
The following works if you configure the MS SQL server to allow remote TCP/IP connections and have an appropriate user to connect as.
You also need to be careful to set up the correct hostname for the db as reported by MS SQL.
import pymssql
connection = pymssql.connect(
user = 'username',
password = 'password',
host = 'server',
database = 'database',
)
cursor = connection.cursor()
cursor.execute('select * from db;')
rows = cursor.fetchall()
When building FreeTDS (http://www.freetds.org/userguide/config.htm):
./configure --with-tdsver=8.0 --enable-msdblib
That error suggests that the TDS version is not set right. You can set that in the configuration setting for FreeTDS. You don't mention which MSSQL version you are using. But, if you are using say 2005, setting 8.0 as the TDS version will work.