As @TimRoberts and @Dhivakar Chelladurai said in te comments, the solution is to find a version of MySQL-Connector-Python for Python2 or Python3.5 max as this is the last version that Debian 9 has for Python3.
- Download:
wget https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python_2.1.8-1debian9_all.deb
- Install:
sudo dpkg -i mysql-connector-python_2.1.8-1debian9_all.deb
Thats it!
Answer from driconmax on Stack OverflowI'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()
Edited for some formatting oddities...
what version of python are you running?
I was facing the similar issue. My env details - Python 2.7.11 pip 9.0.1 CentOS release 5.11 (Final)
Error on python interpreter -
>>> import mysql.connector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mysql.connector
>>>
Use pip to search the available module -
$ pip search mysql-connector | grep --color mysql-connector-python
mysql-connector-python-rf (2.2.2) - MySQL driver written in Python
mysql-connector-python (2.0.4) - MySQL driver written in Python
Install the mysql-connector-python-rf -
$ pip install mysql-connector-python-rf
Verify
$ python
Python 2.7.11 (default, Apr 26 2016, 13:18:56)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>>
Thanks =)
For python3 and later use the next command: $ pip3 install mysql-connector-python-rf
Use
pip3 install mysql-connector
to install the python packaged (if you are using Python 3. For Python 2 you can use pip).
I was facing the similar issue. My env details - Python 2.7.11 pip 9.0.1 CentOS release 5.11 (Final)
Error on python interpreter -
>>> import mysql.connector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mysql.connector
>>>
Use pip to search the available module -
$ pip search mysql-connector | grep --color mysql-connector-python
mysql-connector-python-rf (2.2.2) - MySQL driver written in Python
mysql-connector-python (2.0.4) - MySQL driver written in Python
Install the mysql-connector-python-rf -
$ pip install mysql-connector-python-rf
Verify
$ python
Python 2.7.11 (default, Apr 26 2016, 13:18:56)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>>
Depending on your python version and how you installed it, it is possible that mysql connector isn't installed, you can install it with pip
To install mysql connector:
pip install mysql-connector-python
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
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