nameerror: name _mysql is not defined error occurs while connecting python end application with MySQL DB due to incompatibility or client/ connector unavailability. Typically There is a process to connect any SQL DB with Python, Here we need a connector that fulfills the requirement of request routing, connection development, and driver, etc. Specific to this error, if you get this error (name _mysql is not defined) you should check the root cause in the mentioned order. Because we will start with the most common scenarios with the rare and complex ones. If you change the order you may require extra time to fix it. Well, let’s, deep dive, into the solution.
nameerror: name _mysql is not defined ( Possible causes and Fixes ) –
Since this error is generic so we made the solution stepwise. Please follow the same.
Step 1: Validate the Connection Credential :
Firstly and the easiest thing which we should focus on is verifying the connection credentials. If we make any mistake we encounter this error. The default Connection Credential is below. Please double-check if you have modified anything into it.
Step 2: Reinstalling the MySQL :
Secondly, As we mentioned check if we have installed the connector and server properly. If we are not sure we should reinstall them to remove all the incompatibility.
Approach 1: Using pip to install mysql-
pip install mysql
Approach 2: Using conda to install mysql –
conda install -c conda-forge mysql
Approach 3: Using source code to install mysql –
Finally Here are some sets of commands to run and get MySQL to install.
wget https://github.com/farcepest/MySQL-python/tarball/master -O MySQL-python.tar.gz
tar -xvf MySQL-python.tar.gz
sudo python setup.py install
Step 3: Platform-dependent Libraries :
Most Importantly, There are some dependent libraries in which Operating System needs to run a python application with MySQL Database. If the other solution is not working then try the below command.
Case 1: For Linux-Based OS –
sudo apt-get install libmysqlclient-dev
Case 2: For MAC-Based OS –
brew install mysql-connector-c
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.