Changing the tmp folder of mysql - 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

Related

Missing mysql on /etc/init.d/ directory

I've been trying to install mysql using WSL and I've followed the steps indicated in this guide https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-database. I tried running mysql --version and it worked (prompted mysql Ver 8.0.23 for Linux on x86_64 (MySQL Community Server - GPL)). But as I try the next command sudo /etc/init.d/mysql start, it says sudo: /etc/init.d/mysql: command not found. I've also tried checking the contents of /etc/init.d/ directory and there is no existing mysql file/folder there. What should I do next to proceed with my mysql installation?
Thanks!
There is a great blog post on this problem, with explanations of the issue and detailed solutions.
https://www.58bits.com/blog/2020/05/03/installing-mysql-80-under-wsl-2-and-ubuntu
From the post:
One solution is to download the mysql.server.sh script from here -
https://github.com/mysql/mysql-server/tree/8.0/support-files - and
then copy and rename the script to /etc/init.d/mysql (make sure that
it's also executable - chmod +x mysql)
You'll then need to set the default values for basdir, datadir and pid
file locations.
Here's an excerpt with the top portion of the file and the settings
that worked for me...
# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.
basedir=/usr
datadir=/var/lib/mysql
# Default value, in seconds, afterwhich the script should timeout waiting
# for server start.
# Value here is overriden by value in my.cnf.
# 0 means don't wait at all
# Negative numbers mean to wait indefinitely
service_startup_timeout=900
# Lock directory for RedHat / SuSE.
lockdir='/var/lock/subsys'
lock_file_path="$lockdir/mysql"
# The following variables are only set for letting mysql.server find things.
# Set some defaults
mysqld_pid_file_path=/var/run/mysqld/mysqld.pid
if test -z "$basedir"
After this you should be able to start and stop MySQL as follows:
sudo service mysql start
sudo service mysql stop
As he mentions later, you also need to create /var/run/mysqld/ and set permissions:
sudo mkdir /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld

lower_case_table_names Settings in MySQL 8.0.12

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=

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.

How can I create a mariadb drop in replacement for MAMP [duplicate]

I successfully installed mariadb, but MAMP continues to use the copy of mysql located in its bin folder; specifically:
/Applications/MAMP/Library/bin/mysql
How do I get MAMP to use mariadb, which in my case is located in /usr/local/bin/mysql?
I tried creating a symbolic link in MAMP's bin folder to point to /usr/local/bin, but that didn't work. Hmm.
MAMP uses MAMP/bin/startMysql.sh to start mysql. Try to change it.
here's how i do it so that you can use either mysql or mariadb since mariadb is a drop in replacement (typing this from memory, so please let me know if there are some mistakes)...
0) make a backup of your mysql db dir just in case, and do some mysql prep just in case
$ cp -R /Applications/MAMP/db/mysql /Applications/MAMP/db/mysql.2013-02-06-1850.bak
$ /Applications/MAMP/bin/repairMysql.sh
$ /Applications/MAMP/bin/quickCheckMysqlUpgrade.sh
$ /Applications/MAMP/bin/upgradeMysql.sh
1) make a copy or take note of some settings in your my.cnf file. It can be located in a variety of different places, so to find them all (there are a bunch):
$ locate my.cnf
/Applications/MAMP/conf/my.cnf
/etc/my.cnf
/usr/local/etc/my.cnf
/usr/local/etc/my.cnf.d
/usr/local/etc/my.cnf.d/client.cnf
/usr/local/etc/my.cnf.d/mysql-clients.cnf
/usr/local/etc/my.cnf.d/server.cnf
2) figure out which my.cnf was loaded (for MAMP, it MAY be in /Applications/MAMP/conf/my.cnf)
$ /usr/local/bin/mysql --help | grep my.cnf
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf
$ /Applications/MAMP/Library/bin/mysql --help | grep my.cnf
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /Applications/MAMP/conf/my.cnf ~/.my.cnf
3) make a backup of the my.cnf in /etc/my.cnf and edit my.cnf to make sure it's got a few parameters in there, most importantly the port, socket, and datadir settings so that mariadb will know where to look for your db files:
$ sudo cp /etc/my.conf /etc/my.cnf.2013-02-06-1858.bak
$ sudo vi /etc/my.cnf
port = 3306
socket = /Applications/MAMP/tmp/mysql/mysql.sock
datadir = /Applications/MAMP/db/mysql
tmpdir = /Applications/MAMP/tmp/mysql
4) add any mariadb specific config options you may want in a [mariadb] section
5) install mariadb (i like using brew, but pick your poison)... and you can really do this any time
$ brew install mariadb
6) make a symbolic link from the my.conf from step two
$ sudo ln -s /Applications/MAMP/conf/my.cnf /etc/my.cnf
6a) you can put your my.cnf anywhere, as long as there's a copy or link to it in /etc/my.cnf... the goal here is to have mariadb and MAMP's implementation of mysql use the same config settings.
7) now make a shell shell script to load apache and mariadb
$ mkdir -p ~/scripts/mamp
$ touch ~/scripts/mamp/startSomething.sh ~/scripts/mamp/stopSomething.sh
$ chmod ug+rx ~/scripts/mamp/*Something.sh
8) get/take note of the current start/stop script for apache (it'll prob won't be anything fancy)
$ more /Applications/MAMP/bin/startApache.sh
$ more /Applications/MAMP/bin/stopApache.sh
9) get the installed mariadb path, and make sure it's the mariadb version
$ which mysql
/usr/local/bin/mysql
$ mysql --version
mysql Ver 15.1 Distrib 5.5.29-MariaDB, for osx10.8 (i386) using readline 5.1
10) now edit startSomething.sh
# /bin/sh
/Applications/MAMP/Library/bin/apachectl start
/usr/local/bin/mysql.server start &
11) do the same for stopSomething.sh
# /bin/sh
/Applications/MAMP/Library/bin/apachectl stop
/usr/local/bin/mysql.server stop &
12) that's it!. to start or stop things
$ ~/scripts/mamp/startSomething.sh
$ ~/scripts/mamp/stopSomething.sh
if you want the vanilla MAMP, use the MAMP app that came with MAMP. otherwise, have fun with this slightly faster database with a bunch of fun new features... but keep in mind that while mariadb is by design a drop in replacement for mysql, it's not true the other way around (MariaDB v MySQL compatibility)

MySQL Job failed to start

I'm on Kubuntu 12.04, and after installing mysql via an apt-get (mysql ver: 5.5.35), i'm trying to start mysql service, but I got this error:
sudo service mysql start
start: Job failed to start
So I googled this problem, it says i have to go to the /var/log/mysql/error.log
But my error.log file is empty :(
Then I checked the permissions
:
drwxr-s--- 2 mysql adm 4096 Apr 7 11:21 mysql
-rw-r----- 1 mysql adm 0 Apr 7 11:21 error.log
So I don't know what to do... Why this error ? Why is the error file empty ?
First make a backup of your /var/lib/mysql/ directory just to be safe.
sudo mkdir /home/<your username>/mysql/
cd /var/lib/mysql/
sudo cp * /home/<your username>/mysql/ -R
Next purge MySQL (this will remove php5-mysql and phpmyadmin as well as a number of other libraries so be prepared to re-install some items after this.
sudo apt-get purge mysql-server-5.1 mysql-common
Remove the folder /etc/mysql/ and it's contents
sudo rm /etc/mysql/ -R
Next check that your old database files are still in /var/lib/mysql/ if they are not then copy them back in to the folder then chown root:root
(only run these if the files are no longer there)
sudo mkdir /var/lib/mysql/
sudo chown root:root /var/lib/mysql/ -R
cd ~/mysql/
sudo cp * /var/lib/mysql/ -R
Next install mysql server
sudo apt-get install mysql-server
Finally re-install any missing packages like phpmyadmin and php5-mysql.
My problem was running out of memory. Digital ocean has great instruction for adding swap memory for Ubuntu: https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04
This solved the issue and enabled me to restart the Mysql that otherwise would not start.
Reinstallation will works because it will reset all the value to default. It is better to find what the real culprits (my.cnf editing mistake does happens, e.g. bad/outdated parameter suggestion during mysql tuning.)
Here is the mysql diagnosis if you suspect some value is wrong inside my.cnf : Run the mysqld to show you the results.
sudo -u mysql mysqld
Afterwards, fix all the my.cnf key error that pop out from the screen until mysqld startup successfully.
Then restart it using
sudo service mysql restart
In my case, it simply because the disk is full.
Just clear some disk space and restart and everything is fine.
In most cases, just purging the mysql-server package and re-installing it will do the job.
Run,
sudo apt-get purge mysql-server-5.1 mysql-common
followed by
sudo apt-get install mysql-server
This line did solve the issue in my case,
sudo apt clean
In my case, i do:
sudo nano /etc/mysql/my.cnf
search for bind names and IPs
remove the specific, and let only localhost 127.0.0.1 and the hostname
Check the file permissions, if edited
Fail:
$ sudo chmod 776 /etc/mysql/my.cnf
$ sudo service mysql restart
mysql stop/waiting
start: Job failed to start
Ok:
$ sudo chmod 774 /etc/mysql/my.cnf
$ sudo service mysql restart
stop: Unknown instance:
mysql start/running, process 9564
To help others who do not have a full disk to troubleshoot this problem, first inspect your error log (for me the path is given in my /etc/mysql/my.cnf file):
tail /var/log/mysql/error.log
My problem turned out to be a new IP address allocated after some network router reconfiguration, so I needed to change the bind-address variable.
In my case the problem was the /var/log disk full (check with df -h)
Just deleted some log files and mysql started, no big deal!
The given solution requires enough free HDD, the actual problem was the HDD memory shortage. So If you don't have an alternative server or free disk space, you need some other alternative.
I faced this error with my production server (Linode VPS) when I was running a bulk download into MySQL. Its not a proper solution but VERY QUICK FIX, which we often need in production to bring things UP FAST.
Resize our VPS Server to higher Hard Disk size
Start MySQL, it works.
Login to your MySQL instance and make appropriate adjustments that caused this error (e.g. remove some records, table, or take DB backup to your local machine that are not required at production, etc. After all you know, what caused this issue.)
Downgrade your VPS Server to previous package you was already using
In my case:
restart server
restart mysql
create .socket in directory
I had the same problem. But i discover that my hd is full.
$ sudo cat /var/log/upstart/mysql.log
/proc/self/fd/9: ERROR: The partition with /var/lib/mysql is too full!
So, I run
$ df -h
And I got the message
/dev/xvda1 7.8G 7.4G 0 100% /
Then I found out which folder was full by running the following command on the terminal
$ cd /var/www
$ for i in *; do echo $i; find $i |wc -l; done
This give me the number of files on each folder on /var/www. I logged into the folder with most files, and deleted some backup files, and i continued deleting useless files and cache files.
then I run $ sudo /etc/init.d/mysql start and it work again