>>> pip instal mysql-connector-python - this >>> is python interactive shell.
you must execute pip command from command prompt/terminal, e.g. start cmd on Windows and type pip install mysql-connector-python on the prompt, e.g.
c:\>pip install mysql-connector-python
Run pip install mysql-connector from your Terminal, as the error handler suggests. Or try to run the same command from wherever you've already tried, but without the -python at the end.
Click here! Use MySQLdb instead, i used it and it's working fine. Method is in the blog. Hope it will help you
Python can not import mysql
database - f"MySQL Connector/Python C Extension not available ({exc})" SyntaxError: invalid syntax - Stack Overflow
MySQL connector fails after switching from Python 2.7.X to 3.X - Stack Overflow
importerror - Cannot import mysql.connector in Python 2.7.12 - Stack Overflow
I had to intall the python3 packages of mysql-connector-python.
On my Mac I did the following:
- brew install python3
- pip3 install --allow-external mysql-connector-python mysql-connector-python
After that is worked. So use pip3.
On Windows with Python 3.5, I did something very close to toom's answer, that is :
pip3.5 install --allow-external mysql-connector-python mysql-connector-python
I'm attempting to get a listing of all users and their permissions, specific to each DB on our local CentOS 7.5 mysql system. I was given the python script below by a co-worker assisting me with this project.
It's complaining about this quote character, but I can't find any information on what's wrong with it let alone what it's supposed to be.
> python sqlpermissions.py
File "sqlpermissions.py", line 20
cursor.execute(f"USE '{db}'")
^
SyntaxError: invalid syntax
sqlpermissions.py
---------------------------------------------
import mysql.connector
# Replace these with your actual MySQL credentials
mysql_config = {
'user': '***',
'password': '***',
'host': 'localhost',
}
# Connect to MySQL
conn = mysql.connector.connect(**mysql_config)
cursor = conn.cursor()
# Get a list of databases
cursor.execute("SHOW DATABASES")
databases = [db[0] for db in cursor.fetchall()]
# Loop through databases and users
for db in databases:
cursor.execute(f"USE '{db}'")
cursor.execute("SELECT DISTINCT user, host FROM mysql.user")
users = cursor.fetchall()
for user, host in users:
cursor.execute(f"SHOW GRANTS FOR '{user}'@'{host}'")
user_grants = cursor.fetchall()
print(f"Database: {db}, User: {user}@{host}")
for grant in user_grants:
print(grant[0])
print('-' * 50)
# Close the connection
cursor.close()
conn.close()
Finally figured out what was my problem.
python-mysql.connector was not a py3 package and apt-get nor aptitude was proposing such version.
I managed to install it with pip3 which was not so simple on ubuntu 12.04 because it's only bundled with ubuntu starting at 12.10 and the package does not have the same name under pip...
vfi@ubuntu:$sudo apt-get install python3-setuptools vfi@ubuntu:$sudo easy_install3 pip vfi@ubuntu:$ pip --version pip 1.4.1 from /usr/local/lib/python3.2/dist-packages/pip-1.4.1-py3.2.egg (python 3.2) vfi@ubuntu:$sudo pip install mysql-connector-python
This worked for me on CentOS 6.5
sudo yum install mysql-connector-python