Purpose is to output some data from MySQL to txt file with Ubuntu 18.04
MySQL outfile command
1 | SELECT * |
However, I spent hours trying to work out why this query gives me nothing. Here are the steps to solve it.
- secure_file_priv: it comes with aws ubuntu 18.04 and is used to limit data import and export operations. A file can be only exported to the path set by secure_file_priv.
To see the current setting, login to MySQL shell as root user
1 | mysql -u root -p |
Then:
1 | SHOW VARIABLES LIKE "secure_file_priv"; |
The result:
1 | +------------------+-----------------------+ |
It means MySQL can only export to /var/lib/mysql-files/
But, I want it to export to /var/www/html/tmp. First I need to make the direction and find mysql config files to change the secure_file_priv variable.
1 | mkdir /var/www/html/tmp |
Then
1 | mysql --help | grep "Default options" -A 1 |
Result:
1 | Default options are read from the following files in the given order: |
I open all three files, only /etc/mysql/my.cnf contains useful information and it’s responsible for global configuration. So this is the file to edit.
Open this file:
1 | sudo vim /etc/mysql/my.cnf |
Set the variable to:
1 | [mysqld] |
Restart mysql and check
1 | sudo /etc/init.d/mysql restart |
The output should look like:
1 | +------------------+-----------------------+ |
Now, mysql is set. But if we run the query above, we see
1 | ERROR 1 (HY000): Can't create/write to file '/var/www/html/temp/output.txt' (Errcode: 13 - Permission denied) |
- Solve permission issues
First we should give write and read permission to mysql
1 | chown mysql:mysql /var/www/html/tmp |
After this, we need to update AppArmor profile. AppArmor is a Linux Kernel security module that restrict or permit certain actions.
1 | cd /etc/apparmor.d |
You should be able to see “usr.sbin.mysqld”
1 | sudo vim usr.sbin.mysqld |
Add these two lines under “# Allow data files dir access”
1 | /var/www/html/tmp/ rw, |
Reparse the profile
1 | sudo apparmor_parser -r /etc/apparmor.d/usr.sbin.mysqld |
1 | mysql |
https://computingforgeeks.com/how-to-solve-mysql-server-is-running-with-the-secure-file-priv-error/
https://serverfault.com/questions/896653/how-do-i-get-the-right-apparmor-profile-for-mysql-on-ubuntu
赞赏一下
支付宝打赏
微信打赏