lower_case_table_names Settings in MySQL 8.0.12 - mysql

I've just compiled the version MySQL 8.0.12 in a Ubuntu 16.0.4.
After following the instructions in the website and making the following my.cnf file:
[mysqld]
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
port=3306
log-error=/usr/local/mysql/data/localhost.localdomain.err
user=mysql
secure_file_priv=/usr/local/mysql/mysql-files
local_infile=OFF
log_error = /var/log/mysql/error.log
# Remove case sensitive in table names
lower_case_table_names=1
I get the following error:
2018-08-11T19:45:06.461585Z 1 [ERROR] [MY-011087] [Server] Different lower_case_table_names settings for server ('1') and data dictionary ('0').
What should I change so that data dictionary is aligned to server settings?

So far, I can get it to work with a workaround (I originally posted on askubuntu): by re-initializing MySQL with the new value for lower_case_table_names after its installation. The following steps apply to a new installation. If you have already data in a database, export it first to import it back later:
Install MySQL:
sudo apt-get update
sudo apt-get install mysql-server -y
Stop the MySQL service:
sudo service mysql stop
Delete the MySQL data directory:
sudo rm -rf /var/lib/mysql
Recreate the MySQL data directory (yes, it is not sufficient to just delete its content):
sudo mkdir /var/lib/mysql
sudo chown mysql:mysql /var/lib/mysql
sudo chmod 700 /var/lib/mysql
Add lower_case_table_names = 1 to the [mysqld] section in /etc/mysql/mysql.conf.d/mysqld.cnf.
Re-initialize MySQL with --lower_case_table_names=1:
sudo mysqld --defaults-file=/etc/mysql/my.cnf --initialize --lower_case_table_names=1 --user=mysql --console
Start the MySQL service:
sudo service mysql start
Retrieve the new generated password for MySQL user root:
sudo grep 'temporary password' /var/log/mysql/error.log
Change the password of MySQL user root either by:
sudo mysql -u root -p
and executing:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPa$$w0rd';
afterwards, OR by calling the "hardening" script anyway:
sudo mysql_secure_installation
After that, you can verify the lower_case_table_names setting by entering the MySQL shell:
sudo mysql -u root -p
and executing:
SHOW VARIABLES LIKE 'lower_case_%';
Expected output:
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| lower_case_file_system | OFF |
| lower_case_table_names | 1 |
+------------------------+-------+

As per this link, lower_case_table_names should be set together with --initialize option.

MySQL Documentation says
lower_case_table_names can only be configured while initializing the server. Changing the lower_case_table_names setting after the server is initialized is prohibited.
https://dev.mysql.com/doc/refman/8.0/en/identifier-case-sensitivity.html

The best way to prevent this problem is :At first add
[mysqld]
lower_case_table_names=1
then start mysql service for first time.
But anyway if you have started the server already,to solve your problem:
1.stop mysql:
systemctl stop mysql
2.clean data directory or change the default, the following is for new installations , if you have data in your database BACK UP them beforehand
rm -rf /var/lib/mysql
3.Insert lower_case_table_names = 1 in your my.cnf:
[mysqld]
lower_case_table_names=1
4.Start again
systemctl start mysqld

To fix this issue,
Just take the backup of the existing db Schema using the following command inside bin folder (/usr/local/mysql/bin)
./mysqldump -uroot -p password > dump.sql
Once the backup is taken delete the existing data folder in Mysql Home(/usr/local/mysql/) using the command
rm -rf data
Now add the configuration as "lower_case_table_names=1" in my.cnf under MYSQLD section (/etc/my.cnf)
Now Initialize the data directory using the following command inside bin directory (/usr/local/mysql/bin)
For Secure mode
./mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql --console
For Insecure mode
./mysqld --defaults-file=/etc/my.cnf --initialize-insecure --user=mysql --console
Once the data directory initialized, For Insecure mode repeat the Installation again and For Secure mode use the root password which is initialized during the run time of data directory Initialization.
Now import the existing dump file inside the Mysql Server using the command inside (/usr/local/mysql/bin) directory
./mysql -uroot -p password < file.sql

