Suppress warning output in bash - mysql

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
I tried adding 2>/dev/null, &>/dev/null, etc, nothing seemed to suppress the warnings.

mysql_tzinfo_to_sql /usr/share/zoneinfo 2>/dev/null | mysql -u root mysql
The command that is producing the error output to STDERR is the first command, not the second one. Put the STDERR redirection before the pipe, and this should fix your problem.

Better give your exact code attempt and warnings in your original post, but if you try this one :
{ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql; } 2>/dev/null
or
mysql_tzinfo_to_sql 2>/dev/null /usr/share/zoneinfo |
mysql -u root mysql 2>/dev/null
that should work.

Try enclosing it on a subshell
( mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql ) &>/dev/null

Related

Making a copy of a existing database with out data in it

I want to copy a Database without copying its data, I mean I just want to copy the stucture and tables and foreign key and ... not the data in it.
The answer is here but I do not know where should I copy it ? In shell? In workbench? In query?
I entered it in query in workbenck and it has error !
Thank you in advance!
Edit
When I run it in my mysql shell I get this:
MySQL JS > mysqldump -u myusername -pmypassword -d olddb | mysql -u myusername -pmypassword -D newdb
SyntaxError: Unexpected identifier.
You'll need to run it on the command line for your OS (not the shell for MySQL as you tried earlier).
Under Linux (including Macs) it would look something like:
smm#smm-HP-ZBook-15-G2:~/$ mysqldump -u myusername -pmypassword -d olddb | mysql -u myusername -pmypassword -D newdb
Under Windows:
C:\> mysqldump -u myusername -pmypassword -d olddb | mysql -u myusername -pmypassword -D newdb
This is assuming mysqldump is in the PATH for your command line (it isn't if you get a command not found error). How to use a command line and set up the PATH depends on the OS and is beyond the scope of this answer.
Refer following links..
1) Create dump file
2) Reload dump file

How to run multiple file .sql with mysql?

I tried to run this command in shell:
mysql> source /path/*.sql
But I did not have any results.
Do you have any suggestions what is wrong?
You can try to use Bash like this:
cat *.sql | mysql
or
cat script*.sql | mysql -u root -pmypassword yourdatabase

Backup mysql databases into self contained files

I have a linux system with Mysql contain more than 400 databases,I need to export each databases as a single *.sql file.Is it possible to do this with mysql_dump or Mysqlworkbench.
I have tried mysql_dump with --all-databases option.but this make a file with all database.it is large in size.
One way to achieve this is to write a bash script: (Source)
#! /bin/bash
TIMESTAMP=$(date +"%F")
BACKUP_DIR="/backup/$TIMESTAMP"
MYSQL_USER="backup"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="password"
MYSQLDUMP=/usr/bin/mysqldump
mkdir -p "$BACKUP_DIR/mysql"
databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`
for db in $databases; do
$MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/mysql/$db.gz"
done
For more information, have a look at this similar question.

Conditional commands in ssh & shell & mysql

I have the following commands in a shell script where I do a mysql dump, then I load that SQL file over ssh into a remote database, and then I update the timestamp.
1. mysqldump -u root files path | gzip -9 > $SQL_FILE
2. cat $SQL_FILE | ssh -i ~/metadata.pem ubuntu#1.2.3.4
"zcat | mysql -u 'root' -h 1.2.3.4 metadata"
3. TIMESTAMP=`date "+%Y-%m-%d-%T"`
4. mysql -u 'root' -h 1.2.3.4 metadata -e "UPDATE path_last_updated SET timestamp=DEFAULT"
Is there any way to improve the above commands. For example, what happens if line 2 fails (for example, due to a connectivity issue), but line 4 succeeds?
How would I make line 4 running conditional on the success of line 2?
You could chain all in one block:
mysqldump -u root files path |
gzip -9 |
ssh -i ~/metadata.pem ubuntu#1.2.3.4 "zcat |\
mysql -u 'root' -h 1.2.3.4 metadata" &&
mysql -u 'root' -h 1.2.3.4 metadata -e "
UPDATE path_last_updated SET timestamp=DEFAULT"
So last mysql command won't be executed if something fail before.
You can use $? for get return code of last command, if it's not 0, it failed.
Or you can use && for example : cmd1 && cmd2.
Or you can use set -e to stop script if occur an error.

How do I load a sql.gz file to my database? (importing)

is this right?
mysql -uroot -ppassword mydb < myfile.sql.gz
No, it isn't. The right way would be
zcat myfile.sql.gz | mysql -u root -ppassword mydb
Note there can be no space between the -p and password if using the -p syntax, refer http://dev.mysql.com/doc/refman/5.5/en/mysql-command-options.html#option_mysql_password
Use the following command:
gunzip < databasefile.sql.gz | mysql -u root -p dbname
You must not use the password directly in the terminal, use without it like follows
zcat YOUR_FILE.sql.gz | mysql -u YOUR_DB_USERNAME -p YOUR_DATABASE_NAME
Hit enter and when terminal asked for your password, type your password and hope everything will work fine.
Straight and clear:
gunzip -c myfile.sql.gz | mysql -uroot -ppassword mydb
-c option for gunzip writes to stdout, keeps original files
NOTE: You shouldn't put the password directly in the command. It's better to provide just -p and than enter the password interactively.
For Generating dbName.sql.gz
mysqldump -u <YOUR USERNAME> -p<YOUR PASSWORD> <YOUR DBNAME> | gzip > ~/mysqlBackup/dbName_`date +%Y%m%d%H%M`.sql.gz
For Loading dbName.sql.gz
zcat ~/mysqlBackup/<.SQL.GZ file> | mysql -u <YOUR USERNAME> -p<YOUR PASSWORD> <DATABASE NAME IN WHICH YOU WANT TO LOAD>
On windows you can do this:
First step! Install gzip for windows. I recommend using chocolatey to do it: (https://chocolatey.org/install)
choco install gzip -y
Now you can descompress your gz file and send it to mysql:
gzip -cd backup.sql.gz > mysql -uUSER -pPASSWORD -hLOCALHOST DATABASE
Good luck!
You have to follow below steps:
First check Mysql service should be running.
Then if you have compressed file, decompress it first.
Then you will find .sql file after decompressing.
Then find import data in left corner in Mysql.
Select option import from self-contained file and select
your .sql file and specify a new schema name.
Then click on import data.
After importing you will see your new schema in
available schema list.