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.

  1. Download:
wget https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python_2.1.8-1debian9_all.deb
  1. Install:
sudo dpkg -i mysql-connector-python_2.1.8-1debian9_all.deb

Thats it!

Answer from driconmax on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › invalid syntax issue with python mysql script...
r/learnpython on Reddit: Invalid Syntax issue with python mysql script...
September 1, 2023 -

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()

🌐
Arch Linux Forums
bbs.archlinux.org › viewtopic.php
[SOLVED] Mysql python connector not working. / Programming & Scripting / Arch Linux Forums
MariaDB [(none)]> show databasess; ... python Python 3.10.9 (main, Dec 19 2022, 17:35:49) [GCC 12.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import mysql.connector as pol >>> my = pol.connect(host="localhost",user="root",passwd="110820051234" database="lol") File "<stdin>", line 1 my = pol.connect(host="localhost",user="root",passwd="110820051234" database="lol") ^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: invalid ...
🌐
Reddit
reddit.com › r/mysql › python can not import mysql
r/mysql on Reddit: Python can not import mysql
June 22, 2022 - For anyone facing this issue today.. in command prompt or whatever u used to install (idk im a newbie) instead of pip install i tried pip3 install mysql-connector-python and then imported mysql.connector in python and it worked then. Earlier it wasn't as I only used pip install.
🌐
sebhastian
sebhastian.com › no-module-named-mysql
Python ModuleNotFoundError: No module named 'mysql' | sebhastian
February 28, 2023 - You can also install the MySQL Connector/Python library through other distributions as listed here. Once the installation is finished, try to run the script with the python3 command again. It should work this time. Sometimes, you may also encounter a new error saying invalid syntax as follows: python main.py Traceback (most recent call last): File "main.py", line 1, in <module> import mysql.connector #..... f"This connection is using {tls_version} which is now " ^ SyntaxError: invalid syntax
🌐
MySQL
dev.mysql.com › doc › connector-python › en › connector-python-api-errors-error.html
MySQL :: MySQL Connector/Python Developer Guide :: 10.12.2 errors.Error Exception
MySQL Connector/Python Developer Guide / ... / Connector/Python API Reference / Errors and Exceptions / errors.Error Exception · This exception is the base class for all other exceptions in the errors module. It can be used to catch all errors in a single except statement. The following example shows how we could catch syntax errors: import mysql.connector try: cnx = mysql.connector.connect(user='scott', database='employees') cursor = cnx.cursor() cursor.execute("SELECT * FORM employees") # Syntax error in query cnx.close() except mysql.connector.Error as err: print("Something went wrong: {}".format(err))
🌐
Stack Overflow
stackoverflow.com › questions › 33105272 › mysql-connector-in-centos-7 › 33107532
python - MySQL connector in centOS 7 - Stack Overflow
it looks like something is not right when you install the mysql-connector-python package. Try follow my edited answer below to find out whether the mysql python package is in the sys.path.
🌐
Python Forum
python-forum.io › thread-24828.html
Syntax error - Python Forum
Hi. Im studing. I need to help. I will connect mysql from python in vscode. Im write cmd pip install mysql-connector. And im write vscode terminal pip install mysql-connector. And im write : import mysql.connector mydb = mysql.connector.connect( ...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 34229786 › mysql-connector-python-syntax-error
Mysql connector Python Syntax error - Stack Overflow
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 ''' (id INT NOT NULL AUTO_INCREMENT,cash INT,dt DATE, PRIMARY KEY(id))' at line 1
🌐
Launchpad
answers.launchpad.net › myconnpy › +question › 224997
Question #224997 “Python 2 and Python 3 on the same maschine ins...” : Questions : MySQL Connector/Python
But with Python 3 i have an error: >>> import mysql.connector Traceback (most recent call last): File "<stdin>", line 1, in <module> File "./mysql/
🌐
Python.org
discuss.python.org › python help
Mysql.connector issue - Python Help - Discussions on Python.org
June 23, 2021 - I am trying to connect mysql(8.0) and python(3.8) using mysql.connector but getting the error: mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES) Windows 10 same code working for pymysql the code used: import mysql.connector as sql s=sql.connect(host=‘localhost’,user=‘root’,passwd=‘12345’)
🌐
Stack Overflow
stackoverflow.com › questions › 37039782 › python3-5-refuse-to-work-with-mysql-connector
python - Python3.5 Refuse to Work with MySQL Connector - Stack Overflow
Your non-standard Python3 path suggests you did not get Python3 from a CentOS repository, right? ... Anzel/Ray - Thanks for the lead. Your input is much appreciated!! ... I've figured it out. Anzel was right about the fact that it's installed globally in the default python2.6 directory. Here's how I fixed it: Launch the default python interpreter, in my case "python" -> python2.6, and run the commands below to see where "MySQL.connector" was installed. >>> from distutils.sysconfig import get_python_lib >>> print get_python_lib () /usr/lib/python2.6/site-packages >>>
🌐
MySQL
dev.mysql.com › doc › connector-python › en › connector-python-api-errors-programmingerror.html
MySQL :: MySQL Connector/Python Developer Guide :: 10.12.11 errors.ProgrammingError Exception
MySQL Connector/Python Developer Guide / ... / Connector/Python API Reference / Errors and Exceptions / errors.ProgrammingError Exception · This exception is raised on programming errors, for example when you have a syntax error in your SQL or a table was not found.
🌐
The Geek Stuff
thegeekstuff.com › 2016 › 06 › mysql-connector-python
How to Connect to MySQL Database from Python With Example
June 13, 2016 - If you get the following error, then something is wrong with the Python MySQL library installation. >>> import mysql.connector ImportError: No module named mysql.connector
🌐
Stack Overflow
stackoverflow.com › questions › 73028901 › cannot-import-mysql-connector-in-python-2-7-12
importerror - Cannot import mysql.connector in Python 2.7.12 - Stack Overflow
>>> import mysql.connector Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib\site-packages\mysql\connector\__init__.py", line 53, in <module> from .connection import MySQLConnection File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 518 f"This connection is using {tls_version} which is now " ^ SyntaxError: invalid syntax