If anyone runs into this issue now, if you already initialized mysql, meaning you already had it up and running and then this error occurred, just comment out this line in the my.ini file.
lower_case_table_names=

Related

MySQL community server suddenly stopped working [duplicate]

My problem started off with me not being able to log in as root any more on my mysql install. I was attempting to run mysql without passwords turned on... but whenever I ran the command
# mysqld_safe --skip-grant-tables &
I would never get the prompt back. I was trying to follow these instructions to recover the password.
The screen just looks like this:
root#jj-SFF-PC:/usr/bin# mysqld_safe --skip-grant-tables
120816 11:40:53 mysqld_safe Logging to syslog.
120816 11:40:53 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
and I don't get a prompt to start typing the SQL commands to reset the password.
When I kill it by pressing CTRL + C, I get the following message:
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!
If I retry the command and leave it long enough, I do get the following series of messages:
root#jj-SFF-PC:/run/mysqld# 120816 13:15:02 mysqld_safe Logging to syslog.
120816 13:15:02 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
120816 13:16:42 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
[1]+ Done mysqld_safe --skip-grant-tables
root#jj-SFF-PC:/run/mysqld#
But then if I try to log in as root by doing:
# mysql -u root
I get the following error message:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I checked and /var/run/mysqld/mysqld.sock file doesn't not exist. The folder does, but not the file.
Also, I don't know if this helps or not, but I ran find / -name mysqld and it came up with:
/var/run/mysqld - folder
/usr/sbin/mysqld - file
/run/mysqld - folder
I don't know if this is normal or not. But I'm including this info just in case it helps.
I finally decided to uninstall and reinstall mysql.
apt-get remove mysql-server
apt-get remove mysql-client
apt-get remove mysql-common
apt-get remove phpmyadmin
After reinstalling all packages again in the same order as above, during the phpmyadmin install, I got the same error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
So I tried again to uninstall/reinstall. This time, after I uninstalled the packages, I also manually renamed all mysql files and directories to mysql.bad in their respective locations.
/var/lib/mysql
/var/lib/mysql/mysql
/var/log/mysql
/usr/lib/perl5/DBD/mysql
/usr/lib/perl5/auto/DBD/mysql
/usr/lib/mysql
/usr/bin/mysql
/usr/share/mysql
/usr/share/dbconfig-common/internal/mysql
/etc/init.d/mysql
/etc/apparmor.d/abstractions/mysql
/etc/mysql
Then I tried to reinstall mysql-server and mysql-client again. But I've noticed that it doesn't prompt me for a password. Isn't it supposed to ask for an admin password?
Try this command,
sudo service mysql start
To find all socket files on your system run:
sudo find / -type s
My Mysql server system had the socket open at /var/lib/mysql/mysql.sock
Once you find where the socket is being opened, add or edit the line to your /etc/my.cnf file with the path to the socket file:
socket=/var/lib/mysql/mysql.sock
Sometimes the system startup script that launched the command line executable specifies a flag --socket=path. This flag could override the my.cnf location, and that would result in a socket not being found where the my.cnf file indicates it should be. Then when you try to run the mysql command line client, it will read my.cnf to find the socket, but it will not find it since it deviates from where the server created one. So, Unless you care where the socket resides, just changing the my.cnf to match should work.
Then, stop the mysqld process. How you do this will vary by system.
If you're super user in the linux system, try one of the following if you don't know the specific method your Mysql setup uses:
service mysqld stop
/etc/init.d/mysqld stop
mysqladmin -u root -p shutdown
Some systems aren't setup to have an elegant way to stop mysql (or for some reason mysql doesn't respond) and you can force terminate mysql with either:
One step: pkill -9 mysqld
Two step (least preferred):
Find the process id of mysql with either pgrep mysql or ps aux | grep mysql | grep -v grep
Assuming the process id is 4969 terminate with kill -9 4969
After you do this you might want to look for a pid file in /var/run/mysqld/ and delete it
Make sure the permissions on your socket is such that whatever user mysqld is running as can read/write to it. An easy test is to open it up to full read/write and see if it still works:
chmod 777 /var/run/mysqld/mysqld.sock
If that fixes the issue, you can tailor the permissions and ownership of the socket as needed based on your security settings.
Also, the directory the socket resides in has to be reachable by the user running the mysqld process.
This error occurs due to multiple installations of mysql.
Run the command:
ps -A|grep mysql
Kill the process by using:
sudo pkill mysql
and then run command:
ps -A|grep mysqld
Also Kill this process by running:
sudo pkill mysqld
Now you are fully set just run the following commands:
service mysql restart
mysql -u root -p
Have very well working mysql again
The solution is way easier.
First, you have to locate(in Terminal with "sudo find / -type s") where your mysql.sock file is located. In my case it was in /opt/lampp/var/mysql/mysql.sock
Fire up Terminal and issue
sudo Nautilus
This starts your Files manager with super user privileges
From Nautilus navigate to where your mysql.sock file is located
Right click on the file and select Make Link
Rename the Link File to mysqld.sock then Right click on the file and Cut it
Go to /var/run and create a folder called mysqld and enter it
Now right click and Paste the Link File
Voila! You will now have a mysqld.sock file at /var/run/mysqld/mysqld.sock :)
Just Need to Start MySQL Service after installation:
For Ubuntu:
sudo service mysql start;
For CentOS or RHEL:
sudo service mysqld start;
There is a bug on Ubuntu with MySQL 5.6 and 5.7 where var/run/mysqld/ would disappear whenever MySQL service stopped or is rebooted. This prevents MySQL from running at all. Found this workaround, which isn't perfect, but at least it gets it running after stopping/reboot:
mkdir /var/run/mysqld/
chown mysqld /var/run/mysqld/
Make sure your inaccessible socket file path is same as '/var/run/mysqld/mysqld.sock', otherwise change the path as yours.
Stop the mysqld
$ sudo /etc/init.d/mysqld stop
If the process still runing;
$ sudo pkill -9 mysqld
Remove the mysql directory where socket going to create. For me it did not allowed to remove, so I had to forcefully remove.
$ sudo mkdir -p /var/run/mysqld
Set the ownership to the dirctory
$ sudo chown mysql:mysql /var/run/mysqld
Start mysql
$ sudo /etc/init.d/mysql start
Trying to connect mysql
$ sudo mysql -u dbuser -p
Okay just copy and paste these codes: This should be done in the terminal, inside a server, when your mysql database is not properly installed, and when you are getting this error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'.
Stop MySql
sudo /etc/init.d/mysqld stop
Restart it or start it
sudo /etc/init.d/mysqld restart or sudo /etc/init.d/mysqld start
Make a link like this and give it to the system
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
Run a secure installation which guides all the process you need to do to configure mysql
/usr/bin/mysql_secure_installation
I faced same error and found that it was due to upgradation of packages, So after restarting my system I resolved error.
I think due to sql libraries/ packages update that error occured, So try this if you are doing some upgrading :)
There is a lots of reason for this issue, but sometimes just restart the mysql server, it will fix the issue.
sudo service mysql restart
The answer of the user load step worked for me.
Sometimes is need edit the file in /etc/mysql/my.cnf add line to client
[client]
password = your_mysql_root_password
port = 3306
host = 127.0.0.1
socket = /var/lib/mysql/mysql.sock
Using XAMPP on ubuntu:
Create a folder called mysqld inside /var/run directory. You can accomplish that using the command sudo mkdir /var/run/mysqld.
Create a symbolic link to mysql.sock file that is created by the XAMPP server when it is started. You can use the command sudo ln -s /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysqld.sock.
Note: The mysql.sock file is created when the server is started and removed when the server is stopped, so sometimes the link you created might appear to be broken but it should work as long as you have started the server using either sudo /opt/lampp/lampp start or any other means.
Start the server if it's not already running and try executing your program again.
Good luck! I hope you'll get away with it this time.
I think your MySQL server has not started. So start the server using one of the following commands.
#services mysql start
or
#/etc/init.d/mysql start
Why getting this error
I received new updates of mysql libraries so i updated my Kubuntu OS after that getting these errors.
Commands i tried and how i fixed it.
MySql-server is running correctly but when i tried to connect its giving
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'.
I checked /var/run/mysqld/mysqld.sock'. this directory.
My files did not existed.
I also tried these commands to connect but did not worked for me.
mysql -h 127.0.0.1 -P 3306 -u root -p
sudo service mysql start
After wasting round about 2 hours i found the solution
sudo apt-get clean
sudo apt-get update
sudo apt-get upgrade -f
After that everything fixed for me.
*Error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
solutions
finally uninstall and reinstall mysql. **
sudo apt-get remove mysql-server
sudo apt-get remove mysql-client
sudo apt-get remove mysql-common
sudo apt-get remove phpmyadmin
then install again by
sudo apt-get install mysql-server-5.6
After this operation, 164 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y press YES for complete installations
......
.......
At last you will get these lines....
Setting up libhtml-template-perl (2.95-1) ...
Setting up mysql-common-5.6 (5.6.16-1~exp1) ...
Processing triggers for libc-bin (2.19-0ubuntu6)
Processing triggers for ureadahead (0.100.0-16) ...
And then
root#ubuntu1404:~# mysql -u root -p (for every password first u
should use )
Enter password:
Note :Entered password should be same as the installation time
password of mysql(like .root,system,admin,rahul etc...)
Then type
USE rahul_db(database name);
Thanks.**
Temporary Solution
Maybe someone facing this problem. I am using Mysql Workbench on Ubuntu 14 and got this error.
mysqldump: Got error: 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) when trying to connect
Find your socket file by running sudo find / -type s, in my case it was /run/mysqld/mysqld.sock
So, I just created a link to this file in tmp directory.
sudo ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock
Please note that this is a temporary solution since the file created will be under /tmp. See other answers for a permanent solution.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
in /etc/my.cnf add this lines:
[client]
socket=/var/lib/mysql/mysql.sock <= this path should be also same as is[mysqld]
And restart the service with:
service mysql restart
this worked for me
This was mentioned a couple of times already, but this worked immediately for me:
service mysql restart
you can find mysqld.sock in /var/run/mysqld if you have already installed mysql-server
by sudo apt-get install mysql-server
I just had this problem on Ubuntu 14.10
Turns that mysql-server was no longer installed (somehow it had been removed) but I couldn't just install it because there were some broken packages and dependency issues/conflicts.
In the end I had to reinstall mysql
sudo apt-get remove mysql-client
sudo apt-get install mysql-server
I had the exactly same issue. After struggling for an hour, I found a way of correcting it without reinstalling mysql-common, mysql-client, mysql-server.
First of all, go to "/var/run/mysqld". You will find that the mysql.sock does not exist. Simply remove the entire mysqld directory and recreate it and provide it necessary privileges.
# rm -rf /var/run/mysqld && mkdir /var/run/mysqld && chown mysql /var/run/mysqld/
Now, Kill the mysql process in it's entirety. It might be possible that it will show you "waiting for page cleaner" on running "/etc/init.d/mysql status" command even after shutting down the service.
To completely close the service, use
# pkill -9 mysqld
Once the process is killed, try starting it again using
# /etc/init.d/mysql start
And you will see that it works good! And also there will be no issue in stopping it too.
In My case two mysqld processes were running..
killed the optional processs by using
pkill -9 mysqld
If you have a lot of databases and tables on your system, and if you have innodb_file_per_table set in my.cnf, then your mysql server might have run out of opened objects / files (or rather the descriptors for these objects)
Set a new max number with
open-files-limit = 2048
and restart mysql.
This approach might help when the socket is not created at all, but really this might not not be the real problem, there is an underlying problem.
My solution;
Ubuntu 18.04 (WSL)
/etc/mysql/my.cnf
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
/etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
I changed the port. It's worked for me. You can write another port. Example 3355
I am using XAMPP on Ubuntu. I found this error when connecting database through terminal. I solve it without any configuration because default socket file path in XAMPP is written in "/opt/lampp/etc/my.cnf" as following:
[client]
#password = your_password
port = 3306
socket = /opt/lampp/var/mysql/mysql.sock
now you can connect just by giving this socket path parameter with mysql command on terminal like:
mysql -u root --socket /opt/lampp/var/mysql/mysql.sock
and it's done without any configuration.
If you don't want to type socket path everytime, then go for changing default path in my.cnf by "/var/run/mysqld/mysqld.sock". Provide permissions and restart mysql server.
Edit:
Recently I've installed Ubuntu 20.04 and trying to install MySQL server but my system were crashing and not working at all. So I've just completely removed MySQL and installed MariaDB. Its working like a charm without any problems.
Changing the host to 127.0.0.1 worked for me.
Edit the file in /etc/mysql/my.cnf and add the below mentioned line to the section: client
[client]
port = 3306
host = 127.0.0.1
socket = /var/lib/mysql/mysql.sock
After you are done with it. Execute the following command.
sudo service mysql start
I had similar problem on a CentOS VPS. If MySQL won't start or keeps crashing right after it starts, try these steps:
1) Find my.cnf file (mine was located in /etc/my.cnf) and add the line:
innodb_force_recovery = X
replacing X with a number from 1 to 6, starting from 1 and then incrementing if MySQL won't start. Setting to 4, 5 or 6 can delete your data so be carefull and read http://dev.mysql.com/doc/refman/5.7/en/forcing-innodb-recovery.html before.
2) Restart MySQL service. Only SELECT will run and that's normal at this point.
3) Dump all your databases/schemas with mysqldump one by one, do not compress the dumps because you'd have to uncompress them later anyway.
4) Move (or delete!) only the bd's directories inside /var/lib/mysql, preserving the individual files in the root.
5) Stop MySQL and then uncomment the line added in 1). Start MySQL.
6) Recover all bd's dumped in 3).
Good luck!
I uninstalled mysql in Ubuntu 16.04 https://askubuntu.com/questions/172514/how-do-i-uninstall-mysql
I reinstalled mysql
https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-16-04
This seemed to work.
First create dir /var/run/mysqld
with command:
mkdir -p /var/run/mysqld
then add rigths to the dir
chown mysql:mysql /var/run/mysqld
after this try
mysql -u root
You must install mysql-server
apt install mysql-server

