How can I have MySQL write outfiles as a different user? - mysql

I'm working with a MySQL query that writes into an outfile. I run this query once every day or two and so I want to be able to remove the outfile without having to resort to su or sudo. The only way I can think of making that happen is to have the outfile written as owned by someone other than the mysql user. Is this possible?
Edit: I am not redirecting output to a file, I am using the INTO OUTFILE part of a select query to output to a file.
If it helps:
mysql --version
mysql Ver 14.12 Distrib 5.0.32, for pc-linux-gnu (x86_64) using readline 5.2

The output file is created by the mysqld process, not by your client process. Therefore the output file must be owned by the uid and gid of the mysqld process.
You can avoid having to sudo to access the file if you access it from a process under a uid or gid that can access the file. In other words, if mysqld creates files owned by uid and gid "mysql"/"mysql", then add your own account to group "mysql". Then you should be able to access the file, provided the file's permission mode includes group access.
Edit:
You are deleting a file in /tmp, with a directory permission mode of rwxrwxrwt. The sticky bit ('t') means you can remove files only if your uid is the same as the owner of the file, regardless of permissions on the file or the directory.
If you save your output file in another directory that doesn't have the sticky bit set, you should be able to remove the file normally.
Read this excerpt from the man page for sticky(8):
STICKY DIRECTORIES
A directory whose `sticky bit' is set becomes an append-only directory, or, more accurately, a directory in which the deletion of files is restricted. A file in a sticky directory may only be removed or renamed by a user if the user has write permission for the directory and the user is the owner of the file, the owner of the directory, or the super-user. This feature is usefully applied to directories such as /tmp which must be publicly writable but should deny users the license to arbitrarily delete or rename each others' files.

Not using the "SELECT...INTO OUTFILE" syntax, no.
You need to run the query (ie client) as another user, and redirect the output. For example, edit your crontab to run the following command whenever you want:
mysql db_schema -e 'SELECT col,... FROM table' > /tmp/outfile.txt
That will create /tmp/outfile.txt as the user who's crontab you've added the command to.

I just do
sudo gedit /etc/apparmor.d/usr.sbin.mysqld
and add
/var/www/codeigniter/assets/download/* w,
and
sudo service mysql restart
And that's it, I can do easily SELECT INTO OUTFILE any filename

If you have another user run the query from cron, it will create the file as that user.

Related

Unable to Start MySql (8.0.15) on OS-X High Sierra (10.13.6) PID File

Starting on PHP/MySQL Development and as the title states, downloaded MySQL 8.0.15 DMG. Installed. However, when I attempt to run the terminal command:
"sudo /usr/local/mysql/support-files/mysql.server.start"
I get:
"Starting MysQl .......... "
which finally ends in:
ERROR! The server quit without updating PID file (/usr/local/mysql/data/Macbook.hsd.comcast.net.pid)
So far I have tried:
Tried to remove the "Macbook.hsd.comcast.net.pid" file located in the data directory but all i keep getting a "No Such file or directory"
To look for default "my.cnf" file to mod or to add the default values, HOWEVER I read that: "By default the OS X installation does not use a my.cnf, and My SQL just uses the default values. To set up your own my.cnf, you could just create a file straight in /etc.
An "auto.cnf" exists in the data folder, i'm not sure if MySQL uses that or would respect a "my.cnf" created in the /usr/local/sql/data/ folder.
I'm not sure of the proper location to create a "my.cnf" (see, all I want to do is set the proper value for the "pid-file" such as pid-file = /var/run/mysqld/mysqld.pid and then create the directory and give it the proper permissions. SEE UPDATE AT BOTTOM:
I'm cannot access the "data" folder in MySQL folder (which is alias of): "/usr/local/mysql-8.0.15-macos10.14-x86_64/". It keeps telling me I don't have permission. There is a little red dot
I have set checked the permissions in "/user/local/mysql/" and did a
"sudo chown -RL root:mysql /user/local/mysql"
"sudo chown -RL _mysql:mysql /usr/local/mysql/data"
In the /mysql/ directory now everything reads:
Stumped and not sure where to move on from here.
Is the fact that I can't access the data folder a result because I haven't made one/specified one? Can't seem to find an answer for getting MySQL working with HighSierra/Mojave. I can't believe that it's just dead.
I would appreciate any/all help.
UPDATE/EDIT:
Was able to add my user permissions to the "/user/local/mysql/data" folder and was finally able to access the error file ("Macbook.hsd.comcast.net.err") and a "auto.cnf".
The "auto.cnf" file is empty besides a single line: [auto] server-uuid=e7986494-331d-11e9-9691-bec9b3249da6 (not sure if I need to modify the auto.cnf and add PID entries). See error BELOW which seems to indicate otherwise.
The Error file had repeated Errors (occurred several Times):

mysql folder inaccessible on Ubuntu

I am trying to reset my MySQL root password following the official reference here.
In step #2, I have to do the following
Locate the .pid file that contains the server's process ID. The exact
location and name of this file depend on your distribution, host name,
and configuration. Common locations are /var/lib/mysql/,
/var/run/mysqld/, and /usr/local/mysql/data/. Generally, the file name
has an extension of .pid and begins with either mysqld or your
system's host name.
So I go to /var/lib/ and find the mysql folder. I double-clicked it, I got the following pop-up window:
The folder contents could not be displayed.
You do not have the permissions necessary to view the contents of "mysql".
I am pretty sure that I am indeed the system admin. Why is it like so and how to fix it?
Start with working with the terminal/console as a root user.
Not a system expert - but it should get you somewhere:
Get into the ubuntu terminal/console
switch to the root user (sudo bash)
Then follow this one :
https://help.ubuntu.com/community/MysqlPasswordReset

mysql: SOURCE error 2?

When I tried to source for a particular .sql file, namely 'metropolises.sql' that I created and saved previously from a database, the following error is displayed:
Failed to open file 'metropolises.sql', error: 2
Any idea what went wrong?
Assuming you mean that you are trying to use the source command in order to execute SQL statements from a text file, the error number given appears to be passed through from the POSIX layer.
Therefore, using this resource, we can deduce that the error value of 2 means "no such file or directory".
In short, you got the path wrong.
Try providing an absolute path, as it's not clear what the current working directory will be in the context of your MySQL server. You may be assuming that it's the working directory of your shell, but it's not obvious that we should expect this to be true.
Just use the absolute path of the file and then, instead of using backslashes, use forward slashes.
Example:
with backslashes : source C:\folder1\metropolises.sql
with forward slashes : source C:/folder1/metropolises.sql
IF YOU ARE USING MYSQL INSIDE DOCKER
Note that if you are running MySQL inside docker then you must first copy the dump into your MySQL docker environment. To do that follow the steps below
First, check and copy the container ID for your MySQL docker by:
sudo docker ps
Copy the SQL dump file into your container using:
sudo docker cp /path/to/sql/file.sql MysqlDockerID:/
This will copy the dump file into the docker root folder if you want to copy the file inside any other directory/path inside docker replace the '/' after 'MysqlDockerID:' with the path you want appropriate one.
Now to interact with MySQL inside a running container run the following command:
sudo docker exec -it MysqlDockerID bin/bash
Now connect to the MySQL using the terminal by:
mysql -u yourUserName -p
This will now ask you for the password. Enter the correct password to proceed.
List the databases available by:
show Databases;
This will list out the available databases
Assuming your database name where you want to import dump to is 'MyDatabase'. Switch to that using:
use MyDatabase
Now you can import the file by typing:
source file.sql
Remember the above command works if you have copied your file into your root folder (using step 2). If you have copied it to any other path make sure you use that same path instead
Related issue I had getting error 2 running source command: filename must not be in quotes even if it contains spaces in name or path to file.
It's probably the file path to your file. If you don't know the exact location of the file you want to use, try to find your file in Finder, then drag the file into Terminal window
mysql> SOURCE dragfilePathHere
I first reach to the file
c:\windows>cd c:\akura\Db Scripts
c:\akura\Db Scripts>mysql -u root -p root
mysql>\. EXECUTER_NEW_USER.sql
here EXECUTER_NEW_USER.sql my file name
I've had the same error on Windows.
I solved it with (after on cmd: mysql -u root):
mysql> SOURCE C:/users/xxx/xxxx/metropolises.sql;
Be sure you type the right file path
On my windows 8.1, and mysql 5.7.9 MySQL Community Server (GPL),
I had to remove the ; after the file path.
This failed: source E:/jokoni/db/Banking/createTables.sql;
This Worked: source E:/jokoni/db/Banking/createTables.sql
(without termination, and forward slashes instead of windows' backslashes in path)
If you are using vagrant ensure that the file is on the server and then use the path to the file. e.g if the file is stored in the public folder you will have
sql> source /var/www/public/xxx.sql
Where xxx is the name of the file
I got this error in mysql command line using this query:
source `db.sql`;
I changed the above to the following to make it work:
source db.sql;
On my Mac, this is the only solution worked for me.
https://stackoverflow.com/a/45530305/5414448
1 - Download the .sql file and remember it's location.
2 - Open your mysql from command prompt or terminal.
3 - Create a database with the same name as that of the database present in the .sql file (create database your_database_name)
4 - Now exit out from the mysql command line client
5 - Now try and execute this command =>
mysql -u your_username -p your_database_name < your_sql_file_with_complete_location
example - mysql -u root -p trial < /home/abc/Desktop/trial.sql
here my .sql file is named trial and is present in the desktop, the database is also name trial
6 - You should now have your sql file imported to the corresponding mysql database.
I was having this issue and it turns out if you are using wamp server to run mysql, you have to use the file path within the wamp64 folder. So when the absolute path is: C:/wamp64/www/foldername/filename.sql
The path you have to use is:
www/foldername/filename.sql
May be the file name or path you are used may be incorrect
In my system i created file abcd.sql at c:\
and used command mysql> source c:\abcd.sql Then i got result
If you're on Debian 8 (Jessie) Linux, try to cd into the directory of the 'metropolises.sql'. Run mysql and execute SOURCE ./metropolises.sql;
Basically, try the relative path. I tried this and it works.
If you are running dockerized MySQL container such as ones from this official Docker Image registry: https://hub.docker.com/_/mysql/ You may encounter this issue as well.
For me it was because the file was located on a shared drive and it could not access, for some reason, to that path.
I cut the file and put it on my local drive in a path without spaces and it got resolved.
I got the same error when i used the command source and gave the sql file path by drag n dropping it.
Then I just had to remove those single quotes which appeared by default with drag and drop, a space before file extension and it worked.
soln:
source /home/xyz/file .sql ;(path and a space before file extension)
For Mac users, you can give the path as,
source /Users/YOUR_USER_NAME/Desktop/metropolises.sql;
*I have assumed that the file you need to source is in your desktop and the file name is metropolises.sql
If you type,
source metropolises.sql
then the file will be look in the default location,
/Users/YOUR_USER_NAME/metropolises.sql;
The solution for me was file permissions in Windows. Just give full control in the file to all users and it will work. After the import, get the permissions back to what it was before.
I had a problem on my Mac because one of the folder (directory) names in the path had a space in the name. BTW, the space was escaped by a \, but that was not understood by mySQL
solution - 1) Make sure you're in the root folder of your app. eg app/db/schema.sql.
solution - 2) open/reveal the folder on your window and drag&&drop in the command line next to keywork source (space) filesource. eg source User/myMAC/app/db/schema.sql
I get into this problem in my Xubuntu desktop. I fixed it by renaming all my files and folders so there is no space in the file path.
Passing the full path does not error, but if the folders have MySQL spaces it does not recognize the .sql file.
I have MySQL 8.0.21 on OS UNIX.
I've got the same error on Windows. I solved it running the code on MySQL command line
source c:\Users\xx\Downloads\data_file.sql
Remove spaces in the folder names of the path, It worked for my mac path.
(Eg: change the folder name MySQL Server 5.1 to MySQLServer5.1)
I also got the same message when I try from the MySQL console. However, when I open the command prompt and do the same steps it works.
C:\Users\SubhenduD>cd ../
C:\Users>cd ../
C:\>cd \xampp\mysql\bin
C:\xampp\mysql\bin>mysql -u -root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 68
Server version: 5.6.16 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use balticktravels;
mysql> source balticktravels.sql;

Retrieve lost file using Vi in MySQL

I would like to know how to retrieve a file using Vi in MySQL. I logged in using:
mysql -uuser -p -hserver -A database
Then I do:
\e
The editor opens and I type my query of 200 lines, then I :wq and \G (if I save the file it says: /tmp/sql9SbYQZ saved) and I see the result.
Now, if I make a mistake or run a different query and I try to type \e again, the query is lost.
ll /tmp/sql9SbYQZ
ls: /tmp/sql9SbYQZ: No such file or directory
Is there a way to retrieve the lost file?
Here's what I added to my .vimrc in order to save the current query in case i made a mistake.
nmap <F7> :w! /tmp/query.sql\| wq!<CR>
This will create a map to the F7 key (you can change it of course). So every time you open a file either using edit or \e, you change it use the F7 key.
This will save a backup of your current query to /tmp/query.sql and then save and close the temporary file. This way, if you make a mistake, you just re-open the backup file and try again.
Here's also a link you might like: http://vim.wikia.com/wiki/Open_the_last_edited_file
With the vi/m editor used with mysql, crontab, and many others, the work is done in a tmp file, as you see from your messages.
Edit (Big doah!, remove cruft about ls -l /tmp/..., you already did that!)
In the future the solution is to tell vim to w the buffer to a file name of your chosing, i.e.
w! /home/you/scripts/mysql2.sql
Then close the editor with
q
Note you may not need the ! after w.
I hope this helps.
Here is something you can try:
In Linux, do the following
$ cd
$ cp .mysql_history mystuff.txt
$ vi mystuff.txt
You should see the file .mysql_history. The mysql client records all queries and commands executed. Hopefully, your query is in there.
Give it a Try !!!

MySQL tables on external hard drive

I have a large amount of text data I need to import into MySQL. I'm doing this on a MacBook and don't have enough space for it so I want to store it in an external hard drive (I'm not really concerned about speed at this point - this is just for testing).
What's the best way to do it?
Install MySQL on the external hard drive (is this possible on a Mac?)
Install MySQL on the laptop's hard drive and have the tables on the external (how?)
One simple hack is to create an symbolic link replacing your current mysql database file location pointing to the external disk. Google symbolic link.
sample usage would be after you shutdown mysql, change the old mysql db folder name to something else, and create the symbolic link using the ln command like below
ln -s [EXTERNAL DRIVE PATH] [MYSQL DB FOLDER PATH]
Then move all the previous content of the mysql db folder to the new location.
Open /etc/mysql/my.cnf and find the value of the datadir. Alternatively, you can find this out in the mysql monitor with
mysql> select ##datadir;
Stop mysql
sudo systemctl stop mysql
Copy the data from there to your external drive
sudo rsync -av /var/lib/mysql /mnt/myHDD/somedir/mysql
Modify the location of the datadir in my.cnf.
Start mysql again
sudo systemctl start mysql
Verify that everything is still fine and remove the original data dir.
This page contains a more extensive guide but all the additional issues it warns about were not relevant for me on my raspberry PI. I.e. I skipped them and it worked.
For the second option, a tablespace might do the trick:
http://dev.mysql.com/doc/refman/5.1/en/create-tablespace.html
User user658991 answer is halfway there.
After adding the soft link, you will need to add the following line to /etc/apparmor.d/usr.sbin.mysqld beneath the 2 lines to the old mysql folder.
/path/to/mysql/folder/on/the/external/ r
/path/to/mysql/folder/on/the/external/ ** rwk
Without these 2 lines, MySQL fails to start complaining of:
Can't create test file /path/to/mysql/folder/on/the/external/hostname.lower-test
Can't create test file /path/to/mysql/folder/on/the/external/hostname.lower-test
mysqld: Can't change dir to '/path/to/mysql/folder/on/the/external/' (Errcode: 13)
Restart apparmor for the changes to take effect.
sudo invoke-rc.d apparmor restart
With this, MySQL starts normally.