15

So I basically am installing mariadb with mysql on my mac using homebrew. These are the steps I made:

  • brew doctor -> worked
  • brew update -> worked
  • brew install mariadb -> worked
  • mysql_install_db -> Failed

    WARNING: The host 'Toms-MacBook-Pro.local' could not be looked up with /usr/local/Cellar/mariadb/10.4.6_1/bin/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 ! mysql.user table already exists!

Running mysql_upgrade afterwards gave me following error:

Version check failed. Got the following error when calling the 'mysql' command line client ERROR 1698 (28000): Access denied for user 'root'@'localhost' FATAL ERROR: Upgrade failed

I can't enter mysql like this:

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

but like this:

sudo mysql -u root

The user table returns this:

MariaDB [(none)]> USE mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> SELECT User, Host, plugin FROM mysql.user;
+---------------+-------------------------+-----------------------+
| User          | Host                    | plugin                |
+---------------+-------------------------+-----------------------+
| root          | localhost               | mysql_native_password |
| toms          | localhost               | mysql_native_password |
|               | localhost               |                       |
|               | toms-macbook-pro.local |                       |
+---------------+-------------------------+-----------------------+
4 rows in set (0.004 sec)
3
  • You don't need to run manually mysql_install_db. Try to brew remove mariadb and reinstall again. Commented Sep 5, 2019 at 11:19
  • @OrtomalaLokni the point is that I had this error earlier and reinstalled it, but it did not solve it, mariadb run automatically mysql_install_db right? (or something similiar)
    – junfo
    Commented Sep 5, 2019 at 11:58
  • mysql_install_db is run by the Homebrew's Mariadb install script at /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/mariadb.rb but it needs some specific parameters. Commented Sep 5, 2019 at 12:22

5 Answers 5

23

You could try to update the root password and access it afterwards

ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';

Exit Mysql and try to login

mysql -uroot -p # then use root as a password
1
  • 2
    How would one update the root password without being able to login as root? This is not a solution. The solution is first do the one below about running it from CLI as sudo (unix sockets) then run this.
    – kevinc
    Commented Feb 7, 2022 at 23:51
4

What is the issue?

  • Install MariaDB using brew, brew install [email protected].

  • Try to reset root password.

    • Method 1: mysqld_safe command

      • run command: brew services stop [email protected]
      • run command: mysqld_safe --skip-grant-tables --skip-networking
      • on a new terminal tab,
        run command for MariaDB <= 10.4: mysql_secure_installation
        run command for MariaDB >= 10.4 mariadb-secure-installation
      • this will ask to enter root password
      • hit enter without entering any password (this step might never go away!)
      • if empty root password is granted in previous step
        enter and re-enter new password in the next steps
      • this could show some errors Password update failed!
    • Method 2: /usr/local/mysql/bin/mysqladmin -u root -p password

      • this will ask to enter password
      • hit enter without entering any password
      • this will show some errors!

But preceding two methods did not work!

  • Follow the working method:
    • start the [email protected] service brew services start [email protected]

    • run mysql.servert start

    • this will show an error with error log file location

    • typical mariadb error file location: /usr/local/var/mysql/<filename>.local.err

    • run tail -f /usr/local/var/mysql/<filename>.local.err

    • then re-run mysql.servert start

    • there will be an error related to Invalid flags lib

    • run brew services stop [email protected]

    • (BACKUP, BACKUP, BACKUP YOUR DBS! THIS WILL DELETE ALL DBs!)
      run sudo rm -rf /usr/local/var/mysql

    • run

      mysql_install_db --verbose --user=`whoami`
      --basedir="$(brew --prefix [email protected])"
      --datadir="/usr/local/var/mysql" --tempdir="/tmp"
      

      This will get the mariaDB Cellar installation path from brew and this will install the initial db.

    • instead of running mysql_secure_installation or mariadb-secure-installation run: sudo mysql -u root

    • this will drop to mysql shell

    • enter command: use mysql;

    • enter command: ALTER USER 'root@localhost' IDENTIFIED BY '<password>'; (replace the )

    • enter command: ALTER USER '[email protected]' IDENTIFIED BY '<password>'; (replace the )

    • enter command: FLUSH PRIVILEGES;

    • enter command: exit

    • now you can run mysql -u root -p and use the <password> entered in earlier step.

That's all!

3

MariaDB 10.4 enables Unix socket authentication plugin for the local root by default. It means that on a freshly installed system you can connect to a running server without a password, as long as you are a local root (e.g. run under sudo) and using a socket rather than TCP.

