connect to a distant data base using python - python

Running this command in the terminal with the correct values I get connected and can read the data base:
mysql -h mysql.lyon...fr -u user --password=passworld
I have to do this in a python script, I tried few functions like:
import MySQLdb as mysql
print '======================================================================================'
print 'Connextion to the database'
print '======================================================================================'
# mysql -h mysql.lyon....fr -u XXuser --password=XXpasswd
import MySQLdb as mdb
con = mdb.connect(host='mysql....grid5000.fr' , user='XXuser', passwd='XXpasswdread oar2')
======================================================================================
Connextion to the database
======================================================================================
Traceback (most recent call last):
File "process.py", line 14, in <module>
con = mdb.connect(host='mysql.lyon......fr' , user='user name ', passwd='......')
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'oarreader'#'flyon.lyon.grid5000.fr' (using password: YES)")
How can I define the arguments in the connect function to connect to the server knowing its address?

You are not using the same user that in the terminal, in the terminal you use -u user and using python you use oarreader

Related

Cannot connect Python to MySQL on a hosting. ERROR 1227 (42000), ERROR 1227 (42000)

I make a telegram bot using python and MySQL. I can enter the database in terminal, but when I run my python code, it says I have no permission.
ilyacherne#vh289 ~ $ python3 /home/i/ilyacherne/public_html/cgi-bin/bot.py
Traceback (most recent call last):
File "/home/i/ilyacherne/.local/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 239, in _open_connection
self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Access denied for user 'ilyacherne'#'77.222.61.25' (using password: YES)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/i/ilyacherne/public_html/cgi-bin/bot.py", line 3, in <module>
import config
File "/home/i/ilyacherne/public_html/cgi-bin/config.py", line 8, in <module>
database = mysql.connect(
File "/home/i/ilyacherne/.local/lib/python3.8/site-packages/mysql/connector/__init__.py", line 272, in connect
return CMySQLConnection(*args, **kwargs)
File "/home/i/ilyacherne/.local/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 85, in __init__
self.connect(**kwargs)
File "/home/i/ilyacherne/.local/lib/python3.8/site-packages/mysql/connector/abstracts.py", line 1009, in connect
self._open_connection()
File "/home/i/ilyacherne/.local/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 241, in _open_connection
raise errors.get_mysql_exception(msg=exc.msg, errno=exc.errno,
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'ilyacherne'#'77.222.61.25' (using password: YES)
I checked the password, host name, database name and user.
I connect with this code:
database = mysql.connect(
host = '77.222.61.25',
user = 'ilyacherne',
passwd = 'CENSORED',
database = 'ilyacherne'
)
support said that MySql 5.7 connects to socket by default and i connect just with 'sudo mysql', and then they reccomended me try this:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'Current-Root-Password'; FLUSH PRIVILEGES;
but it says:
ERROR 1227 (42000): Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation
ERROR 1227 (42000): Access denied; you need (at least one of) the RELOAD privilege(s) for this operation
Pip install mysql-connector-python
Then import mysql-connector.

Python can't connect to MySQL db: OperationalError

When in the Python shell I type:
import MySQLdb
Con = MySQLdb.Connect(user='testuser', password='password', db='test')
And I get this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Nickey\AppData\Local\Programs\Python\Python36\lib\site-packages\MySQLdb\__init__.py", line 86, in Connect
return Connection(*args, **kwargs)
File "C:\Users\Nickey\AppData\Local\Programs\Python\Python36\lib\site-packages\MySQLdb\connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2059, <NULL>)
And no description of the error at all. Please, I am waiting for help. Thanks.
UPDATE: In case somebody will be having this problem, it appears to be connected with the new mysql_auth_plugin, with its new Strong Password Encryption for Authentication. I just switched to old_password_plugin(Legacy Authentication Method) in the configuration of MySQL Server and now [All works!].
According to the docs, password is not a keyword argument like you entered, instead it's passwd as you can see --> here

PeeWee PostgreSQL Database creation: does not exist

I'm attempting to create a PostgreSQL database using PeeWee. Upon connecting I get the following error:
File "peewee_test.py", line 44, in
psql_db.connect()
File "c:\python27\lib\site-packages\peewee.py", line 3602, in connect
self.initialize_connection(self._local.conn)
File "c:\python27\lib\site-packages\peewee.py", line 3514, in exit
reraise(new_type, new_type(*exc_args), traceback)
File "c:\python27\lib\site-packages\peewee.py", line 3600, in connect
**self.connect_kwargs)
File "c:\python27\lib\site-packages\playhouse\postgres_ext.py", line 385, in _connect
conn = super(PostgresqlExtDatabase, self)._connect(database, **kwargs)
File "c:\python27\lib\site-packages\peewee.py", line 3990, in _connect
conn = psycopg2.connect(database=database, **kwargs)
File "c:\python27\lib\site-packages\psycopg2__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
peewee.OperationalError: FATAL: database "test" does not exist
Any idea what I am doing wrong?
from peewee import *
from playhouse.postgres_ext import PostgresqlExtDatabase
psql_db = PostgresqlExtDatabase(
'test', # Required by Peewee.
user='xxxxx', # Will be passed directly to psycopg2.
password='xxxxx', # Ditto.
host='', # Ditto.
port='5432'
)
psql_db.connect() # error occurs here
psql_db.create_tables([Person, Pet])
Peewee wont create the database for you, you need to first connect to the database with psql shell for example and admin user access psql --host HOST --port 5432 --username YOUR_USER -W -d postgres and run CREATE DATABASE test;
Postgres has also a helper createdb executable:
createdb my_db

Python MySQLdb error

When I run my script it won't connect to mysql database, I can't find my host name from godaddy I just tried mysq.mydomain.com and mydomain.com. the login details for the database are correct.
Traceback (most recent call last):
File "mysql.py", line 6, in <module>
conn = MySQLdb.connect(host="?",user="?",passwd="?",db="?")
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'official_autotes'#'104.156.228.189' (using password: YES)")
------------------
my code
from threading import Thread
import urllib
import re
import MySQLdb
conn = MySQLdb.connect(host="?",user="?",passwd="?",db="?")
query = "INSERT INTO tutorial (symbol) values ('AAPL')"
x = conn.cursor()
x.execute(query)
row = x.fetchal()
Grand privilage to the user and ip trying to access the MySQL table using the Python code. To do the same, execute the following commands in the mysql shell.
GRANT <privileges> ON database.* TO 'user'#'ip' IDENTIFIED BY 'password';
flush privileges;

python mysql connecting error

>>> db=MySQLdb.connect(passwd="king123",db="thangs",host="localhost",port=3306,user="user")
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
db=MySQLdb.connect(passwd="king123",db="thangs",host="localhost",port=3061,user="user")
File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")
I am running it on right port , still it give me error. Can anyone give me any idea please
If you can't see this port that means mysql is not running in which case the error makes sense. Please see this on how to turn on the mysql server.

Categories