SELECT command denied to user (even though I have all privileges) - python

I host on godaddy.com and after multiple calls they ensured me that I have done right everything when it comes to the database setup. The account "python" has all necessary rights to fulfil its purpose.
import mysql.connector as sql
mydb = sql.connect(
host = "***.***.***.***",
user = "python",
passwd = "*******",
db = "main"
)
print(mydb)
sql_select_Query = "select * from wp_posts.wp_content"
cursor = mydb.cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()
print(cursor.rowcount)
On phpmyadmin Im not able to do anything. I can't grant any rights. When I execute the code, the following error occurs.
<mysql.connector.connection.MySQLConnection object at 0x7fddb0053610>
Traceback (most recent call last):
File "Untitled 2.py", line 12, in <module>
cursor.execute(sql_select_Query)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/cursor.py", line 551, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/connection.py", line 490, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/connection.py", line 395, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1142 (42000): SELECT command denied to user 'python'#'p579F3618.dip0.t-ipconnect.de' for table 'wp_content'
The connection is established, but the database doesn't grant any access. I have the right to login, but nothing more.
Solutions I tried, but that failed:
godaddy support is clueless (they say it should work perfectly)
granting me rights through phpmyadmin (don't have any privilege to do that)
added my IP address into the Remote Hosting Option in cPanel (now im able to establish a connection, but nothing more)
Maybe helpful:
On phpmyadmin im automatically logged in into an account that ends with #localhost. This account has the select privilege, but I can't access its password and therefore can't use it in python.

Related

Python and SQL Server - _pymssql.ProgrammingError: (208, b"Invalid object name X. DB-Lib error message 20018, severity 16 ..)

I want to fetch data from a MSSQL Server (Microsoft SQL Server 2017) via Python (3.8.6) and the pymssql library (2.2.7).
The tables are in the dbo Schema of database A, the default database of the user is master.
By using the following code, I am able to fetch data from most of the tables:
import pymssql
# Connect to the database - dont mind the fake server, pw and other creds, database='A'
conn = pymssql.connect(
server='localhost',
user='sa',
password='1234',
database='A',
port=8888
)
# Create a cursor
cursor = conn.cursor()
# Execute a SELECT statement++++
c = cursor.execute("SELECT TOP 10 * FROM dbo.KHKBookings")
results = cursor.fetchall()
# Iterate over the results and print each row
for row in results:
print(row)
# Close the cursor and connection
cursor.close()
conn.close()
While it works for the example table KHKBookings, it does not for another one called icZGBookings, which is in the same database and also in dbo schema. I get the following error:
Traceback (most recent call last): File "src\pymssql_pymssql.pyx",
line 459, in pymssql._pymssql.Cursor.execute File
"src\pymssql_mssql.pyx", line 1087, in
pymssql._mssql.MSSQLConnection.execute_query File
"src\pymssql_mssql.pyx", line 1118, in
pymssql._mssql.MSSQLConnection.execute_query File
"src\pymssql_mssql.pyx", line 1251, in
pymssql._mssql.MSSQLConnection.format_and_run_query File
"src\pymssql_mssql.pyx", line 1789, in
pymssql._mssql.check_cancel_and_raise File "src\pymssql_mssql.pyx",
line 1835, in pymssql._mssql.raise_MSSQLDatabaseException
pymssql._mssql.MSSQLDatabaseException: (208, b"Invalid object name
'dbo.icZGBookings'.DB-Lib error message 20018, severity 16:\nGeneral
SQL Server error: Check messages from the SQL Server\n")
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "", line 1, in
File "src\pymssql_pymssql.pyx", line 476, in
pymssql._pymssql.Cursor.execute pymssql._pymssql.ProgrammingError:
(208, b"Invalid object name 'dbo.icZGBookings'.DB-Lib error message
20018, severity 16:\nGeneral SQL Server error: Check messages from the
SQL Server\n")
So we are not finding the table, I can also make up a table and get the same error.
Adding the Database to the query didnt help either:
SELECT TOP 10 * FROM A.dbo.icZGBookings
But why is that so? I cant figure it out, because the table icZGBookings exists.
I can see and query it with the same user sa in MSSQL Server Management Studio in the database A and schema dbo.
I even altered the default database for that user from master to A, but that didnt help either.
We should not need to do that anyway because we specify the database we connect to in the beginning.
The master database dbo schema is empty anyway and should have not worked for the other tables too.
So, why cant I query it via Python?
I compared the values of the two tables with the following query:
SELECT *
FROM sys.tables AS t
INNER JOIN sys.partitions AS p
ON t.object_id = p.object_id
WHERE t.name in ('KHKBookings', 'icZGBookings')
The values are not different besides the object_ids, index_ids, and dates of course.
Any idea?
Thank you in advance!

What's the minimum amount of priviliges required to interact with a mysql database using python mysql connector?

I've created a custom user by executing:
mysql> CREATE USER 'myuser'#'localhost' IDENTIFIED BY 'mypwd';
mysql> GRANT select, update, alter ON mydb.* TO myuser#localhost;
My user only needs to read data from the database, update entries and change a few table's structure using the ALTER command.
Now I want to execute the following script:
import mysql.connector
db = mysql.connector.connect(
host="localhost",
user="myuser",
passwd="mypwd",
database="mydb")
print(db);
I don't fully understand why, but following error occurrs:
Traceback (most recent call last):
File "conneciton.py", line 3, in <module>
weewxDb = mysql.connector.connect(
File "/usr/lib/python3/dist-packages/mysql/connector/__init__.py", line 173, in connect
return MySQLConnection(*args, **kwargs)
File "/usr/lib/python3/dist-packages/mysql/connector/connection.py", line 102, in __init__
self.connect(**kwargs)
File "/usr/lib/python3/dist-packages/mysql/connector/abstracts.py", line 735, in connect
self._open_connection()
File "/usr/lib/python3/dist-packages/mysql/connector/connection.py", line 250, in _open_connection
self._do_auth(self._user, self._password,
File "/usr/lib/python3/dist-packages/mysql/connector/connection.py", line 172, in _do_auth
self._auth_switch_request(username, password)
File "/usr/lib/python3/dist-packages/mysql/connector/connection.py", line 216, in _auth_switch_request
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1044 (42000): Access denied for user 'myuser'#'localhost' to database 'mydb'
I think my user is missing some privileges. But which? I want to give him as few permissions as possible.

pymysql.err.InternalError: (1049, "Unknown database")

I want to connect MySQL RDS DB using python from raspberrypi.(i want to get seq from MySQL table 'face' using select query.)
and I have an error but i can not fix it.
This is rds mysql connection code:
import rds_config
import pymysql
rds_host = rds_config.rds_host
name = rds_config.rds_user
password = rds_config.rds_pwd
db_name = rds_config.rds_db
conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name,
connect_timeout=10)
with conn.cursor() as cur:
cur.execute("select seq from face")
conn.commit()
rds_config:
rds_host='rds endpoint'
rds_port=3306
rds_user='user'
rds_pwd='password'
rds_db='db name'
and This is traceback:
Traceback (most recent call last):
File "getRds.py", line 18, in <module>
conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=10)
File "/usr/local/lib/python2.7/dist-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 327, in __init__
self.connect()
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 598, in connect
self._request_authentication()
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 862, in _request_authentication
auth_packet = self._process_auth(plugin_name, auth_packet)
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 933, in _process_auth
pkt = self._read_packet()
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 683, in _read_packet
packet.check_error()
File "/usr/local/lib/python2.7/dist-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/usr/local/lib/python2.7/dist-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1049, u"Unknown database 'bsb-rds'")
i alread added ip address in vpc security group and public access is on.
it was possible to connect through mysql cli or workbench.
can anyone help me?
tl;dr you need to create bsb-rd. Execute this command: create database bsb-rds either with cur.execute() in python or in your favorite sql cli.
If you get this error, good news! You can connect to your RDS instance. This means you have the security group set up right, the host url, username, password and port are all correct! What this error is telling you is that your database bsb-rds does not exist on your RDS instance. If you have just made the RDS instance it is probably because you have not created the database yet. So create it!
Here are two ways to do this
mysql cli
In your terminal run
mysql --host=the_host_address user=your_user_name --password=your_password
Then inside the mysql shell execute
create database bsb-rd;
Now try your code again!
Python with pymysql
import rds_config
conn = pymysql.connect('hostname', user='username', passwd='password', connect_timeout=10)
with conn.cursor() as cur:
cur.execute('create database bsb-rd;')
I came to this page looking for a solution and didn't get it. I eventually found the answer and now share what helped me.
I ran into your exact issue and I believe I have the solution:
Context:
My tech stack looks like the following
Using AWS RDS (MySQL)
Using Flask Development on local host
RDS instance name: "test-rds"
Actual DB Name: test-database
Here is where my issue was and I believe your issue is in the same place:
You are using the AWS RDS NAME in your connection rather than using the true Database name.
Simply changing the DB name in my connection to the true DB name that I had setup via MySQL Workbench fixed the issue.
Other things things to note for readers:
Please ensure the following:
If you are connecting from outside your AWS VPC make sure you have public access enabled. This is a huge "gotcha". (Beware security risks)
Make sure your connection isn't being blocked by a NACL
Make sure your connection is allowed by a Security Group Rule

Python mysql connector change host link to ipaddress

I am coming across an issue with mysql connector.
I have an environment that i can't connect from my local machine.
I use simple command for connection:
from mysql.connector import connect
connection = mysql.connector.connect(user='dbuser', database='dbname',
host='amazon_link', password='dbpassword')
This works for all environment except one. And this also works in the server as well where the database lives.
I can access the database using my MySqlWorkbench from local machine. But when i try from my script, i get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/connection.py", line 95, in __init__
self.connect(**kwargs)
File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/abstracts.py", line 719, in connect
self._open_connection()
File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/connection.py", line 210, in _open_connection
self._ssl)
File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/connection.py", line 144, in _do_auth
self._auth_switch_request(username, password)
File "/Users/rajatvij/Development/env/lib/python2.7/site-packages/mysql/connector/connection.py", line 177, in _auth_switch_request
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'dbuser'#'10.0.1.72' (using password: YES)
And the ip address that i get for the host link is different from what mysql connector is showing here as well.
Is there a way to avoid mysql connector to change link to url here, as when i use the link instead of ip address in my Workbench i am able to connect. So i doubt it is an issue related to vpn or security groups. Otherwise i wouldn't be able to connect to database via work bench at all.
Any help would be appreciated. Thanks.
And sorry in case i missed something basic here.
Try to use the global IP address of your server instead of the domain name.
Refer the stackoverflow link for more details:
Remotely connect to MySQL with Python mysql.connector

Python mysql connector access denied error

I've read everything possible and I just can't connect to my mysql server. Here is my code. very simple. (I used xxx to hide all the private info. everything else is the way i got it.
import mysql.connector
conn = mysql.connector.connect(user='xxxx',password='xxxxx',host='xxxx',db='xxx',port=3306)
The login info is definitely correct. I tried it in a mysql client and it connected just fine. The MySQL server is hosted on dreamhost. I set the allowable IPs for the db user to wildcard %.%.%.% so anyone could connect.I don't know what else could possibly be wrong. When I try to connect in python i get:
C:\Python33\python.exe D:/Dropbox/python/v2/test.py
Traceback (most recent call last):
File "D:/Dropbox/python/v2/test.py", line 3, in <module>
conn = mysql.connector.connect(user='xxx',password='xxx',host='xxx',db='xxx',port=3306)
File "C:\Python33\lib\site-packages\mysql\connector\__init__.py", line 101, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 117, in __init__
self.connect(**kwargs)
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 383, in connect
self._open_connection()
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 350, in _open_connection
self._ssl)
File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 176, in _do_auth
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1044 (42000): Access denied for user 'xxx'#'%.%.%.%' to database 'xxx'
Any help would be greatly appreciated.
You will have to GRANT permissions to the user.
Something like:
GRANT ALL PRIVILEGES ON *.* TO 'user2'#'localhost' IDENTIFIED BY PASSWORD 'xxxxx'

Categories

Resources