Python MySQLdb error - python

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;

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.

Read table from MySQL - Python

I have MySQL ( Database: items, Table: issuelist )
I want to read and print out by python... but when I run code as below, it show error.
import mysql.connector
#from mysql.connector import Error
mydb = mysql.connector.connect(
host="localhost",
#user="yourusername",
#passwd="yourpassword",
database="items"
)
mycursor = mydb.cursor()
sql_statement = "SELECT * FROM issuelist"
mycursor.execute(sql_statement)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Traceback (most recent call last): File
"C:\Users\Administrator\Desktop\Chatbot - ReadData\ExcelMySQL.py",
line 8, in
database="items" File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector__init__.py",
line 219, in connect
return MySQLConnection(*args, **kwargs) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\connection.py",
line 104, in init
self.connect(**kwargs) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\abstracts.py",
line 960, in connect
self._open_connection() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\connection.py",
line 292, in _open_connection
self._ssl, self._conn_attrs) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\connection.py",
line 212, in _do_auth
self._auth_switch_request(username, password) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\connection.py",
line 256, in _auth_switch_request
raise errors.get_exception(packet) mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied
for user ''#'localhost' (using password: NO)
How to do resolve ?
Thanks you so much!
The error message is quite explanatory :
Access denied for user ''#'localhost' (using password: NO)
You can't just connect to mysql with no account...
The error msg clearly shows the mistake -- The connect info is not correct. That is ,in your code, make sure the info of the host,user,passwd and database are all correct?

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

connect to a distant data base using 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

OperationalError: (OperationalError) (2003, "Can't connect to MySQL server on '192.168.129.139' (111)") None None

I am trying to create a remote database using mysql on an Ubuntu machine running 12.04.
It has a root user with remote login enabled and no password.I have started the server.
output of
sudo netstat -tap | grep mysql
shows
tcp 0 0 localhost:mysql *:* LISTEN 13246/mysqld
I have created a database called nwtopology using (as mentioned root doesn't have a password yet.)
create database nwtopology
grant all privileges on *.* to root#192.168.129.221
FLUSH PRIVILEGES;
From the client machine that also runs Ubuntu 12.04 I use a python script to connect to the remote mysql database using sqlalchemy.
from pox.core import core
import pox.openflow.libopenflow_01 as of
import re
import datetime
import time
from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql.expression import exists
log = core.getLogger()
engine = create_engine('mysql://root#192.168.129.139/nwtopology', echo=False)
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()
class SourcetoPort(Base):
""""""
__tablename__ = 'source_to_port'
id = Column(Integer, primary_key=True)
port_no = Column(Integer)
src_address = Column(String,index=True)
#-----------------------------------------
def __init__(self, src_address,port_no):
""""""
self.src_address = src_address
self.port_no = port_no
The create_engine() call is failing with the following error.
POX 0.1.0 (betta) / Copyright 2011-2013 James McCauley, et al.
Traceback (most recent call last):
File "/home/karthik/ms_thesis/pox/pox/boot.py", line 89, in do_import2
__import__(name, globals(), locals())
File "/home/karthik/ms_thesis/pox/custom/tutorial.py", line 39, in <module>
Base.metadata.create_all(engine)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/schema.py", line 2515, in create_all
tables=tables)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2230, in _run_visitor
conn = self.contextual_connect(close_with_result=False)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2340, in contextual_connect
self.pool.connect(),
File "/usr/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 210, in connect
return _ConnectionFairy(self).checkout()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 371, in __init__
rec = self._connection_record = pool._do_get()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 697, in _do_get
con = self._create_connection()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 174, in _create_connection
return _ConnectionRecord(self)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 256, in __init__
self.connection = self.__connect()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 316, in __connect
connection = self.__pool._creator()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/strategies.py", line 80, in connect
return dialect.connect(*cargs, **cparams)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 280, in connect
return self.dbapi.connect(*cargs, **cparams)
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)
OperationalError: (OperationalError) (2003, "Can't connect to MySQL server on '192.168.129.139' (111)") None None
I cannot figure out why this is happening?Any help is greatly appreciated?
Looks like the mysql server is configured to listen only on localhost.
You can test this by running telnet 192.168.129.139 3306 from your client machine.
Most probable reason - mysqld (=MySQL daemon) is configured to do so.
Please try to follow Configuration step described here:
Edit the /etc/mysql/my.cnf file to configure MySQL to listen for connections from network hosts, change the bind-address directive to the server's IP address. For example, in your case, replace 192.168.0.5 with 192.168.129.139.
From:
bind-address = 192.168.0.5
to:
bind-address = 192.168.129.139
If there is no such entry and you cannot connect, create a new line.
You may also try commenting out the line instead.
After making a change to /etc/mysql/my.cnf the MySQL daemon will need to be restarted:
sudo systemctl restart mysql.service
Then test with telnet or by running your application once again. Also netstat would have second entry for mysqld.

Categories