MySQL Directory – How to Create a Database Directory Structure for MySQL

databaseMySQLrubyruby-on-railssql

I was following this tutorial:

https://www.digitalocean.com/community/tutorials/how-to-use-mysql-with-your-ruby-on-rails-application-on-ubuntu-14-04

So far, I installed mysql-server mysql-client libmysqlclient-dev, and I was suppose to tell MySQL to create its database directory structure where it will store its information using sudo mysql_install_db.

$ sudo mysql_install_db

[WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
[ERROR]   The data directory needs to be specified.

I thought running mysqld --initialize would solve the issue:

$ mysqld --initialize 

mysqld: Can't create directory '/var/lib/mysql/' (Errcode: 17 - File exists)
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
[ERROR] Aborting

How do I tell MySQL to create its database directory structure where it will store its information?

Best Answer

I managed to solve the problem using a previously answered question, How to install mysql on Ubuntu 16.04, but I needed to modify the solution to work for me.

When I deleted/moved /var/lib/mysql directory, mysqld --initialize couldn't create the database directory I want in replacement for /var/lib/mysql , and it gave me Errcode: 13 - Permission denied even though I preceded it with sudo.

I deleted/moved the content of /var/lib/mysql/, but not the directory itself. I tried again to run sudo mysql --initialize, and it worked, and created new content within the /var/lib/mysql/ directory. The content produced by mysql --initialize is different than the previous content I deleted/moved.

I continued the rest of the steps in the tutorial, and worked perfectly.

P.S. I needed to use sudo -i to access /var/lib/mysql.

Related Question