mysql (5.1) command line interface and my madness - mysql

Got two little problems with this interface. The worse one is that I have a file with a blank line (i have removed the SQL to demonstrate!). Tried to use mysql but get this
C:\Users\edheal\Desktop>mysql -e "blank.sql" -h localhost -u test -p
Enter password: *
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the ma
nual that corresponds to your MySQL server version for the right syntax to use n
ear 'blank.sql' at line 1
C:\Users\edheal\Desktop>cat -v "blank.sql"
^M
C:\Users\edheal\Desktop>
What could possibly be the problem? It is just a blank line!
The second - just me being lazy during development. Is it possible to not have a password on the account? Seems that the minimum is one character.

A blank line is not a valid SQL statement therefore it's a syntax error.

Try to use
mysql -h localhost -u test -pYourPasword < "blank.sql"
One some standard installations, the root user does not have any password. But it is not possible to set an existing password to "empty".

The -e or the --execute option expects you to have the SQL command following it:
mysql -e "select ....." -h localhost -u test -p
If you want to execute the commands listed in a file say test.sql you can do:
mysql -h localhost -u test -p < test.sql

Related

try to connect to mysql through command line , leaves blank space after password

Trying to connect to an rds mysql server from an ec2 ubuntu server.
I use
mysql -h my_host_name -u admin_name -p database < data.sql
When the password prompts, I enter my password. However all this does it create a new blank line and does nothing else.
Any ideas?
When mysql is processing file input, it doesn't normally print informative messages, it only displays the results of SELECT queries. If you want to see messages from queries that modify the database, add the -v option to make it verbose.
mysql -v -h my_host_name -u admin_name -p database < data.sql
If you use -v -v it will produce even more details, and -v -v -v will be most informative.
This blank line probably means that your mysql is processing what is inside your "data.sql".
If you need to see what is been processed, you can first connect to mysql server with:
mysql -h my_host_name -u admin_name -p
Change to your database ( if you have one defined and your sql is not creating one... ):
mysql> change my_database;
Than you call your script execution with:
mysql> source data.sql;
{}'s

How i can import my database dump file in mysql in ubuntu server

i am executing query for importing sql dump in ubuntu
mysql -u username - p passsword databasenme < var/www/sql.sql;
error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql -u
There should not be any blank space between -p and the password.
mysql -u username -p<PASSWORD HERE> databasenme < var/www/sql.sql;
UPDATE:
I assumed you weren't logged in to mysql already. If you are, you should exit from mysql console and then retry.
You can import .sql file using :
mysql -u <user> -p<password> <dbname> < mysqlfile.sql
Note: There shouldn't space between -p and <password>
Reference: http://dev.mysql.com/doc/refman/5.0/en/mysql-batch-commands.html
Time taken for importing huge files most importantly it takes more time is because default setting of mysql is "autocommit = true", you must set that off before importing your file.
First open MySQL:
mysql -u root -p
Then, You just need to do following :
mysql>use your_db
mysql>SET autocommit=0 ; source the_sql_file.sql ; COMMIT ;
You are logged in to mysql. press ctl+z to exit and try again.
don't write user's password on this line, type only flag -p , and in new line enter password:
$ mysql -u user -p database_name < your_file.sql
Enter password:
EDIT:
Check .sql file for syntax, maybe have a syntax error in the file.

How to backup and restore in MySQL 5.2?

This is the command I used:
mysql> mysql -u root -p -h HOST sample < mysqldump.sql;
But I encountered an error as follows:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql -u root -p -h HOST sample < mysqldump.sql' at line 1
Hope you can help me out?
That's the command you should give in the command line, not in MySQL itself.
Open mysql terminal or konsole and then go to the directory where mysqldump.sql file is present. try:
shell> cd /file_path/;
shell> ls -al mysqldump.sql;
It should display your file. Also you dont need to specify HOST if you are logged on to the same server.
shell> mysql -u root -p sample < mysqldump.sql;
from client you can run \. mysqldump.sql but i think first variant is preferable.

How to run SQL script in MySQL?

