MySQL 数据目录位置

在下载了64位版本的 DMG 文件后,我在 Mac 上安装了 MySQL。

在尝试创建数据库时,出现了错误1006——无法创建数据库。浏览了多个网站后,似乎是由于用户所有权设置的 MySQL“数据目录”位置需要更改。

MySQL 默认的 data目录在哪里? 我在本地主机中找不到 /var/lib/mysql

谢谢。

163699 次浏览

See if you have a file located under /etc/my.cnf. If so, it should tell you where the data directory is.

For example:

[mysqld]
set-variable=local-infile=0
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
...

My guess is that your mysql might be installed to /usr/local/mysql-XXX.

You may find these MySQL reference manual links useful:

As suggested, I edited this message to place a proper answer.


The 'physical' location of the MySQL database is under /usr/local/mysql

The mysql is a symlink to the current active MySQL installation, in my case the exact folder is mysql-5.6.10-osx10.7-x86_64.

Inside that folder you'll see another data folder, inside it are RESTRICTED folders with your databases.

You can't actually see the size of your databases (that was my issue) from the Finder because the folder are protected, you can though see from the terminal with sudo du -sh /usr/local/mysql/data/{your-database-name} and like this you'll get a nice formatted output with the size.

In those folder you have different files with all the folders present in your database, so it's safer to check the db's folder to get a size.

That's it. Enjoy!

If the software is Sequel pro the default install mysql on Mac OSX has data located here:

/usr/local/var/mysql

Check where is the root folder of mysql with:

mysql_config

Well, if yo don't know where is my.cnf (such Mac OS X installed with homebrew), or You are looking found others choices:

ps aux|grep mysql
abkrim            1160   0.0  0.2  2913068  26224   ??  R    Tue04PM   0:14.63 /usr/local/opt/mariadb/bin/mysqld --basedir=/usr/local/opt/mariadb --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/opt/mariadb/lib/plugin --bind-address=127.0.0.1 --log-error=/usr/local/var/mysql/iMac-2.local.err --pid-file=iMac-2.local.pid

You get datadir=/usr/local/var/mysql

If you are using macOS {mine 'High Sierra'} and Installed XAMPP

You can find mysql data files;

Go to : /Applications/XAMPP/xamppfiles/var/mysql/

If you are using Homebrew to install MySQL@5.7, the location is

/usr/local/Homebrew/var/mysql

I don't know if the location is the same for other versions.

If you install MySQL via homebrew on MacOS, you might need to delete your old data directory /usr/local/var/mysql. Otherwise, it will fail during the initialization process with the following error:

==> /usr/local/Cellar/mysql/8.0.16/bin/mysqld --initialize-insecure --user=hohoho --basedir=/usr/local/Cellar/mysql/8.0.16 --datadir=/usr/local/var/mysql --tmpdir=/tmp
2019-07-17T16:30:51.828887Z 0 [System] [MY-013169] [Server] /usr/local/Cellar/mysql/8.0.16/bin/mysqld (mysqld 8.0.16) initializing of server in progress as process 93487
2019-07-17T16:30:51.830375Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
2019-07-17T16:30:51.830381Z 0 [ERROR] [MY-013236] [Server] Newly created data directory /usr/local/var/mysql/ is unusable. You can safely remove it.
2019-07-17T16:30:51.830410Z 0 [ERROR] [MY-010119] [Server] Aborting
2019-07-17T16:30:51.830540Z 0 [System] [MY-010910] [Server] /usr/local/Cellar/mysql/8.0.16/bin/mysqld: Shutdown complete (mysqld 8.0.16)  Homebrew.

On M1 and homebrew the "data directory" is:

/opt/homebrew/var/mysql

And the main path is:

/opt/homebrew/Cellar/mysql