Connect to MySQL server for client failed - python

I'm writing an application to upload data for my customer's database. When I tested it on my PC, it works ok but when connecting to the client's database, it has some errors. I'm using this code to connect to MySQL:
def connect_sql(self):
mydb = mysql.connector.connect(
host=host,
passwd=pw,
port=port,
database=db,
user=user,
)
return mydb
When tried to connect to the database on the client's PC, it shows this error:
1115 (42000): Unknown character set: 'utf8mb4'
I've noticed that this error maybe because different database server version between me and my client. While he uses 5.0.67-community-nt (which does not support 'utf8mb4') and I use 10.0.21 MariaDB-log.
Is there any way to fix this error on my site (maybe export .sql file to set the default character set to prevent this error, etc..), or do I need to ask my customer to update his MySQL database's version?
Thanks for any help!!!

Try it as following:
def connect_sql(self):
mydb = mysql.connector.connect(
host=host,
passwd=pw,
port=port,
database=db,
user=user,
charset='utf8',
use_unicode=True
)
return mydb
Here's documentation.

Related

trying to run this command to create a database with mysql but i get a "SQL Syntax error"

This is the error:
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1
Here is the code that I use to create the database:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd = "password12",
)
my_cursor = mydb.cursor()
my_cursor.execute("CREATE DATABASE database")
my_cursor.execute("SHOW DATABASES")
for db in my_cursor:
print(db)
I tried using a different name for the database, but I still got the same error.
You have an extraneous comma after your password parameter. Change your mydb connection string to the following:
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
password = 'password12'
)
my_cursor = mydb.cursor()
my_cursor.execute("CREATE DATABASE database")
my_cursor.execute("SHOW DATABASES")
for db in my_cursor:
print(db)
You should use the word password over passwd. Even though they're synonymous, according to the MySQL documentation here: Connector/Python Connection Arguments
An asterisk (*) following an argument indicates a synonymous argument
name, available only for compatibility with other Python MySQL
drivers. Oracle recommends not to use these alternative names.
Argument Name
Default
Description
user (username*)
The user name used to authenticate with the MySQL server.
password (passwd*)
The password to authenticate the user with the MySQL server.
password1, password2, and password3
For Multi-Factor Authentication (MFA); password1 is an alias for password. Added in 8.0.28.
database (db*)
The database name to use when connecting with the MySQL server.
host
127.0.0.1
The host name or IP address of the MySQL server.
Also, as mentioned in the comments above, you should not use the word database as your actual database name. I understand you may be doing it as an example, but its worth noting that it is a Reserved Keyword.

Cannot connect to MySQL in Python program

I am trying to establish a connection with a MySQL database in a python program
import mysql.connector
cnx = mysql.connector.connect(
host='localhost',
user='admin',
password='admin1234',
database='meu_banco'
)
cnx.close()
print(cnx)
but keep getting the following error:
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'admin'#'localhost' (using password: YES)
Im am pretty sure that the username is admin and that I've setted 'admin1234' as the password. Anyone knows what might be happening?
Notes:
-I've also tried to use 'passwd' and 'db' instead of 'password' and 'database' keyword arguments.
-I'm using MySQL that comes with XAMPP version 8.0.2 / PHP 8.0.2 for Linux.
You are connecting to the database and afterwards you are closing the connection.
Only use cnx.close() when you want to close the database connection, it may cause an error.
If that's not the problem, you may have the user wrong, try to use the default username that MySQL Workbench has, it is root.

How to specifies database name in pyhdb

import pyhdb
connect = pyhdb.connect(
host="example.com",
port=30015,
user="user",
password="secret"
)
From the official explanation
Pyhdb only gives four parameters. Without the parameter database name, I can't understand how the system knows which database you want to connect to in this case?
And when i connect in this way, Program error:"pyhdb.exceptions.DatabaseError: authentication failed",
it looks like my password is wrong, so i let friends use JAVA(jdbc) to connect with four parameters,
it failed too, but if he add database name, it worked! so my parameters is right , and question is how to specify database name in pyhdb?
Or there are other ways to connect to Hana, Thankyou!
Looking at the __init__.py file of the pyhdb package shows that DATABASENAME is not supported when creating a connection:
[...]
def connect(host, port, user, password, autocommit=False):
conn = Connection(host, port, user, password, autocommit)
conn.connect()
return conn
[...]
The good news here is that pyhdb is not what you should be using to connect to HANA anyhow as it is the old and unsupported client library.
Use hdbcli instead as described in the documentation.
With hdbcli it's no problem at all to use the DATABASENAME:
from hdbcli import dbapi
connection =dbapi.connect(address="hxehost", port=39013, databasename="HXE", user="xxxxx", password="xxxxx")
cursor = connection.cursor()
cursor.execute("SELECT 'Hello, Python world' FROM DUMMY")
print(cursor.fetchone())
connection.close()

PyMySQL using localhost vs socket incoherant behaviour

I am using PyMySQL to connect to a database running on localhost. I can access the database just fine using the username/password combiunation in both the command line and adminer so the database does not appear to be the probem here.
My code is as follow. However, when using the host="127.0.0.1" options, I get an OperationalError and an Errno 111. Using the same code, but connecting via the socket Mariadb runs on is fine.
import pymysql.cursors
from pprint import pprint
# This causes an OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")
# connection = pymysql.connect(
# host="127.0.0.1",
# port=3306,
# user="root",
# password="S3kr37",
# db="my_test",
# )
# This works.
connection = pymysql.connect(
user="root",
password="S3kr37",
db="my_test",
unix_socket="/var/lib/mysql/mysql.sock"
)
try:
with connection.cursor() as cursor:
sql = "select * from MySuperTable"
cursor.execute(sql)
results = cursor.fetchall()
pprint(results)
finally:
connection.close()
What am I doing wrong?
PS: Note that this question has the same problem but the solution offered is the socket. That is no good enough: I want to know why I cannot use the hostname as the documentation suggests.
Errorcode 2003 (CR_CONN_HOST_ERROR) is returned by the client library, in case the client wasn't able to establish a tcp connection to the server.
First you should check, if you can connect via telnet or mysql command line client to your server.
If not, check the server configuration file:
does the server run on port 3306?
is IPv4 disabled?
is skip-networking enabled?
is bind-address activated (with another IP?

Connection to external MySQL instance using Python

I have tried the following code:
MySQLdb.connect(host='xxx', port=3306, user='yyy')
But I get the following error:
(2005, "Unknown MySQL server host ...
I have tried to remove all firewall restrictions on the external MySQL instance, as a test. I am able to connect to the instance from my developing machine.
I believe this should be possible now that the App Engine supports sockets, or am I wrong?
I think this connection is not allowed (no external socket support in MySQLdb) :
https://developers.google.com/appengine/docs/python/cloud-sql/?hl=en#Python_Using_a_local_MySQL_instance_during_development
You have to use localhost/127.0.0.1 or CloudSQL socket (unix_socket='/cloudsql/'):
if (os.getenv('SERVER_SOFTWARE') and
os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):
db = MySQLdb.connect(unix_socket='/cloudsql/' + _INSTANCE_NAME, db='guestbook', user='root')
else:
db = MySQLdb.connect(host='127.0.0.1', port=3306, user='root')
# Alternately, connect to a Google Cloud SQL instance using:
# db = MySQLdb.connect(host='ip-address-of-google-cloud-sql-instance', port=3306, user='root')

Categories

Resources