Recover MySQL /var/lib/mysql after update

I did an update from Mysql 5.5 to 8.0 in a Centos 6 server without dumping the databases to a .sql file, I just copied the /var/lib/mysql directory to another location.
Now if I try to load mysqld service it crashes.
Being naive I deleted all the content of /var/lib/mysql and installed the service again, now it runs but now I do not know how to manually import the DB files to the directory (copy & paste of the folder does not work) in order to do a check of the DB and/or repair it.
rysnc should likely already be installed, but if it isn't you would:
sudo yum install rsync
Then
#make sure mysql isn't running
sudo service mysqld stop
#double check that there is no MySQL PID running
sudo ps aux | grep mysql
#move the new MySQL 8.0 data files out of the way
sudo mv /var/lib/mysql /var/lib/mysql.bak
#copy the original data files back to /var/lib/mysql
#note that the trailing / is required for both paths
sudo rsync -av /path/to/original/mysql/db/files/ /var/lib/mysql/
#change user and group ownership to mysql
sudo chown -R mysql:mysql /var/lib/mysql
#start MySQL
sudo service mysqld start
#run mysql_upgrade, replace {usernamehere} with the actual username
sudo mysql_upgrade -u {usernamehere} -p
Update:
You will need to downgrade the binaries to 5.5. Take a backup of each database once MySQL is running again and then upgrade in the following order:
5.6
5.7
8.0
You will need to run mysql_upgrade after each upgrade.

