Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
Asked Answered
H

18

82

I am on a server that has afresh install on RHEL 5. I was able to install Apache and PHP just fine., but I am having serious trouble with my MySQL installation. I tried the following:

yum install mysql-server mysql 

And didn't get any errors or conflicts. Then I tried to start mysql with the following commands:

chkconfig --levels 235 mysqld on
service mysqld start

And get Timeout error occurred trying to start MySQL Daemon.

I checked my logs and see this error:

[ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist

I'm not sure where to go from here.

For reference I am using RHEL 5 and installed the latest versions of PHP 5 and Apache.

Hardfeatured answered 31/1, 2012 at 16:45 Comment(2)
So I'm searching the mysql site and see that this error message pops up for their troubleshooting on installing mysql on windows systems: dev.mysql.com/doc/refman/5.0/en/windows-troubleshooting.html OK, that's probably the same problem on a unix based system as well, so i guess I need to figure out what the default location is uspposed to be, and where it's actually being installed. "These messages often occur when the MySQL base or data directories are installed in different locations than the default locations"Hardfeatured
For osx and mysql 5.7, refer to https://mcmap.net/q/241707/-fatal-error-can-39-t-open-and-lock-privilege-tables-table-39-mysql-host-39-doesn-39-t-existLabial
H
70
  1. Uninstall mysql using yum remove mysql*

  2. Recursively delete /usr/bin/mysql and /var/lib/mysql

  3. Delete the file /etc/my.cnf.rmp

  4. Use ps -e to check the processes to make sure mysql isn't still running.

  5. Reboot server with reboot

  6. Run yum install mysql-server. This also seems to install the mysql client as a dependency.

  7. Give mysql ownership and group priveleges with:

    chown -R mysql /var/lib/mysql

    chgrp -R mysql /var/lib/mysql

  8. Use service mysqld start to start MySQL Daemon.

Hardfeatured answered 1/2, 2012 at 16:37 Comment(6)
I think changing group and ownership was the vital part. It seems that when mysql starts it automatically creates the mysql.sock file, but it can't do that if it doesn't have system permissions.Hardfeatured
I wonder if SELinux permissions might also have been involved? Destroying the whole thing might have let the relabeling daemon label the new files with the correct labels.Leaseback
I've reproduced this error and the chgrp statement alone was enough to resolve it.Rodgerrodgers
This answer should be edited to include @Gisellegish answer between steps 7 and 8. That's how a got my mysql (mariadb on centOS 7) sorted out.Crissman
Got the same error message, but changing the permissions did not fix the problem. Any thoughts? askubuntu.com/questions/623797/error-updating-mysql-packageNell
My issue was that I already had a previous /etc/my.cnf, and it was put in place after the installation, so bad referencing of the "datadir." Using the mysql_install_db with the proper --ldata /my/custom/path/to/lib/mysql solved my issue.Gile
G
97

After chown and chgrp'ing /var/lib/mysql per the answer by @Bad Programmer, you may also have to execute the following command:

sudo mysql_install_db --user=mysql --ldata=/var/lib/mysql

Then restart your mysqld.

Gisellegish answered 4/3, 2014 at 18:14 Comment(6)
./bin/mysqld: Too many arguments (first extra is '–-user=mysql').B
sudo mysql_install_db --user=mysql --ldata=/var/lib/mysqlDemit
@bk0, Aside from re-installation, how can we fix this issue? What exactly is causing the error and how can it be mended?Brunei
After a fresh install of mysql (or after you delete /var/lib/mysql), you need to manually create the system tables via mysql_install_db. The mysql daemon will not do it for you on startup.Gisellegish
For anyone else wondering, copy pasting this part seems to fail. I think there's a special character or something in there, so I had to copy by hand.Schiedam
fixed issue for meClimacteric
H
70
  1. Uninstall mysql using yum remove mysql*

  2. Recursively delete /usr/bin/mysql and /var/lib/mysql

  3. Delete the file /etc/my.cnf.rmp

  4. Use ps -e to check the processes to make sure mysql isn't still running.

  5. Reboot server with reboot

  6. Run yum install mysql-server. This also seems to install the mysql client as a dependency.

  7. Give mysql ownership and group priveleges with:

    chown -R mysql /var/lib/mysql

    chgrp -R mysql /var/lib/mysql

  8. Use service mysqld start to start MySQL Daemon.

Hardfeatured answered 1/2, 2012 at 16:37 Comment(6)
I think changing group and ownership was the vital part. It seems that when mysql starts it automatically creates the mysql.sock file, but it can't do that if it doesn't have system permissions.Hardfeatured
I wonder if SELinux permissions might also have been involved? Destroying the whole thing might have let the relabeling daemon label the new files with the correct labels.Leaseback
I've reproduced this error and the chgrp statement alone was enough to resolve it.Rodgerrodgers
This answer should be edited to include @Gisellegish answer between steps 7 and 8. That's how a got my mysql (mariadb on centOS 7) sorted out.Crissman
Got the same error message, but changing the permissions did not fix the problem. Any thoughts? askubuntu.com/questions/623797/error-updating-mysql-packageNell
My issue was that I already had a previous /etc/my.cnf, and it was put in place after the installation, so bad referencing of the "datadir." Using the mysql_install_db with the proper --ldata /my/custom/path/to/lib/mysql solved my issue.Gile
B
60

I had this issue on arch linux as well. The issue was pacman installed the package in a different location than MySQL was expecting. I was able to fix the issue with this:

sudo mysql_install_db --user=mysql --basedir=/usr/ --ldata=/var/lib/mysql/

Hope this helps someone!

Busload answered 22/4, 2016 at 20:2 Comment(2)
I even don't knowed this command, it works indeed with mariadb and on a current Debian systemTurin
You unbricked my mariadb system, after hours and hours of searching online. Thank you! (running Arch Linux). My system just spontaneously died after a system update.Groundsel
O
12

The root of my problem seemed to be selinux, which was turned on (enforcing) automatically on OS install.

I wanted my mysql in /data.

After verifying that my.cnf had:

datadir=/data/mysql

(and leaving the socket at /var/lib/mysql) I executed the command to turn off selinux for mysqld (alternative is to turn it off completely):

setsebool -P mysqld_disable_trans=1

I ran the following commands:

> chown -R mysql .
> chgrp -R mysql .
> mysql_install_db --user=mysql

I started the mysql daemon and everything worked fine after that.

Osmund answered 19/12, 2012 at 0:55 Comment(0)
I
11
mysql_install_db –-user=mysql --ldata=/var/lib/mysql

Worked for me in Centos 7

Inainability answered 24/11, 2014 at 9:14 Comment(2)
If applicable, I find this a much better solution than completely uninstalling and reinstalling the database server like other answers are suggesting.Barilla
Solved my problem ,and actually this command should be run under mysql directory as : ./scripts/mysql_install_db --user=mysql –ldata=/var/lib/mysqlLawgiver
D
10

initialize mysql before start on windows.

mysqld --initialize
Definiendum answered 21/12, 2015 at 9:27 Comment(3)
[ERROR] --initialize specified but the data directory has files in it. Aborting.Rackrent
@giannischristofakis If it's a brand new install -- no actual database with valuable data, just delete the contents of the data directory and reissue the command. (2 years later.)Maimaia
@giannischristofakis What if it's not a brand new install?Poindexter
D
7

When download MySql zip version, if run mysqld directly, you'll get this error:

2016-02-18T07:23:48.318481Z 0 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
2016-02-18T07:23:48.319482Z 0 [ERROR] Aborting

You have to run below command first:

mysqld --initialize

Make sure your data folder is empty before this command.

Diagenesis answered 18/2, 2016 at 7:32 Comment(0)
B
4

Just this command is enough to do the magic on centos 6.6

mysql_install_db
Budd answered 20/2, 2016 at 6:36 Comment(0)
L
4

I just met the same problem with mysql 5.7 on OSX:

rm -rf {datadir}
mysqld --initialize --datadir {datadir}
mysqld --datadir {datadir}
Labial answered 7/6, 2016 at 0:12 Comment(0)
C
3

If you move your datadir, you not only need to give the new datadir permissions, but you need to ensure all parent directories have permission.

I moved my datadir to a hard drive, mounted in Ubuntu as:

/media/*user*/Data/

and my datadir was Databases.

I had to set permissions to 771 to each of the media, user and Data directories:

sudo chmod 771 *DIR*

If this does not work, another way you can get mysql to work is to change user in /etc/mysql/my.cnf to root; though there are no doubt some issues with doing that from a security perspective.

Chiaki answered 9/10, 2014 at 12:36 Comment(0)
F
2

For myself, I had to do:

yum remove mysql*

rm -rf /var/lib/mysql/
cp /etc/my.cnf ~/my.cnf.bkup

yum install -y mysql-server mysql-client

mysql_install_db

chown -R mysql:mysql /var/lib/mysql
chown -R mysql:mysql /var/log/mysql

service mysql start

Then I was able to get back into my databases and configure them again after I nuked them the first go around.

Fiddlestick answered 6/9, 2013 at 14:54 Comment(1)
This is the one that did it for me. mysql_install_db wasn't enough, but this solved for me. Thanks.Ufa
M
2

In Windows run the following commands in the command prompt as adminstrator

Step 1:
mysql_install_db.exe

Step 2:
mysqld --initialize

Step 3:
mysqld --console

Step 4:
In windows

Step 4:
mysqladmin -u root password "XXXXXXX"

Step 5:
mysql -u root -p

Mariadb database information

Misogyny answered 29/8, 2020 at 1:11 Comment(0)
D
0

In my case the path of MySQL data folder had a special character "ç" and it make me get...

Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist.

I'm have removed all special characters and everything works.

Dasi answered 26/6, 2013 at 18:36 Comment(0)
H
0

On CentOS EL 6 and perhaps on earlier versions there is one way to get into this same mess.

Install CentOS EL6 with a minimal installation. For example I used kickstart to install the following:

%packages
@core
acpid
bison
cmake
dhcp-common
flex
gcc
gcc-c++
git
libaio-devel
make
man
ncurses-devel
perl
ntp
ntpdate
pciutils
tar
tcpdump
wget
%end

You will find that one of the dependencies of the above list is mysql-libs. I found that my system has a default my.cnf in /etc and this contains:

[mysqld]
dataddir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

When you build from the Generic Linux (Architecture Independent), Compressed TAR Archive your default data directory is /usr/local/mysql/data which conflicts with the /etc/my.cnf already present which defines datadir=/var/lib/mysql. Also the pid-file defined in the same file does not have permissions for the mysql user/group to write to it in /var/run/mysqld.

A quick remedy is to mv /etc/my.cnf /etc/my.cnf.old which should get your generic source procedure working.

Of course the experience is different of you use the source RPMs.

Huneycutt answered 9/12, 2013 at 23:0 Comment(0)
S
0

I had the same issue in trying to start the server and followed the "checked" solution. But still had the problem. The issue was the my /etc/my.cnf file was not pointing to my designated datadir as defined when I executed the mysql_install_db with --datadir defined. Once I updated this, the server started correctly.

Survivor answered 21/4, 2014 at 0:22 Comment(0)
Z
0

If you have a server which used to happily run MySQL, but now gives this error, then an uninstall and re-install of MySQL is overkill.

In my case, the server died and took a few disk blocks with it. This affected a few files, including /var/lib/mysql/mysql/host.frm and /var/lib/mysql/mysql/proc.frm

Luckily, I could copy these from another server, and this got me past that table error.

Zagazig answered 7/12, 2016 at 12:30 Comment(0)
P
0

I got similar error on overlayfs (overlay2) that is the default on Docker for Mac. The error happens when starting mysql on the image, after creating a image with mysql.

2017-11-15T06:44:22.141481Z 0 [ERROR] Fatal error: Can't open and lock privilege tables: Table storage engine for 'user' doesn't have this option

Switching to "aufs" solved the issue. (On Docker for Mac, the "daemon.json" can be edited by choosing "Preferences..." menu, and selecting "Daemon" tab, and selecting "Advanced" tab.)

/etc/docker/daemon.json :

{
  "storage-driver" : "aufs",
  "debug" : true,
  "experimental" : true
}

Ref:

https://github.com/moby/moby/issues/35503

https://qiita.com/Hige-Moja/items/7b1208f16997e2aa9028

Paladin answered 15/11, 2017 at 7:24 Comment(0)
W
-3

My case on Ubuntu 14.04.2 LTS was similar to others with my.cnf, but for me the cause was a ~/.my.cnf that was leftover from a previous installation. After deleting that file and purging/re-installing mysql-server, it worked fine.

Wicketkeeper answered 5/7, 2015 at 4:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.