Python: How to Solve mysqlclient Install Error in Mac

It’s finally solved after many hardships. Please refer to the notes of Niu Ren and record it

Install brew

First, install brew and Baidu. There are many tutorials

Install MySQL

#Install
brew install mysql

#Configure environment variables
echo'export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH"' >> ~/.bash_profile

#Make environment variables take effect
source ~/.bash_profile

#Start mysql service through script
mysql.server start

#Start mysql and set to boot
brew services start mysql

#Initialization, set password
mysql_secure_installation

ya..... bin% mysql_secure_installation
Enter password:

Securing the MySQL server deployment.


VALIDATE PASSWORD PLUGIN can be used to test passwords //Password verification plug-in, in order to improve security, you need to verify the password
and improve security. It checks the strength of password // It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin? //Prompt to install the password verification plugin

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy: //Three levels of password validation policy

LOW Length >= 8 //The minimum length is greater than or equal to 8 characters
MEDIUM Length >= 8, numeric, mixed case, and special characters //Numbers, letters, and special characters are mixed, the specific ones should be at least 1 number, 1 letter, 1 special character, and the length should not exceed 32 characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file // The most stringent, plus, the dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2 //Here I choose 2 MEDIUM
Using existing password for root.

Estimated strength of the password: 50 //Here is also the rating of password strength
Change the password for root ?((Press y|Y for Yes, any other key for No): y

New password: //Password

Re-enter new password:

Estimated strength of the password: 50
Do you wish to continue with the password provided? (Press y|Y for Yes, any other key for No): y //Prompt to use the password you just entered?
 ... Failed! Error: Your password does not satisfy the current policy requirements

New password: //Password

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No): y
By default, a MySQL installation has an anonymous user, //By default, MySQL has an anonymous user,
allowing anyone to log into MySQL without having to have //This anonymous user does not have to be created for them by a user. An anonymous user allows anyone to log into MySQL,
a user account created for them. This is intended only for //This is just to facilitate test use
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production //When using in a formal environment, it is recommended that you remove them
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No): y //Prompt to remove anonymous users
Success.

Normally, root should only be allowed to connect from //Under normal circumstances, root users are only allowed to log in using "localhost",
'localhost'. This ensures that someone cannot guess at // to ensure that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No): n //

 ... skipping.
By default, MySQL comes with a database named'test' that //By default, there is a test library in the MySQL database that can be accessed by any user.
anyone can access. This is also intended only for testing, //This is also intended only for testing
and should be removed before moving into a production // in a formal environment, should be removed
environment.

Remove test database and access to it?(Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes                       //Refresh the permission table to ensure that all modifications can take effect immediately
made so far will take effect immediately.

Reload privilege tables now?(Press y|Y for Yes, any other key for No) : y
Success.

All done! 

Install mysql-connector-c

brew install mysql-connector-c

Installing Xcode-

xcode-select --install

Install OpenSSL

brew install openssl

After installation, the terminal will display the method of configuring environment variables

If you need to have openssl@3 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@3 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"

Unlink MySQL and the linked MySQL connector mysql-connector-c

brew unlink mysql
brew link --overwrite mysql-connector-c

As shown in the figure, mysql-connector-c has a warning
warning: MySQL client is keg only and must be linked with -- force.
execute
brew link -- overwrite mysql-connector-c -- force

Install mysqlclient

Using PIP install mysqlclient will not cause any errors

Connect MySQL again

Do the opposite of unlinking MySQL and the linked MySQL connector mysql-connector-c:

brew unlink mysql-connector-c
brew link --overwrite mysql --force

Read More: