Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 54 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save brandonsimpson/5204ce8a46f7a20071b5 to your computer and use it in GitHub Desktop.
Save brandonsimpson/5204ce8a46f7a20071b5 to your computer and use it in GitHub Desktop.
OSX How To: Uninstall native MySQL and install MariaDB via Homebrew

OSX How To: Uninstall native MySQL and install MariaDB via Homebrew

This is a short overview on how to completely remove any old mysql server installs in OSX and upgrade to MariaDB without conflicts. Things can get a bit weird when you have various old installs of MySQL server floating around, and utilizing homebrew to install and upgrade MariaDB as a drop in replacement for MySQL has worked well, especially if you're used to managing MySQL installs via yum in linux.

First: Backup Your Data!

Backup all of your current databases with mysqldump

This isn't a tutorial on backups, and there are many ways to do it. You should know how to backup your data anyway. For this example, we'll do a full backup of our InnoDB databases.

$ mysqldump -uroot -p --all-databases --flush-logs --master-data=2 > all_databases.sql

Second: Backup Your MySQL User Permissions

Within the msyqldump above, make sure you have dumped the mysql.users table. Either do this, or dump your mysql table data independently, or make note of your existing mysql user permissions if you just want to manually add them to your clean new MariaDB server later. If you have a lot of different permissions setup, you will probably want to migrate them. If you don't have many db users, you may want to add them manually later so you can get a refresher of how your user permissions are setup. Either way, you should understand your user permissions before removing your old database.

Dump your existing mysql table:

$ mysqldump -uroot -p mysql > mysql.sql

After installing your new MariaDB server, you can restore the mysql database by executing:

$ mysql -uroot -p mysql < mysql.sql

then make sure to flush privileges for the new user permissions to take effect:

FLUSH PRIVILEGES

Remove MySQL

You are now ready to remove everywhere MySQL has put itself into your OSX system. You may opt to move these folders to a backup folder instead of removing them, but for our case, we want to remove MySQL completely so there are no conflicts later.

First, check where mysql is running on your system:

$ which mysql

You should see something like: /usr/local/mysql

Now, stop the database server:

$ sudo /usr/local/mysql/support-files/mysql.server stop

And move/backup or remove the following folders/files:

  • sudo rm /usr/local/mysql
  • sudo rm -rf /usr/local/mysql*
  • sudo rm -rf /Library/StartupItems/MySQLCOM
  • sudo rm -rf /Library/PreferencePanes/My*
  • rm -rf ~/Library/PreferencePanes/My*
  • edit /etc/hostconfig and remove the line MYSQLCOM=-YES-

These may not exist, but if they do, remove them to avoid any conflicts with other versions of MySQL:

  • sudo rm -rf /Library/Receipts/mysql*
  • sudo rm -rf /Library/Receipts/MySQL*
  • sudo rm -rf /private/var/db/receipts/*mysql*

Install MariaDB with Homebrew

First, make sure you have homebrew installed and running, and make sure everything is up to date:

$ brew update

You can then run searches (brew search mariadb) , and if you would like to get more information about it, you could do: brew info mariadb. When you're ready to install MariaDB, simply execute:

$ brew install mariadb

After homebrew installs MariaDB server, follow the onscreen instructions on how to install the database (mysql_install_db) and add your startup scripts if you want MariaDB to load on boot. You should also run mysql_secure_installation to lock down your install and set your root password.

You should now be able to login to mysql with your new root user and see the MariaDB message from the server:

$ mysql -uroot -p


Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 48
Server version: 10.0.14-MariaDB Homebrew

You can now import your mysql users and dumped databases file as needed.

@Varun006
Copy link

Varun006 commented Jul 29, 2017

Hi,

I am getting the below error when i install mariadb using brew.

WARNING: The host 'Varuns-MacBook-Pro.local' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MariaDB version. The MariaDB daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MariaDB privileges !
chown: /usr/local/var/mysql: Operation not permitted
Cannot change ownership of the database directories to the 'varundavda'
user.  Check that you have the necessary permissions and try again.

READ THIS: https://docs.brew.sh/Troubleshooting.html

These open issues may also help:
kitchen-sync bottle with mariadb - Library not loaded https://github.com/Homebrew/homebrew-core/issues/13680

Also, when i run

mysql -root

I get the following error.

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

I just have no idea, regarding what is wrong.

@maindufeu
Copy link

maindufeu commented Aug 1, 2017

Hi, I want to install MariaDB without homebrew But I had already install MariaDB with brew, someone knows how to uninstall?

I followed the steps above to install it and it worked, then I tried to install MariaDB from the web and it brokes down. Now I can't use it either

@mscheffer
Copy link

also getting ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

@nouhabel
Copy link

is Mysql service started in your device?

@10001oleg
Copy link

me to => Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

**ck this manual!

@Juliamd
Copy link

Juliamd commented Apr 1, 2018

Hi,

I have trouble uninstalling mariadb 10.2.14 on my macOS High Sierra Version 10.13.4 and I don't know coding. Please help. Thank you.

@elhardoum
Copy link

@10001oleg it's probably because the mysql daemon didn't start yet. To do so run brew services start mariadb (or brew services start mysql for those who installed mysql client instead of mariadb client)

@elhardoum
Copy link

elhardoum commented Apr 4, 2018

@Juliamd try this:

brew services stop mariadb
brew uninstall mariadb --force

@tiwiex
Copy link

tiwiex commented Jul 28, 2018

working on installing this on my OS X Sierra. It was usually a routine install. It is quite weird now. It installs but I can't log in. I login as mysql but there is no table for user so there is really no root account anywhere. I have information_schema and test tables.

What could be going on here?
Thanks

@dzpt
Copy link

dzpt commented Jun 10, 2019

❯ sudo /usr/local/mysql/support-files/mysql.server stop
Password:
ERROR! MySQL server PID file could not be found!

@nikmanzotti
Copy link

also getting ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

Same problem, in my case I had this clue in the installation output:

==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

And the actual path was /usr/local/etc/my.cnf

@brandonsimpson
Copy link
Author

Interesting to see everyone is still struggling with this 6 years later. I'd recommend not doing anything natively and simply spin up a docker container. Good luck!

https://hub.docker.com/_/mysql
https://hub.docker.com/_/mariadb

@nikmanzotti
Copy link

nikmanzotti commented Mar 24, 2020

Interesting to see everyone is still struggling with this 6 years later. I'd recommend not doing anything natively and simply spin up a docker container. Good luck!

https://hub.docker.com/_/mysql
https://hub.docker.com/_/mariadb

True story, learning Docker is on my to-do list!

Maybe it's helpful for others, I'll post here the solution that worked for me.

ISSUE AND DEBUGGING
My issue was due to a previous Homebrew MySQL installation. I came up with a solution thanks to this post, the problem wasn't with (or wasn't just with) my.cnf.

This post-install step was failing with no useful hint on the true problem:

shell> /usr/local/Cellar/mariadb/10.4.11/bin/mysql -u root mysql
mysql> show tables;`

Trying to run mysql -u root mysql, I received the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

This post suggested that it was because MySQL is installed but not yet running. It seemed to be true, as trying a mysql.server start I received this:

Starting MariaDB
.200324 00:22:41 mysqld_safe Logging to '/usr/local/var/mysql/MacBook-Pro-7.err'.
200324 00:22:41 mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
/usr/local/bin/mysql.server: line 264: kill: (38000) - No such process
 ERROR!

I thus decided to take a look at the suggested log file, finding the following error repeating several times:
[ERROR] Could not open mysql.plugin table. Some plugins may be not loaded

Through an internet research of this error, I found the previously mentioned post and implemented the solution provided below. In the post, people with similar problems were saying that to solve it they had to delete some log files (in a different folder compared to the one I deleted, but they weren't using homebrew).

PROBLEM SOLUTION

  1. brew uninstall mariadb [uninstall MariaDB]
  2. rm -rf /usr/local/var/mysql/ [remove mysql log folder]
  3. rm -rf /usr/local/etc/my.cnf [remove mysql global option files]
  4. rm -rf /usr/local/etc/my.cnf.d [remove mysql global option files]
  5. rm -rf /usr/local/etc/my.cnf.default [remove mysql global option files]
  6. brew install mariadb [install MariaDB]

Not sure if steps 3 to 5 (removing my.cnf and related) are needed, as I got this message even with the successful installation :

==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

But post-install was correctly completed during installation, and everything seems to be working fine now!

@juliorosseti
Copy link

Interesting to see everyone is still struggling with this 6 years later. I'd recommend not doing anything natively and simply spin up a docker container. Good luck!
https://hub.docker.com/_/mysql
https://hub.docker.com/_/mariadb

True story, learning Docker is on my to-do list!

Maybe it's helpful for others, I'll post here the solution that worked for me.

ISSUE AND DEBUGGING
My issue was due to a previous Homebrew MySQL installation. I came up with a solution thanks to this post, the problem wasn't with (or wasn't just with) my.cnf.

This post-install step was failing with no useful hint on the true problem:

shell> /usr/local/Cellar/mariadb/10.4.11/bin/mysql -u root mysql
mysql> show tables;`

Trying to run mysql -u root mysql, I received the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

This post suggested that it was because MySQL is installed but not yet running. It seemed to be true, as trying a mysql.server start I received this:

Starting MariaDB
.200324 00:22:41 mysqld_safe Logging to '/usr/local/var/mysql/MacBook-Pro-7.err'.
200324 00:22:41 mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
/usr/local/bin/mysql.server: line 264: kill: (38000) - No such process
 ERROR!

I thus decided to take a look at the suggested log file, finding the following error repeating several times:
[ERROR] Could not open mysql.plugin table. Some plugins may be not loaded

Through an internet research of this error, I found the previously mentioned post and implemented the solution provided below. In the post, people with similar problems were saying that to solve it they had to delete some log files (in a different folder compared to the one I deleted, but they weren't using homebrew).

PROBLEM SOLUTION

  1. brew uninstall mariadb [uninstall MariaDB]
  2. rm -rf /usr/local/var/mysql/ [remove mysql log folder]
  3. rm -rf /usr/local/etc/my.cnf [remove mysql global option files]
  4. rm -rf /usr/local/etc/my.cnf.d [remove mysql global option files]
  5. rm -rf /usr/local/etc/my.cnf.default [remove mysql global option files]
  6. brew install mariadb [install MariaDB]

Not sure if steps 3 to 5 (removing my.cnf and related) is needed, as I got this message even with the successful installation :

==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

But post-install was correctly completed during installation, and everything seems to be working fine now!

Tks man! Saved my hole life!

@emiliaanoldz
Copy link

Thank you @nikmanzotti that worked for me!

@patrickoliveras
Copy link

patrickoliveras commented Jul 14, 2020

Thanks @juliorosetti! I was missing the removal of /usr/local/var/mysql/

mysql_install_db was getting stuck on some permissions issues

@mreinar
Copy link

mreinar commented Apr 14, 2021

@nikmanzotti so many TNX! Works, at least, after 28348784 re-re-un-rm-fuck-install)

@Luuk34
Copy link

Luuk34 commented May 8, 2021

In a good manual, you also explain how to UNDO "Remove MySQL". If you do not do that, you should not have this chapter in your manual.

@AroraShreshth
Copy link

Thanks a ton, @nikmanzotti your solution worked after scratching my head for hours on this.

@ibnusulaiman83
Copy link

@nikmanzotti thanks you so much brader. After following many sources from google your solutions is da best one

@jjjarden
Copy link

@nikmanzotti thanks a lot, your solution worked for me

@ferume-oh3
Copy link

@nikmanzotti thanks a lot!

@benwoodward
Copy link

thank you @nikmanzotti, saved me hours of banging my head against a wall!

@diegosepulveda
Copy link

@nikmanzotti thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment