Install mysql client mac m1

Yes, I had the same problem when I tried to do pip install mysqlclient on my MacBook Pro 2021 running Monterey 12.0.1 with an Apple Silicon M1 chip.

I used a solution by Github user jcbloch found in the discussion in the link https://github.com/PyMySQL/mysqlclient/issues/496 which I found after reading the https://github.com/PyMySQL/mysqlclient/issues/497 link that I found in sathira lamal's answer.

The problem, as I learned from reading jcbloch's explanation, is with the Python that currently comes with the MacBook Pro 2021 with Monterey 12.0.1. That Python is built for x86_64 architecture. The mysqlclient library however has an arm64 version available, which is what you are going to get by default if you do something like brew install mysql-client.

Using the file command shows the issue:

$ file /Library/Frameworks/Python.framework/Versions/2.7/bin/python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python: Mach-O 64-bit executable x86_64

The text above shows that python is x86_64.

$ file /opt/homebrew/opt/mysql-client/lib/libmysqlclient.21.dylib 
/opt/homebrew/opt/mysql-client/lib/libmysqlclient.21.dylib: Mach-O 64-bit dynamically linked shared library arm64

The text above shows that libmysqlclient.21.dylib is arm64.

To work around this, Github user jcbloch created an alternative Homebrew installation that is set to use x86_64 architecture, used it to install an x86_64 version of the MySQL client library, then used that library when installing the MySQL client into Python via pip.

Here is what I had to do, which is basically the same thing jcbloch suggests.

What differs is that I made sure to include an uninstall of any borked mysqlclient python package and to drop all the cached pip libraries so that you don't just end up reinstalling the cached incorrect package again. And, I ended up with a slightly later version of MySQL client than jcbloch did. And, I had to include an LDFLAGS variable to point to the x86_64 openssl library.

Make sure to double check all the paths below before executing these commands. I got derailed for many hours because I had copied and pasted the wrong version of the mysql library in my last command.

cd /opt
mkdir homebrew-x86 
curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew-x86
arch -x86_64 homebrew-x86/bin/brew install 
rm -rf ~/Library/Caches/pip
pip uninstall mysqlclient
export PATH="/opt/homebrew-x86/opt//bin:$PATH"
CPPFLAGS="-I/opt/homebrew-x86/opt//include" LDFLAGS="-L/opt/homebrew-x86/opt//lib" MYSQLCLIENT_CFLAGS="-I/opt/homebrew-x86/Cellar//5.7.32/include/mysql" MYSQLCLIENT_LDFLAGS="-L/opt/homebrew-x86/Cellar//5.7.32/lib -lmysqlclient" arch -x86_64 pip install -I -vvv mysqlclient

And then, again as jcbloch suggests, I verified the installation by checking that the mysql_affected_rows symbol is present. I guess that symbol only exists in the x86_64 architecture version of the MySQL client.

$ sudo symbols homebrew-x86/Cellar//5.7.34/lib/libmysqlclient.dylib -arch x86_64 | grep mysql_affected_rows
       0x0000000000003fe7 (     0xd) mysql_affected_rows [FUNC, EXT, NameNList, MangledNameNList, Merged, NList, FunctionStarts]

Anyone found the solution? I am struggling with this problem
I have installed native m1 python 3.9, project working perfectly with sqlite but when I switch database to mysql although I have downloaded mysql client
I get below error
(venv) (base) salishkumar@Salishs-MacBook-Pro tiffinwala % python3 manage.py makemigrations
Traceback (most recent call last):
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/MySQLdb/init.py", line 18, in
from . import _mysql
ImportError: dlopen(/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/MySQLdb/_mysql.cpython-39-darwin.so, 0x0002): symbol not found in flat namespace '_mysql_affected_rows'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/salishkumar/backend/tiffinwala/manage.py", line 22, in
main()
File "/Users/salishkumar/backend/tiffinwala/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/core/management/init.py", line 425, in execute_from_command_line
utility.execute()
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/core/management/init.py", line 401, in execute
django.setup()
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/init.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/apps/config.py", line 300, in import_models
self.models_module = import_module(models_module_name)
File "/Users/salishkumar/opt/anaconda3/lib/python3.9/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1030, in _gcd_import
File "", line 1007, in _find_and_load
File "", line 986, in _find_and_load_unlocked
File "", line 680, in _load_unlocked
File "", line 850, in exec_module
File "", line 228, in _call_with_frames_removed
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/contrib/auth/models.py", line 3, in
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/contrib/auth/base_user.py", line 47, in
class AbstractBaseUser(models.Model):
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/db/models/base.py", line 122, in new
new_class.add_to_class('_meta', Options(meta, app_label))
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/db/models/base.py", line 326, in add_to_class
value.contribute_to_class(cls, name)
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/db/models/options.py", line 207, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/utils/connection.py", line 15, in getattr
return getattr(self._connections[self._alias], item)
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/utils/connection.py", line 62, in getitem
conn = self.create_connection(alias)
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/db/utils.py", line 204, in create_connection
backend = load_backend(db['ENGINE'])
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/db/utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "/Users/salishkumar/opt/anaconda3/lib/python3.9/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 15, in
import MySQLdb as Database
File "/Users/salishkumar/backend/tiffinwala/venv/lib/python3.9/site-packages/MySQLdb/init.py", line 24, in
version_info, _mysql.version_info, _mysql.file
NameError: name '_mysql' is not defined

How do I install MySQL client on Mac?

Download the package from http://dev.mysql.com/downloads/shell/..
Double-click the downloaded DMG to mount it. Finder opens..
Double-click the . pkg file shown in the Finder window..
Follow the steps in the installation wizard..
When the installer finishes, eject the DMG. (It can be deleted.).

Is MySQL available for M1 Mac?

Yes (8.0 [homebrew]) Through homebrew, mysqld can be installed with native m1 support.

How do I download MySQL on M1 Mac?

Downloads are available at https://dev.mysql.com/downloads/workbench/. To install MySQL Workbench on macOS, download the file. Double-click the downloaded file to open the installation window shown in the figure that follows. Drag the MySQL Workbench icon onto the Applications icon as instructed.

How do I start MySQL client on Mac?

To enable the launchd service, you can either:.
Open macOS system preferences and select the MySQL preference panel, and then execute Start MySQL Server. ... .
Or, manually load the launchd file. ... .
To configure MySQL to automatically start at bootup, you can: $> sudo launchctl load -w com..