Database back-up as cronjob - mysql

I use this command to create .sql files of all tables in a huge mysql database:
mysqldump -h localhost --user=username -p --tab=/var/tmp database_name
The command above works perfectly but is prompting for the password.
Before i run this command i need to run:
GRANT FILE ON *.* TO 'username'#'%'
otherwise i get the error:
Got error: 1086: "File '/var/tmp/a_ab_text.txt' already exists" when executing 'SELECT INTO OUTFILE'
a_ab_text is the first table of the database.
Is there a way to get around this as I need to create a script that can be daily run as a cronjob.
To be clear a need a way to have the password in the script but typing the password after the --password '********' does not work.
And i need a way to overwrite the tables every day as we export all tables daily to a local server giving timestamp to the back-up file in order to have a dayly back-up version.
And of course i need a way to get around the command GRANT FILE ON *.* TO 'username'#'%' and restart the mysql server.
Thank you

Related

Trouble Restoring MYSQL Database on CentOS Using SSH

I just had to change [dedicated CentOs] servers at Godaddy.
After I uploaded my .sql database backup file to the site using FTP, I connected to my site using SSH.
After changing to super user using the su - command, i tried using the following code to restore my database:
[root#sXXX.XXX.XXX.XXX ~]# mysql -u alaskasl_destiny -p alaskasl_freakout < /home/alaskasl/backup/databases/alaskasl_freakout.sql
I get the following ERROR 1044 (42000): Access denied for user 'alaskasl_destiny'#'localhost' to database 'alaskasl_freakout'
I can't figure out what I am doing wrong. This command has always worked for me in the past
First and foremost, if alasas1... is your real username, I would replace it with 'username'. Never give that info out, especially on a public place like Stackoverflow.
That said, a few things to check
1. You need to know the root user's login credentials for this new mysql instance. If this is a new setup the user is root with no password, so you should be able to use the following command:
$ mysql -u root
Once you do get in to mysql, you will see a prompt similar to:
mysql>
at that prompt you can type
mysql> show databases;
and a list of databases will display. Does the database you expect to be in there in there?
if not, here is what you need to do:
mysql> CREATE DATABASE 'database_name';
mysql> CREATE USER 'username' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.database_name TO 'username';
mysql> exit
At this point what you did was create the database, create a user with a password in mysql, and granted access to that database for that user. Once you type exit, you should now be back at the command prompt.
Now you can run mysql -u username -p database_name < /path/to/sql_file.sql

Fail to run mysql script for other user than root

I have mysql installed on ubuntu server. There is a mysql script that has a select statement in it. I want to output the result of this select into .csv file.
If I run the following command
mysql -u root -pTHIS_IS_PASS MY-SCHEMA < /home/me/script.sql
things work out beautifully. I, however, I run
mysql -u OTHER-USER -pTHIS_IS_PASS MY-SCHEMA < /home/me/script.sql
I get this error:
ERROR 1045 (28000) at line 7: Access denied for user 'OTHER-USER'#'localhost' (using password YES)
I have no escaping characters in the password of OTHER-USER.
How can I fix this?
Found out the solution myself: I needed to grant FILE permission to the OTHER-USER.
GRANT FILE ON *.* TO 'OTHER-USER'#'localhost';
Don't forget that file permission is granted not to just one schema, but to the whole mysql.

mysql: revoke privilege "if exists"?

In a script, I want to make sure, a mysql user has no privileges at all on a specific database. So I do:
revoke all privileges on `testdb`.* from 'testuser'#'%'
But if the user does not have any privileges on this database I get this error:
There is no such grant defined for user 'testuser' on host '%'
What is absolutely correct, but my script throws an error now and stops. I do not want to make my script ignoring all errors from this statement.
So is there something like
revoke all privileges if exist ...
I could not find anything about that in the mysql manual.
The mysql command has an -f option which prevents the script from aborting on errors, so you might want to try this:
mysql -u myuser -f < script.sql
Of course this will also ignore other errors which you might not want to be ignored...
Also this unfortunately does not work in combination with the -e option. However if you run mysql from a bash script anyway this can easily be circumvented by using a heredoc:
mysql -u myuser -f <<EOF
REVOKE ALL PRIVILEGES ...
EOF

Copy remote database views to local database as tables

I currently have access to only the views on a remote database, and I'm trying to copy the data of those views locally so that I can work faster. I've tried to use mysqldump to create a .sql file and load that, but it doesn't seem to work.
To get the remote db:
$ mysqldump --single-transaction -u username -p -h somesite.com -P 32000 db_name > mysql.sql
To copy to local:
$ mysql -u root -p mydatabase < mysql.sql
But I get an error
ERROR 1146 (42S02) at line 668: Table 'db_name.some_view' doesn't exist
My local database is completely empty except me creating an empty one using the command create database mydatabase; in the mysql shell.
Am I getting this problem because I can't use this method in the first place since I only have access to the views in the remote database?
Did you taken the data from the remote system correctly.If not try this command once you connect through remotely to database server,the command is
mysqldump -u username -h 192.168.X.X -P password --routines>xxxx.sql
It will ask the password of the database.Till now data is only in database server.From your local system use this command to take the data from database server,the command is
rsync -vaH username#192.168.X.X:/datalocation /yourlocation
Now the data is on your local system.Then after create one database in your system.
mysql
create database;
grant all privileges on databasename .*to databaseschema#'%' identified by 'password';
flush privileges;
I think this may help you,if not sorry bro.

MYSQL error: 1045 (28000): Access denied

ruby on rails mysql production replication
This project is a cloned repo I want to be able to duplicate the production file for mysql database locally. I can run the application but, I'm missing a lot of assets. I have listed a few resources about the contents of the application below. Please let me know if I need to add anything else to better help you help me. Thanks
I have taken a look at this thread.. I ran all of the the commands as root, so I don't think that link applies to this issue.
.bash_profile
export HUB_test_DB="hub_test"
export HUB_test_USER="hub_test"
export HUB_test_PASS="whatevs"
export HUB_DEV_DB="hub_development"
export HUB_DEV_USER="hub_development"
export HUB_DEV_PASS="whatevs"
Mysql commands
mysql -u root -p THEROOTPASSWORD 28
CREATE USER 'hub_test'#'localhost' IDENTIFIED BY 'whatevs';
CREATE USER 'hub_development'#'localhost' IDENTIFIED BY 'whatevs';
grant all privileges on hub_test.* to hub_test#localhost;
grant all privileges on hub_development.* to hub_development#localhost;
flush privileges;
The test db isn't really used yet.
Command to duplicate database
mysql -u $HUB_DEV_USER -p $HUB_DEV_DB < db/backup/production-backup-latest.sql
Mysql> show databases;
Database
-information_schema
mysql
performance_schema
test
Users and Privileges