I'm trying to execute this mysql command from a batch file:
mysql -f -utest -ppass db < alter1.sql
However, < is an escape character. I tried nesting it in double-quotes, but the double quotes end up appearing as part of the command.
I even put a carrot (^) in front of it, LOOKS fine in the prompt window, but mysql still gets that ^ passed to it, and doesn't execute the command.
Any suggestions? Thanks!
You could try:
type alter1.sql | mysql -f -utest -ppass db
Related
I've the following bash script to upgrade my database schema. Script reads hostname and database password from command line.
The problem is here that if the password is alphanumeric e.g r00t then script works. But if password contains special characters e.g pa**w0rd, then script does not work and directly exits. Please help me with this. Thanks.
#!/bin/bash
echo "Enter hostname."
read -p "Hostname [localhost]: " DB_HOST
DB_HOST=${DB_HOST:-localhost}
echo "Enter MySQL root password"
DB_PASS=
while [[ $DB_PASS = "" ]]; do
read -sp "Password: " DB_PASS
done
MYSQL="mysql --force --connect-timeout=90 --host=$DB_HOST -u root --password=${DB_PASS}"
# Apply schema updates. My DBName is "mydb"
# Upgrade schema file is stored in "mysql" folder
$MYSQL mydb -e exit > /dev/null 2>&1 && $MYSQL mydb < "../mysql/upgrade_schema_v.2.1.sql"
Logging into mysql using bash
For ubuntu or linux shell try to use command
mysql -u username -p'p#ssw()rD'
for remote host login use
mysql -h hostname -u user -p'password'
This is occurring because you are using shell GLOB (wildcard) characters in the password, and in Bash (or on Linux generally) wildcards are expanded by the shell.
The safest and most reliable solution is to not use shell wildcard characters or other characters interpreted by the shell in the password. You should also avoid spaces. There are plenty of other characters.
Here are the ones you should avoid:
" ' $ , [ ] * ? { } ~ # % \ < > | ^ ;
Here are the ones it is usually safe to use:
: # . , / + - ! =
To ensure the password is still secure, make it longer. As an example:
K#3amvv7l1wz1192sjqhym
This meets old-fashioned password complexity rules, because upper, lower, numbers and special characters are in the first four, the remainder is randomly generated but avoids any problematic characters.
However if you must use them, you can quote the password parameter with single quotes - though you will still run in to trouble if the password contains single quotes!
Try enclosing your password in single quotes.
If it's pa**w0rd, use 'pa**w0rd'
Variables are best used for data, not code. The layers of variables make it hard to protect the expansion when you want some parts of the expansion (i.e., you want your command line to be word split on the tokens you want), but don't want all the other effects. The solution is to not store the code in a string. Instead, use a function like:
do_mysql() {
host="$1"
pass="$2"
mysql --force --connect-timeout=90 --host="$host" -u root --password="$pass" "$#"
}
then you can run it with extra arguments like
do_mysql "$DB_HOST" "$DB_PASS" -e exit > /dev/null && do_mysql "$DB_HOST" "$DB_PASS" < "../mysql/upgrade_schema_v.2.1.sql"
Though it would also be better not to use upper case for your variables. Doing so makes it so you could collide with environment variables and accidentally change things you don't intend to change (as the number of people who accidentally reset PATH can attest).
I am trying to execute an insert statement from linux shell where one of the columns has '$2a$10$zKjqmgld1gDYB/qkDuAS' in the value. When I see the inserted data the value is truncated and I get only 'aKjqmgld1gDYB/qkDuAS' as any digit followed by dollar is treated by linux as a parameter passed to the script.
This is how I am executing the script
mysql -u user --password=password -e "insert into users(id,name,password) values(1,'Some Name','\$2a\$10\$zKjqmgld1gDYB/qkDuAS')"
I have even tried escaping the $ like \$2a\$10\$zKjqmgld1gDYB/qkDuAS, but still it yields the same truncated data , however when I do echo '\$2a\$10\$zKjqmgld1gDYB/qkDuAS', I get the entire thing back.
Sameer
Single quotes don't nest in shell (do they anywhere else, anyway?). So, your string is effectively outside the quotes. Combine with double quotes and backslashes:
mysql -u user --password=password -e \
"insert into users(id,name,password) values(1,'Some Name','\$2a$10\$zKjqmgld1gDYB/qkDuAS')"
it was already in double quotes (sorry, for writing the wrong query). That didn't work either, however I found a workaround
echo "insert into users(id,name,password) values(1,'Some Name','\$2a\$10\$zKjqmgld1gDYB/qkDuAS')" >> temp.sql
mysql -u user --password=password < temp.sql
This finally worked.
I have a sql file which contains over a million insert statements. The official tool for MySQL administration is not able to open it because of its size. Is it possible to insert records using a BASH script?
I tried this so far but it doesn't work.
while read line
do
mysql -u root --password=root << eof
use dip;
$line;
eof
done < $1
mysql -u root --password=root <mysqlfile.sql
Try this:
while read line
do
mysql -u root --password=root -c "$line"
done < $1
Notes:
If the sql contains double quotes ("), yo'll have to escape them
If the SQL statements go over multiple lines, you'll have to figure that out
The advantage of this method is each line gets its own transaction, whereas if you fire the whole file in, it could blow the logs being such a large change set
I would like to import a 350MB MySQL .sql file on a Windows 7 machine. I usually do this by using
mysql -uuser -p -e "source c:/path/to/file.sql" database
since < doesn't work in Powershell.
My .sql file has an error in it this time. I'd prefer to just skip the bad row and continue the import. How can I force the import to continue on Windows?
On a unix/linux based system, I could use
mysql --force ... < file.sql
but --force doesn't seem to work with the -e "source ..." command (understandably so).
Thanks,
Mike
You're probably going to have to have Powershell execute this in the standard console in order to use < properly. Technically you could use get-content and pipe the output to mysql, but I've always found that to be slow, and it somehow still keeps the file contents in memory of the Powershell session.
This is how I would execute it from the Powershell prompt (changed file path to include spaces to demonstrate inner quotes, just in case):
cmd /C 'mysql -uuser -p --force < "C:\path\with spaces\to\file.sql"'
[GC]::collect() would apparently clear it up the memory, but you can't do that until after it's done anyway. When it comes to mysql and mysqldump, I don't bother with Powershell. The default encoding used in > is Unicode, making dump files twice as big out of Powershell as out of cmd unless you remember to write | out-file dump.sql -enc ascii instead of > dump.sql.
I'd suggest to also have a look at this SO answer, that takes advantage of source SQL command:
I am trying to import a mysqldump file via the command line, but continue to get an error. I dumped the file from my other server using:
mysqldump -u XXX -p database_name > database.sql
Then I try to import the file with:
mysql -u XXX -p database_name < database.sql
It loads a small portion and then gets stuck. The error I receive is:
ERROR at line 1153: Unknown command '\''.
I checked that line in the file with:
awk '{ if (NR==1153) print $0 }' database.sql >> line1153.sql
and it happens to be over 1MB in size, just for that line.
Any ideas what might be going on here?
You have binary blobs in your DB, try adding --hex-blob to your mysqldump statement.
You know what's going on - you have an extra single quote in your SQL!O
If you have 'awk', you probably have 'vi', which will open your line1153.sql file with ease and allow you to find the value in your database that is causing the problem.
Or... The line is probably large because it contains multiple rows. You could also use the --skip-extended-insert option to mysqldump so that each row got a separate insert statement.
Good luck.
I had the same problem because I had Chinese characters in my datasbase. Below is what I found from some Chinese forum and it worked for me.
mysql -u[USERNAME] -p[PASSWORD] --default-character-set=latin1
[DATABASE_NAME] < [BACKUP_SQL_FILE.sql]
I think you need to use path/to/file.sql instead of path\to\file.sql
Also, database < path/to/file.sql didn't work for me for some reason - I had to use use database; and source path/to/file.sql;.
If all else fails, use MySQLWorkbench to do the import. This solved the same problem for me.
I recently had a similar problem where I had done an sql dump on a Windows machine and tried to install it on a Linux machine. I had a fairly large SQL file and my error was happening at line 3455360. I used the following command to copy all text up to the point where I was getting an error:
sed -n '1, 3455359p' < sourcefile.sql > destinationfile.sql
This copied all the good code into a destination file. I looked at the last few lines of the destination file and saw that it was a complete SQL command (The last line ended with a ';') so I imported the good code and didn't get any errors.
I then looked at the rest of the file which was about 20 lines. It turns out that the export might not have completed b/c I saw the following php code at the end of the code:
Array
(
[type] => 1
[message] => Maximum execution time of 300 seconds exceeded
[file] => C:\xampp\htdocs\openemr\phpmyadmin\libraries\Util.class.php
[line] => 296
)
I removed the offending php code and imported the rest of the database.
I had special character in table names , like _\ and it give error when try to import that tables.
i fixed it by changing \ to \\ in dumped sql.
my table names where like rate_\ and i used this command to repair dump :
sed 's._\\._\\\\.g' dump.sql > dump2.sql
i didn't replace all backslashes , because i was not sure if there is some backslash somewhere in database that should not be replaces.
special characters in table name will be converted to # at sign in file name.
read http://dev.mysql.com/doc/refman/5.5/en/identifier-mapping.html
I have same error as,
Unknown command '\▒'.
when I ran this
mysql -u root -p trainee < /xx/yy.gz
So I'd followed these answers. But I did not got the restored db trainee. Then found that
yy.gz is zip file. So I restoring after unzip the file as:
mysql -u root -p trainee < /xx/yy.sql