Steps to install MariaDB (MySQL) database files in NFS share, and run the service as a regular user (Linux)

I've been looking for instructions but couldn't find a complete instruction set for the following:
Install MariaDB (MySQL) database and logs in an NFS share
Run MariaDB daemon as a regular user defined in NIS
Using:
Linux Ubuntu 14.04
MariaDB 10.2.5
After reading many posts, and doing some trial & error, I came up with the steps listed below. I'd like to know if something is missing or wrong ??
1- Create directory in NFS:
mkdir /nfs_dir/data -p
mkdir /nfs_dir/logs -p
chmod 700 /nfs_dir/data
chmod 750 /nfs_dir/logs
chown nisuser:nisgroup ...
2- Crate run dir in local drive:
sudo mkdir /var/run/mariadb
sudo chown nisuser:nisgroup /var/run/mariadb
sudo chmod 755 /var/run/mariadb
3- Install MariaDB
Instructions from download.mariadb.org
4- sudo service mysql stop
5- Create database files in NFS:
sudo mysql_install_db --user=nisuser --datadir/nfs_dir/data
6- Edit MariaDB config (below, only the edited lines are shown, others left as default):
sudo vi /etc/mysql/my.cnf
[client]
socket = /var/run/mariadb/m.sock
[mysql_safe]
socket = /var/run/mariadb/m.sock
[mysqld]
user = nisuser
pid-file = /var/run/mariadb/m.pid
socket = /var/run/mariadb/m.sock
datadir = /nfs_dir/data
log_warnings = 1
log_error = /nfs_dir/logs/error.log
log_bin = /nfs_dir/logs/mysql-bin.log
7 - Update the MySQL Init script
sudo vi /etc/init.d/mysql
Replace the test line as follows:
test -e /var/run/mariadb || install -m 755 -o nisuser -g nisgroup -d /var/run/mariadb
8 - Update Debian config file
sudo /etc/mysql/debian.cnf
[client]
socket = /var/run/mariadb/m.sock
[mysql_upgrade]
socket = /var/run/mariadb/m.sock
9- Create debian-sys-main user in mysql > user table
create user 'debian-sys-main'#'localhost' identified by 'password_from_debian_file'; GRANT ALL PRIVILEGES ON *.* to 'debian-sys-main'#'localhost' with GRANT OPTION;
10 - Restart MariaDB
sudo service mysql restart

