Initializing MySQL database after installation

Please read whole tutorial before doing anything. These commands were tested on a Linux environment (running MySQL 4.1.22) and should work on most Unix machines too. However, they will have to be modified a little for Windows environment.

If you have an empty data folder or you want to initialize/reinitialize MySQL server from scratch (possibly after dropping all existing databases) then use the following instructions (assuming you have root access or you can use sudo):

For this tutorial, I'll assume the data directory (where all the data was stored or has to be stored) is: /clients/example/mysql/data/. Now, execute the following commands from shell command line.

Replace data directory path with your own (will delete all your data, so think twice before you run the command):

sudo rm -drf /clients/example/mysql/data

Recreate data directory:

sudo mkdir /clients/example/mysql/data

If you are running MySQL as mysql user then do the following (replace mysql with your user name and group name):

sudo chown mysql:mysql /clients/example/mysql/data

If you have logging of any type enabled then you can also use the above commands (or slightly modify them) to delete, recreate, and assign proper privileges to them.

Now, run the MySQL installation script as follows (from inside the MySQL installation folder e.g. /opt/mysql/). Replace with your data directory path.

sudo ./scripts/mysql_install_db --user=mysql --datadir=/clients/example/mysql/data/

You should also give ownership to mysql user and group using the following command.

sudo chown -R mysql:mysql /clients/example/mysql/

That's it. Now, you can run your MySQL daemon using mysqld directly or using mysqld_safe (recommended).

Did this tutorial help a little? How about buy me a cup of coffee?

Buy me a coffee at ko-fi.com

Please feel free to use the comments form below if you have any questions or need more explanation on anything. I do not guarantee a response.

IMPORTANT: You must thoroughy test any instructions on a production-like test environment first before trying anything on production systems. And, make sure it is tested for security, privacy, and safety. See our terms here.

Comments (write a comment):

After installation we must see to it that the software installed is successfully done to avoid complications hereafter. Posted by: database s on May 09, 2012

I did the above and tried using mysql_install_db.sh but got this:FATAL ERROR: Could not find my_print_defaults The following directories were searched:

/opt/app/mysq-5.6.10/bin
/opt/app/mysq-5.6.10/extra

If you compiled from source, you need to run 'make install' to
copy the software into the correct location ready for operation.

If you are using a binary release, you must either be at the top
level of the extracted archive, or pass the --basedir option
pointing to that location.


I ran make and make install Posted by: Tommy on Feb 21, 2013

@Tommy: did you see any errors when you ran configure, make, and make install commands? Posted by: Shahryar on Feb 22, 2013

I got it built, installed and running. Now I cannot connect. It says it connected but then errors out with "Segmentation fault". Any ideas? It is the client, not the mysqld. Posted by: Tommy P on Feb 28, 2013

@Tommy: it could be a bug or may be you did something wrong during the build and install process. I found a few bugs via Google:
http://bugs.mysql.com/bug.php?id=58497
http://bugs.mysql.com/bug.php?id=60844
http://bugs.mysql.com/bug.php?id=65296

You may find more details on these bug reports or patches or if/when the bug was fixed etc.

Good luck! Posted by: shahryar on Mar 01, 2013

Thanking You Posted by: prasad on Aug 22, 2014

While running mysql_install_db script got the error as below

[oracle@localhost mysql-5.6.10]$ sudo ./scripts/mysql_install_db --user=mysql --datadir=/opt/mysql-5.6.22/data/[sudo] password for oracle:
Installing MySQL system tables...2015-01-08 00:15:52 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2015-01-08 00:15:52 21904 [ERROR] COLLATION 'latin1_swedish_ci' is not valid for CHARACTER SET 'utf8'
2015-01-08 00:15:52 21904 [ERROR] Aborting

2015-01-08 00:15:52 21904 [Note] Binlog end
2015-01-08 00:15:52 21904 [Note] ./bin/mysqld: Shutdown complete
Please suggest what to do to run it. Posted by: Salil j on Jan 07, 2015

leave a comment on tutorial leave a comment