I want to execute a text file containing SQL queries, in MySQL.
I tried to run source /Desktop/test.sql and received the error:
mysql> . \home\sivakumar\Desktop\test.sql ERROR: Failed to open file
'\home\sivakumar\Desktop\test.sql', error: 2
Any idea on what I am doing wrong?
If you’re at the MySQL command line mysql> you have to declare the SQL file as source.
mysql> source \home\user\Desktop\test.sql;
You have quite a lot of options:
use the MySQL command line client: mysql -h hostname -u user database < path/to/test.sql
Install the MySQL GUI tools and open your SQL file, then execute it
Use phpmysql if the database is available via your webserver
you can execute mysql statements that have been written in a text file using the following command:
mysql -u yourusername -p yourpassword yourdatabase < text_file
if your database has not been created yet, log into your mysql first using:
mysql -u yourusername -p yourpassword
then:
mysql>CREATE DATABASE a_new_database_name
then:
mysql -u yourusername -p yourpassword a_new_database_name < text_file
that should do it!
More info here: http://dev.mysql.com/doc/refman/5.0/en/mysql-batch-commands.html
My favorite option to do that will be:
mysql --user="username" --database="databasename" --password="yourpassword" < "filepath"
I use it this way because when you string it with "" you avoiding wrong path and mistakes with spaces and - and probably more problems with chars that I did not encounter with.
With #elcuco comment I suggest using this command with [space] before so it tell bash to ignore saving it in history, this will work out of the box in most bash.
in case it still saving your command in history please view the following solutions:
Execute command without keeping it in history
extra security edit
Just in case you want to be extra safe you can use the following command and enter the password in the command line input:
mysql --user="username" --database="databasename" -p < "filepath"
All the top answers are good. But just in case someone wants to run the query from a text file on a remote server AND save results to a file (instead of showing on console), you can do this:
mysql -u yourusername -p yourpassword yourdatabase < query_file > results_file
Hope this helps someone.
I came here searching for this answer as well, and here is what I found works the best for me: Note I am using Ubuntu 16.x.x
Access mysql using:
mysql -u <your_user> - p
At the mysql prompt, enter:
source file_name.sql
Hope this helps.
Give the path of .sql file as:
source c:/dump/SQL/file_name.sql;
mysql> source C:\Users\admin\Desktop\fn_Split.sql
Do not specify single quotes.
If the above command is not working, copy the file to c: drive and try again.
as shown below,
mysql> source C:\fn_Split.sql
instead of redirection I would do the following
mysql -h <hostname> -u <username> --password=<password> -D <database> -e 'source <path-to-sql-file>'
This will execute the file path-to-sql-file
Never is a good practice to pass the password argument directly from the command line, it is saved in the ~/.bash_history file and can be accessible from other applications.
Use this instead:
mysql -u user --host host --port 9999 database_name < /scripts/script.sql -p
Enter password:
mysql -uusername -ppassword database-name < file.sql
So many ways to do it.
From Workbench: File > Run SQL Script -- then follow prompts
From Windows Command Line:
Option 1: mysql -u usr -p
mysql> source file_path.sql
Option 2: mysql -u usr -p '-e source file_path.sql'
Option 3: mysql -u usr -p < file_path.sql
Option 4: put multiple 'source' statements inside of file_path.sql (I do this to drop and recreate schemas/databases which requires multiple files to be run)
mysql -u usr -p < file_path.sql
If you get errors from the command line, make sure you have previously run
cd {!!>>mysqld.exe home directory here<<!!}
mysqld.exe --initialize
This must be run from within the mysqld.exe directory, hence the CD.
Hope this is helpful and not just redundant.
From linux 14.04 to MySql 5.7, using cat command piped with mysql login:
cat /Desktop/test.sql | sudo mysql -uroot -p
You can use this method for many MySQL commands to execute directly from Shell. Eg:
echo "USE my_db; SHOW tables;" | sudo mysql -uroot -p
Make sure you separate your commands with semicolon (';').
I didn't see this approach in the answers above and thought it is a good contribution.
Very likely, you just need to change the slash/blackslash:
from
\home\sivakumar\Desktop\test.sql
to
/home/sivakumar/Desktop/test.sql
So the command would be:
source /home/sivakumar/Desktop/test.sql
use the following from mysql command prompt-
source \\home\\user\\Desktop\\test.sql;
Use no quotation. Even if the path contains space(' ') use no quotation at all.
Since mysql -u yourusername -p yourpassword yourdatabase < text_file did not work on a remote server (Amazon's EC2)...
Make sure that the Database is created first.
Then:
mysql --host=localhost --user=your_username --password=your_password your_database_name < pathTofilename.sql
For future reference, I've found this to work vs the aforementioned methods, under Windows in your msql console:
mysql>>source c://path_to_file//path_to_file//file_name.sql;
If your root drive isn't called "c" then just interchange with what your drive is called. First try backslashes, if they dont work, try the forward slash. If they also don't work, ensure you have your full file path, the .sql extension on the file name, and if your version insists on semi-colons, ensure it's there and try again.
If you are here LOOKING FOR A DRUPAL ENVIRONMENT
You can run with drush command on your project directory
drush sqlc
If you are trying this command :
mysql -u root -proot -D database < /path/to/script.sql
You may get an error like this : if you have special characters, mainly '`'
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/path/to/script.sql' at line 1
So I would suggest to use a command like this :
echo "source /path/to/script.sql" | mysql -u root -proot -D database
This command will execute source /path/to/script.sql once connected to the server, which execute your script.
I had this error, and tried all the advice i could get to no avail.
Finally, the problem was that my folder had a space in the folder name which appearing as a forward-slash in the folder path, once i found and removed it, it worked fine.
I use Bash's Here Strings for an instant SQL execution:
mysql -uroot -p <<<"select date(now())"
https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Here-Strings

Import mysql DB with XAMPP in command LINE

I have this command:
mysql -u #myusername -p #mypasswd MYBASE < c:\user\folderss\myscript.sql
But I get the following error:
Error
unknow command '\U'
unknow command '\m'
unknow command '\D'
unknow command '\L'
For those using a Windows OS, I was able to import a large mysqldump file into my local XAMPP installation using this command in cmd.exe:
C:\xampp\mysql\bin>mysql -u {DB_USER} -p {DB_NAME} < path/to/file/ab.sql
Also, I just wrote a more detailed answer to another question on MySQL imports, if this is what you're after.
Go to the xampp shell command line
and run
mysql -h localhost -u username databasename < dump.sql
You may also need to specify the host. From your statement, it looks like you run on Windows. Try this:
mysql -h localhost -u #myusername -p #mypasswd MYBASE
< c:/user/folderss/myscript.sql
Or
mysql -h localhost -u #myusername -p #mypasswd MYBASE
< "c:\user\folderss\myscript.sql"
mysql -h localhost -u username databasename < dump.sql
It works, try it.
If password is blank no need to add the -p args.
/opt/lampp/bin/./mysql -u dbusername -p dbname < sqlfilepath.sql
It works:
mysql -u root -p db_name < "C:\folder_name\db_name.sql"
C:\ is for example
Copy Database Dump File at (Windows PC)
D:\xampp\mysql\bin
mysql -h localhost -u root Databasename <#data11.sql
this command posted by Daniel works like charm
C:\xampp\mysql\bin>mysql -u {DB_USER} -p {DB_NAME} < path/to/file/ab.sql
just put the db username and db name without those backets
**NOTE: Make sure your database file is reside inside the htdocs folder, else u'll get an Access denied error
No Doubt It does the trick:
C:\xampp\mysql\bin>mysql -u {DB_USER} -p {DB_NAME} < path/to/file/ab.sql
But some time you many find "MySql Server has Gone a way"
For that you need to change mysql/bin/my.ini max_allowed_packet = 64M to my.ini
I think the commands are OK, issue is with the spaces.
Please observe the spaces between -u and #myusername also between -p and #mypasswd.
mysql -u #myusername -p #mypasswd MYBASE < c:\user\folderss\myscript.sql
Can also be written as:
mysql -u#myusername -p#mypasswd MYBASE < c:\user\folderss\myscript.sql
Also, you need not add -p and the time of command, if your password is blank, it will not ask you for the password.
If you have password to your database, it will ask for the password.
Thanks
The process is simple-
Move the database to the desired folder e.g. /xampp/mysql folder.
Open cmd and navigate to the above location
Use command to import the db - "mysql -u root -p dbpassword newDbName < dbtoimport.sql"
Check for the tables for verification.
In Windows, place the MySQL file (i.e. example.sql) in C:\xampp\mysql\bin.
Open the XAMPP Control Panel, and launch Shell:
cd c:\xampp\mysql\bin
mysql --default-character-set=utf8 -h localhost -u username databasename < example.sql
Note: Using --default-character-set=utf8 can help to prevent encoding issues with special characters if you intend on working with UTF-8.
Afterwards, remove the MySQL file from C:\xampp\mysql\bin.
Do your trouble shooting in controlled steps:
(1) Does the script looks ok?
DOS E:\trials\SoTrials\SoDbTrials\MySQLScripts
type ansi.sql
show databases
(2) Can you connect to your database (even without specified the host)?
DOS E:\trials\SoTrials\SoDbTrials\MySQLScripts
mysql -u root -p mysql
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.0.51b-community-nt MySQL Community Edition (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
(3) Can you source the script? (Hoping for more/better error info)
mysql> source ansi.sql
+--------------------+
| Database |
+--------------------+
| information_schema |
| ... |
| test |
+--------------------+
7 rows in set (0.01 sec)
mysql> quit
Bye
(4) Why does it (still not) work?
DOS E:\trials\SoTrials\SoDbTrials\MySQLScripts
mysql -u root -p mysql < ansi.sql
Enter password: ********
Database
information_schema
...
test
I suspected that the encoding of the script could be the culprit, but I got syntax errors for UTF8 or UTF16 encoded files:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near '´╗┐
show databases' at line 1
This could be a version thing; so I think you should make sure of the encoding of your script.
I think the OP's problem was not escaping the slashes.
on windows a path like: "c:\user\folderss\myscript.sql" should in the command line be written like either:
c:\\\user\\\folders\\\myscript.sql
or:
c:/user/folders/myscript.sql
therefore "\u" from "c:\user" was interpreted as a command.
see: http://dev.mysql.com/doc/refman/5.5/en/mysql-commands.html for more info
here is my try to import database from cmd in xampp server
the correct command for me is this you can change it with your requirements.and i also attached Errors that i face first time importing db from command line.you can see last command that i use and i paste it here also its working fro me.
mysql -h localhost -u root ovxsolut_windhs < C:\Users\eee\Downloads\ovxsolut_windhs.sql
C:\xampp\mysql\bin>mysql -u {DB_USER} -p {DB_NAME} < path/to/file/filename.sql
mysql -u root -p dbname< "C:\Users\sdkca\Desktop\mydb.sql"
mysql -h localhost -u username -p databasename < dump.sql