How to Solve Error Message The data directory needs to be specified in initializing MySQL Data Directory

Posted on

Introduction

Actually, this article will just focus on how to solve the error message exist in the title of this article. The error message is ‘The data directory needs to be specified. It happens on installing a new MySQL Database Server manually. Upon the installation process, there is one step where it will aim to create a data directory for that MySQL Database Server. Actually, before going on the command execution, there are several step before. The following below are those steps :

1. Download the source installer of MySQL Database Server. It exist in this link. This is an attempt for installing MySQL Database Server version 5.7. The file source installer for that MySQL Database Server is ‘mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz’.

2. Move or copy the source installer to a specific location. For an example, it is in ‘/opt/mysql/temp.

3. After that, don’t forget to extract the file by executing the command as follows :

root@hostname:/opt/mysql/temp# tar -xvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
....
root@hostname:/opt/mysql/temp#

4. Another step after that is installing the service. The service installation is quite simple. It is just a simple step by copying a template file exist in the support-files folder. The ‘support-files’ folder available in the MySQL Database Server’s source installation folder. So, using the above path, it exist in ‘/opt/mysql/temp/mysql-5.7.28-linux-glibc2.12-x86_64/support-files’. The initial script file is ‘mysql.server’. So, just copy the file to ‘/etc/init.d/’.

Aside from the above step, there is another important step in order for the MySQL Database Server run properly. It is the existance of the data directory folder. The error message occur as in the title of this article is actually because of the improper execution of the command to generate that folder.

root@hostname:/opt/mysql/temp#
2020-01-07 20:22:24 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2020-01-07 20:22:25 [ERROR]   The data directory needs to be specified.
root@hostname:/opt/mysql/temporary/mysql-5.7.28-linux-glibc2.12-x86_64/bin# 

Solution

In order to create the data directory by having all the content inside, the binary command execution is ‘mysql_install_db’. The following is the actual process for generating the data directory using the execution command of ‘mysql_install_db’ :

root@hostname:/opt/mysql/temporary/mysql-5.7.28-linux-glibc2.12-x86_64/bin# ./mysql_install_db
2020-01-07 20:22:24 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2020-01-07 20:22:25 [ERROR]   The data directory needs to be specified.
root@hostname:/opt/mysql/temporary/mysql-5.7.28-linux-glibc2.12-x86_64/bin# 

But the execution of the command is wrong because it does not use another parameter for specifying the location of the data directory folder. The following is the correct execution of the command above :

root@hostname:/opt/mysql/temporary/mysql-5.7.28-linux-glibc2.12-x86_64/bin# ./mysql_install_db --datadir=/opt/mysql/temporary/mysql-5.7.28-linux-glibc2.12-x86_64/data
2020-01-06 15:30:13 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
...

Leave a Reply