Further, MariaDB 10.4 allows multiple authentication methods for accounts. It configures the local root to be able to use password authentication as well, but it initially invalidates the password (doesn't set an empty password as it used to). If you want to use the password authentication and connect as mysql -uroot -p, you need first connect as a root using Unix socket and run SET PASSWORD=....

The advanced user configuration is now stored in mysql.global_priv table in JSON format. mysql.user has been kept for backward compatibility, but it has stopped being a table and has become a view. As a consequence of allowing multiple authentication methods, it doesn't always show user configuration accurately. Specifically, it doesn't show all authentication methods available for a user, you need to query mysql.global_priv for that. On a fresh installation, you'll see something like

+-----------+--------+--------------------------------------------------------------------------------------------------------------------------------------------+
| Host      | User   | Priv                                                                                                                                       |
+-----------+--------+--------------------------------------------------------------------------------------------------------------------------------------------+
| localhost | root   | {"access":18446744073709551615,"plugin":"mysql_native_password","authentication_string":"invalid","auth_or":[{},{"plugin":"unix_socket"}]} |
...

You can find more information about 10.4 authentication changes here.

5
  • 1
    That's great but how is this a solution? This is just information. Commented Apr 12, 2020 at 1:33
  • Solution for what, exactly? If the author wants to connect as a root via UNIX socket (in other words, without a password), he already figured out how to do so and wrote about it, sudo mysql; I would assume the remaining question was why, and the above is the explanation. If the author wants to connect with password authentication, without having to sudo, the solution is to set the password first, as the 2nd paragraph of the above answer suggests.
    – elenst
    Commented Apr 13, 2020 at 12:45
  • 1
    Okay I've downvoted your answer, and upvoted the other answer. Thanks. Commented Apr 14, 2020 at 20:02
  • This answer is controversial but I have to upvote it. I came here, because I installed MariaDB on my local machine and couldn't connect. This answer explains why, just use sudo when doing something with the root user on your local machine. Commented Jul 7, 2021 at 19:39
  • This ultimately helped me, but the actual solution is: Run cli using above / sudo, THEN change the password using the accepted answer (which is not a real solution either)
    – kevinc
    Commented Feb 7, 2022 at 23:50
2

tl;dr

  1. Using sudo is the advised method. (e.g. sudo mysql)
  2. Set the root account's password only when necessary.

Introduction

Starting from MariaDB 10.4.3, the default authentication method changed to unix_socket.

Once (the unix_socket plugin) has the (UNIX) user name, it will authenticate the connecting user as the MariaDB account that has the same user name. — Authentication Plugin - Unix Socket

Using unix_socket means that if you are the system root user, you can login as root@locahost without a password. — Authentication from MariaDB 10.4

To paraphrase, if you are the system's 'root' user, then you can access the MariaDB's 'root' user without any password. This can be achieved using the sudo command in linux and macOS.

Unix

The mysql command is denied, because there is no MariaDB user whose name is 'username'.

username@hostname:~$ mysql --version
# mysql  Ver 15.1 Distrib 10.6.12-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper

username@hostname:~$ mysql
# ERROR 1045 (28000): Access denied for user 'username'@'localhost' (using password: NO)

The sudo mysql command worked because there is a DB user named 'root' out-of-the-box.

username@hostname:~$ sudo mysql
# Welcome to the MariaDB monitor.  Commands end with ; or \g.
# Your MariaDB connection id is 32
# Server version: 10.6.12-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04

MariaDB [(none)]> SELECT CURRENT_USER;
# +----------------+
# | CURRENT_USER   |
# +----------------+
# | root@localhost |
# +----------------+
# 1 row in set (0.001 sec)

The 'root' and 'mysql' users have the authentication string set as invalid, which is not a valid password hash. This is the reason why the these accounts seem to have a password set.

MariaDB [(none)]> SELECT user, plugin, authentication_string FROM mysql.user;
# +-------------+-----------------------+-----------------------+
# | User        | plugin                | authentication_string |
# +-------------+-----------------------+-----------------------+
# | mariadb.sys | mysql_native_password |                       |
# | root        | mysql_native_password | invalid               |
# | mysql       | mysql_native_password | invalid               |
# +-------------+-----------------------+-----------------------+
# 3 rows in set (0.002 sec)

Two all-powerful accounts are created by default — root and the OS user that owns the data directory, typically mysql. They are created as:

CREATE USER root@localhost IDENTIFIED VIA unix_socket OR mysql_native_password USING 'invalid'
CREATE USER mysql@localhost IDENTIFIED VIA unix_socket OR mysql_native_password USING 'invalid'

Authentication from MariaDB 10.4

macOS and Brew

For some reason, the latest version of MariaDB installed with Brew has a different setup.

username@hostname ~ % mysql
# Welcome to the MariaDB monitor.  Commands end with ; or \g.
# Your MariaDB connection id is 20
# Server version: 10.11.3-MariaDB Homebrew

MariaDB [(none)]> SELECT CURRENT_USER;
# +--------------------+
# | CURRENT_USER       |
# +--------------------+
# | username@localhost |
# +--------------------+
# 1 row in set (0.001 sec)

MariaDB [(none)]> SELECT user, plugin, authentication_string FROM mysql.user;
# +-------------+-----------------------+-----------------------+
# | User        | plugin                | authentication_string |
# +-------------+-----------------------+-----------------------+
# | mariadb.sys | mysql_native_password |                       |
# | root        | mysql_native_password | invalid               |
# | username    | mysql_native_password | invalid               |
# | PUBLIC      |                       |                       |
# +-------------+-----------------------+-----------------------+
# 4 rows in set (0.003 sec)

The mysql command and the authentication WORKS without sudo because there is another MariaDB user created by default - it's username matches the macOS username.

mysql_secure_installation

The infamous mysql_secure_installation script is probably not necessary in MariaDB 10.4+.

Note that many of the reasons for the existence of this script no longer apply. In particular, from MariaDB 10.4, Unix socket authentication is applied by default, and there is usually no need to create a root password. See Authentication from MariaDB 10.4. — mysql_secure_installation

0

I'm using this mysql_secure_installation and it now works for me:

$ mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): << enter root here >>

I enter root as current password

OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

and do the rest

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.