How to connect to ProxySQL from application - python

I have Percona XtraDB Cluster running in 3 nodes (node1, node2, node3). I've configured ProxySQL in the 4th node (admin).
I have an python application code trying to access the cluster.
While connecting to 3306 port of node1, I'm able to connect.
import MySQLdb as mdb
db = mdb.connect(host="node1", port=3306,user="root", passwd="*****", db="percona")
In order to achieve load balancing, the application should point to the ProxySQL port 6032.
import MySQLdb as mdb
db = mdb.connect(host="admin", port=6032,user="admin", passwd="*****", db="percona")
While trying to connect, I'm getting the following error:
OperationalError: (1045, "ProxySQL Error: Access denied for user 'admin'#'' (using password: YES)")
I used the grant privilages command in the proxysql
grant all privileges on percona.* to 'admin'#'%' identified by password 'bullet';
Unfortunately, I got the following error,
ERROR 1045 (#2800): near "grant": syntax error
Correct me if I'm wrong.
Should I use some other configuration to connect to the Percona XtraDB Cluster?

Port 6032 is for the administrative CLI.
Instead, you would instead want to connect to port 6033 which listens to all traffic and does load balancing towards the backend PXC nodes.
Good luck!

Related

What does 'DPY-6001: cannot connect to database' mean with python-oracledb?

With Python code that uses the
python-oracledb driver:
import oracledb
import os
un = os.environ.get("PYTHON_USERNAME")
pw = os.environ.get("PYTHON_PASSWORD")
cs = "localhost/doesnotexist"
c = oracledb.connect(user=un, password=pw, dsn=cs)
what does this error message mean?
DPY-6001: cannot connect to database. Service "doesnotexist" is not registered with the listener at host "localhost" port 1521. (Similar to ORA-12514)
The error means that Python successfully reached a computer (in this case
"localhost" using the default port 1521) that is running a database. However
the database service you wanted ("doesnotexist") doesn't exist there.
Technically the error means the listener doesn't know about the service at the
moment. So you might also get this error if the DB is currently restarting.
This error is similar to the ORA-12514 error that you would see when connecting
with python-oracledb in Thick mode, or might see with some other Oracle tools.
The solution is to use a valid service name, for example:
cs = "localhost/xepdb1"
You can:
Check and fix any typos in the service name you used
Check the hostname and port are correct
Ask your DBA for the correct values
Wait a few moments and re-try in case the DB is restarting
Review the connection information in your cloud console or cloud wallet, if you are using a cloud DB
Run lsnrctl status on the database machine to find the known service names

Flask looks for db locally when it's on another server

I'm trying to setup my Flask application to work with a database hosted on a different server. My whole setup works, if i try to work with a simple PyMysql script i will be able to connect to the database, but when i try to do that from Flask i get any kind of problem.
I'm keeping my db configurations on config.py:
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://user:pass#external_ip/mydb'
But whenever i try to do a query, i will get the following error:
SELECT command denied to user 'user'#'local_ip'
So flask is looking for the db locally, for some reason, even though i set it to point to an external server. Can anyone help me out on this?
On the same environment, the following will connect and allow me to make queries:
connection = pymysql.connect(host='external_ip', user='user', password='pass', db='mydb', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
This error looks more like a db server error, more then a local flask client error.
If it was a Flask local error of inability to connect to the mysql server you should end up with something like:
Unable to connect to the server
Hostname unreachable
Connection refused (if you reach the server but with the wrong port, for instance)
Reading this error I guess that you have reached the server, but for that user + IP + Database combination you have no read permissions.
See the GRANT Statement doc for further details

Connecting to CloudSQL from Dataflow in Python

I'm trying to connect to CloudSQL with a python pipeline.
Actual situation
I can do it without any problem using DirectRunner
I can not connect using DataflowRunner
Connection function
def cloudSQL(input):
import pymysql
connection = pymysql.connect(host='<server ip>',
user='...',
password='...',
db='...')
cursor = connection.cursor()
cursor.execute("select ...")
connection.close()
result = cursor.fetchone()
if not (result is None):
yield input
The error
This is the error message using DataflowRunner
OperationalError: (2003, "Can't connect to MySQL server on '<server ip>' (timed out)")
CloudSQL
I have publicIP (to test from local with directrunner) and I have also trying to activating private IP to see if this could be the problem to connect with DataflowRunner
Option2
I have also tried with
connection = pymysql.connect((unix_socket='/cloudsql/' + <INSTANCE_CONNECTION_NAME>,
user='...',
password='...',
db='...')
With the error:
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 2] No such file or directory)")
Take a look at the Cloud SQL Proxy. It will create a local entrypoint (Unix socket or TCP port depending on what you configure) that will proxy and authenticate connections to your Cloud SQL instance.
You would have to mimic the implementation of JdbcIO.read() in Python as explained in this StackOverflow answer
With this solution I was able to access to CloudSQL.
For testing purpose you can add 0.0.0.0/0 to CloudSQL publicIP without using certificates
I created a example using Cloud SQL Proxy inside the Dataflow worker container, connection from the Python pipeline using Unix Sockets without need for SSL or IP authorization.
So the pipeline is able to connect to multiple Cloud SQL instances.
https://github.com/jccatrinck/dataflow-cloud-sql-python
There is a screenshot showing the log output showing the database tables as example.

Database connection failed for local MSSQL server with pymssql

I had been working with pyodbcfor database connection in windows envirnment and it is working fine but now I want to switch to pymssql so that it is easier to be deployed to Linux machine as well. But I am getting this error:
(20009, b'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (localhost:1433)\nNet-Lib error during Unknown error (10060)\n')
My connection code for using both pyodbc and pymssql is:
import pyodbc
import pymssql
def connectODSDB_1():
conn_str = (
r"Driver={SQL Server};"
r"Server=(local);"
r"Database=populatedSandbox;"
r"Trusted_Connection=yes;"
)
return pyodbc.connect(conn_str)
def connectODSDB_2():
server = '(local)'
database = 'populatedSandbox'
conn = pymssql.connect(server=server, database=database)
return conn
What could be the problem? And solution?
Well after browsing internet for a while, it seems pymssql needs TCP/IP be enabled for communication.
Open Sql Server Configuration Manager
Expand SQL Server Network Configuration
Click on Protocols for instance_name
Enable TCP/IP
I have faced the same issue while using RDS(AWS database instance). We should configured the inbound outbound rules.
Do following steps to configure.
Services->RDS->DB Instances -> Select DB-> Connectivity&Security
Under Security Section
VPC security groups -> click on security group
Change the inbound rules.
Check the source IP and change into anywhere or specific IP

Connect on remote MySQL database through Python

I have tried the following script but unfortunately doesn't work. I am using a free MySQL database provider. Any ideas?
import MySQLdb
myDB = MySQLdb.connect(host="208.11.220.249",port=3306,user="XXXXX",passwd="XXXXX",db="XXXXX")
cHandler = myDB.cursor()
cHandler.execute("SHOW DATABASES")
results = cHandler.fetchall()
for items in results:
print items[0]
Currently, I am getting the following error:
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1044, "Access denied for user 'XXXXX'#'%' to database 'XXXXX'")
GRANT ALL
ON *.*
TO user#192.168.39.17 -- client ip address
IDENTIFIED BY 'pwd';
Edit
This is SQL that you'd run on the database in order to ensure that the user has access to everything. pwd is the user's password.
Basically, this answer assumes that the connection issue is a credentials issue.
This is what I would do
See if the port is actually open on that machine.
On the machine you are connecting from, open console/cmd/terminal and see if you can connect using mysql -u XXXX -h 208.11.220.249 -p. If your mysql client can not connect, then there is no way you can connect using python
You don't have permission for connecting to database with this user.
Follow these steps:
1.try connecting to mysql DB:
mysql -h208.11.220.249 -uXXXXX -pXXXXX XXXXX
2.If you don't have permission for connecting to DB ,try creating user that has remote permission
GRANT ALL
ON DB.* -- Database name
TO user#ip -- client ip address
IDENTIFIED BY 'pwd';
3.on the last check my.cnf . "bind-address" must be 0.0.0.0 If you want to connect all remote addresses.
open your database in mysql and type the following :
mysql> GRANT ALL ON *.* TO root#'x' IDENTIFIED BY 'Your-password'
where x is the ip address of the user that wants to access your db.
use this link for more information:
How to Allow MySQL Client to Connect to Remote MySQL server

Categories