Changing the tmp folder of mysql

Our Mysql queries use temporary tables which creates temporary files in the process. Currently the files are written to /tmp. How exactly can the path of the temp folder to which mysql writes to be changed?
You should edit your my.cnf
tmpdir = /whatewer/you/want
and after that restart mysql
P.S. Don't forget give write permissions to /whatewer/you/want for mysql user
Here is an example to move the mysqld tmpdir from /tmp to /run/mysqld which already exists on Ubuntu 13.04 and is a tmpfs (memory/ram):
sudo vim /etc/mysql/conf.d/local.cnf
Add:
[mysqld]
tmpdir = /run/mysqld
Then:
sudo service mysql restart
Verify:
SHOW VARIABLES LIKE 'tmpdir';
==================================================================
If you get an error on MySQL restart, you may have AppArmor enabled:
sudo vim /etc/apparmor.d/local/usr.sbin.mysqld
Add:
# Site-specific additions and overrides for usr.sbin.mysqld.
# For more details, please see /etc/apparmor.d/local/README.
/run/mysqld/ r,
/run/mysqld/** rwk,
Then:
sudo service apparmor reload
sources: http://2bits.com/articles/reduce-your-servers-resource-usage-moving-mysql-temporary-directory-ram-disk.html, https://blogs.oracle.com/jsmyth/entry/apparmor_and_mysql
This is answered in the documentation:
Where MySQL Stores Temporary Files
On Unix, MySQL uses the value of the TMPDIR environment variable as
the path name of the directory in which to store temporary files. If
TMPDIR is not set, MySQL uses the system default, which is usually
/tmp, /var/tmp, or /usr/tmp.
On Windows, Netware and OS2, MySQL checks in order the values of the
TMPDIR, TEMP, and TMP environment variables. For the first one found
to be set, MySQL uses it and does not check those remaining. If none
of TMPDIR, TEMP, or TMP are set, MySQL uses the Windows system
default, which is usually C:\windows\temp.
if you dont have apparmor or selinux issues, but still get errorcode 13's:
mysql must be able to access the full path. I.e. all folders must be mysql accessible, not just the one you intend in pointing to.
example, you try using this in your mysql configuration: tmp = /some/folder/on/disk
# will work, as user root:
mkdir -p /some/folder/on/disk
chown -R mysql:mysql /some
# will not work, also as user root:
mkdir -p /some/folder/on/disk
chown -R mysql:mysql /some/folder/on/disk
This maybe helpful for MySql with AppArmor
stop mysql :
sudo /etc/init.d/mysql stop
Create directory called /somewhere/tmp
Edit Config:
sudo vim /etc/mysql/my.cnf # or perhaps sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
change
tmpdir = /somewhere/tmp/
Then
sudo vim /etc/apparmor.d/usr.sbin.mysqld
Add
# Allow data dir access
/somewhere/ r,
/somewhere/** rwk,
sudo chown -R root:root /somewhere
sudo chmod -R 1777 /somewhere
Restart
sudo /etc/init.d/apparmor reload
sudo /etc/init.d/mysql restart
You can also set the TMPDIR environment variable.
In some situations (Docker in my case) it's more convenient to set an environment variable than to update a config file.
Works for 5.7 on centos 8
mkdir /tmp/1 /tmp/1
semanage fcontext -a -t mysqld_db_t "/tmp/1(/.*)?"
restorecon -Rv /tmp/1
semanage fcontext -a -t mysqld_db_t "/tmp/2(/.*)?"
restorecon -Rv /tmp/2
to my.cnf tmpdir=/tmp/1:/tmp/2
sudo service mysql restart
If you are a MariaDB user, all this above apply, by don't forget to unlock the "home" protection by doing this.
touch /etc/systemd/system/mariadb.service.d/override.conf
nano /etc/systemd/system/mariadb.service.d/override.conf
Inside override.conf put this content and save.
[Service]
ProtectHome=false
Then run the following commands :
systemctl daemon-reload
/scripts/restartsrv_mysql
After restarting mysql, the variables can be checked by :
mysqladmin variables|grep tmp

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

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.
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.
Uninstall mysql using yum remove mysql*
Recursively delete /usr/bin/mysql and /var/lib/mysql
Delete the file /etc/my.cnf.rmp
Use ps -e to check the processes to make sure mysql isn't still running.
Reboot server with reboot
Run yum install mysql-server. This also seems to install the mysql client as a dependency.
Give mysql ownership and group priveleges with:
chown -R mysql /var/lib/mysql
chgrp -R mysql /var/lib/mysql
Use service mysqld start to start MySQL Daemon.
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!
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.
mysql_install_db –-user=mysql --ldata=/var/lib/mysql
Worked for me in Centos 7
initialize mysql before start on windows.
mysqld --initialize
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.
Just this command is enough to do the magic on centos 6.6
mysql_install_db
I just met the same problem with mysql 5.7 on OSX:
rm -rf {datadir}
mysqld --initialize --datadir {datadir}
mysqld --datadir {datadir}
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.
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.
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.
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.
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.
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.
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
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
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.