Basic MySQL syntax error when trying to connect to server [duplicate] - mysql

I am connecting to database mysql using command prompt using below command
mysql -h localhost -u test -ptest test;
But i am getting following error:-
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
-h localhost -u test -ptest test' at line 1
I have done a lot of googling but could not find the reason behind this problem. Any help?

It seems you are executing the statement at mysql command line console but not at system shell prompt.
The statement
mysql -h localhost -u test -ptest test;
is for system shell prompt.
And while running at command prompt, you don't need to terminate the command by a semi colon.
And for suppressing use of password at command line,
change
-ptest
to
-p
It then prompts you to input your password.

You need to run that command (which starts the mysql shell) from the command prompt.
You are trying to run it inside the mysql shell.

First make sure your command line doesnt show
mysql>
prefix. if it shows you are already in the mysql console.
you have to type the command outside the mysql console.
mysql -h localhost -u test -ptest
That will fix you issue.
Refer Mysql documentation for more information
http://dev.mysql.com/doc/refman/5.0/en/connecting.html
Thanks.

mysql -u root -p -h localhost
then command would ask for password, input it and you can logged into mysql server

Related

Mysql ERROR : not connected

I am trying to connect to MySQL database from MySQL shell on windows.
No matter what I type in MySQL shell, it keeps giving me error : 'Not connected'.
Query eg 1 : mysql --host=localhost --port=3306 --user=root -p;
Query eg 2 : mysql -u root -p
O/P : ERROR: Not connected
I have MySQL server installed on my machine. Also MySQL service is running in the background.
Also, I was able to connect from MySQL workbench.
ERROR MESSAGE
MySQL Workbench Connection
My temporary workaround is that I make use of ssl protocol to connect to MySQL server :
MySQL> \connect root#localhost
MySQL localhost:33060+ ssl SQL > show databases;
The first step is that you need to check if you are in the MYSQL Shell SQL mode or JS mode.
Then if you are in SQL mode then you are good to go else you need to switch to SQL mode by this command
\sql
The next step is to connect using this command
\connect root#localhost
In your case, you might have given the privilege as the IP address so you need to check your localhost IP which can be done by this command in your command prompt.
ipconfig and then just check the IP address and put it in place of localhost in the previous command. If this still doesn't works then put 127.0.0.1:3306.
After this, it will prompt to add or save the password , enter a unique password there.
After this you are good to go and check the user and localhost after this by this command
SELECT user, host FROM mysql.user;
Try mysql -u root -p
I haven't used MySQL shell, I typically use gitbash and it works just fine
I had faced the same issue on my Windows 10 machine with MySQL 5.7 and the following commands helped me:
mysqlsh.exe - to open mysql shell; then
\sql - to start working with SQL;
finally:
\connect root#127.0.0.1:3306
You can use:
mysql -uroot -hlocalhost -P3306 -p
or
mysql -uroot -h127.0.0.1 -P3306 -p
or
mysql -uroot -p

Querying remote MySql database with bash

I'm trying to query remote database with the following script.
some_db="somedb"
isAnythingToProcess=$(mysql -uroot -proot -D$some_db -e "$checkSearch");
This works for me locally however whenever i try to run bash script to remote AWS server I get error
ERROR 1049 (42000): Unknown database 'somedb'
Any hints?
P.S. The database exists for sure. I can connect to it via MySQL client.
You need to add the parameter for remote host -h
some_db="somedb"
isAnythingToProcess=$(mysql -uroot -proot -h REMOTE_IP -D$some_db -e "$checkSearch");

Can't backup with MariaDB Console

I am trying to take a backup of a database. I open up the MariaDB client console on windows 10, enter root password and then enter this:
MariaDB [(none)]> mysqldump database_name > backup.sql;
It returns this error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'mysqldump database_name > backup.sql' at line 1
What did I do wrong?
Try using
mysqldump -u root -p database_name > file.sql
before logging to database (new CLI window). You will have to enter root password after this command
You should be able to run this command in any CLI, like CMD.exe or Git Bash - depending on the way you installed things on your machine. I hope this helps

Trying to set MySQL root password results in error

I'm still very new to MySQL and Debian, but have been reading a lot during the last days.
I have a root account to a virtual machine that I want to use for Wordpress. One thing I read was that I should password protect MySQL because by default root user doesn't need a password for it.
I read someone doing it like this:
mysql> mysqladmin -u root password mypassword;
But that gives me the following error:
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 'mysqladmin -u root password mypassword' at line 1
You are running the command from within mysql, but the command is actually meant to be run directly from the terminal.
mysqladmin -u root password mypassword
If you are already in mysql within terminal, type exit then enter your mysqladmin command.

How do i connect to the Live database(Mysql) using Mysql command prompt?

I have tried the following command syntax to connect with live
database...
-u Username -pPassword -h HostName Database Name
But there is an error
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 ' -u Username -pPassword -h HostName Database Name'
The command you're describing is for connecting to the database in command line. If you need to connect to a database inside a PHP script, you must use:
mysqli_connect aka mysqli::__construct
PDO::__construct
I'm not recommending it but if you're stuck with legacy code, mysql_connect also does the same for the deprecated mysql_* extension.
Try this... This might help you
connect to mysql database using command line and php
You must be using this command already on mysql shell prompt or on phpmyadmin like sql frontend. You must not use this command on mysql-shell as you are already loggin into mysql. Try this command on bash shell on linux or dos prompt on windows.
mysql -u Username -pPassword -h HostName DatabaseName
One more thing, If you are using windows (like wamp/xampp). You may need to pass full mysql path like
c:\wamp\bin\mysql\mysql -u Username -pPassword -h HostName DatabaseName