Terminal mysql queries - mysql

I'm looking to use terminal to execute mysql queries. I currently connect to a sql db via the sql workbench but would like to do this via terminal. Is this possible?
I've installed mysql via homebrew
when I hit mysql in terminal it says command not found, maybe I need to do something else besides the homebrew setup?

Go to the directory:
mysql/bin
To execute the query from command line:
mysql -u [username] -p [dbname] -e [query]
example:
mysql -u root -p database -e "select * from user"

mysql --help
You can do also:
cat dump.sql | mysql -u user -p password database
Or:
echo "select * from abc;" | mysql -u user -p password database

Related

Typing in MySQL Password Through Shell Script

So I'm trying to run the following lines as a shell script
mysql -u root -p -h myserver
connect testdb
SELECT * FROM testtable
However, after I type the initial "mysql -u root -p" it asks for a password rather than running the entire thing automatically. My question is, is there some line I can add to my shell script to automatically type the password in and press "Enter".
Thanks.
Try below line
echo "SELECT * FROM testtable" | mysql -h myserver -u root -pPASSWORD testdb
replace PASSWORD with your mysql password.

MySQL treats password in script as database

I am new to Bash scripting. I want to create a script that logs in to MySQL for me:
PASSWORD="MyPassword"
sudo service mysql start
mysql -u root -p $PASSWORD
However, it throws an error telling me that my password ($PASSWORD) is not a database.
Is there any way to do it?
Thanks and sorry if I am asking something RTFM or UTFM.
From man mysql:
If you use the short option form (-p), you cannot have a space between the option and the password.
So either
mysql -u root -p"$PASSWORD"
or
mysql -u root --password "$PASSWORD"
It is insecure to use password on the command line.
From the mysql user guide:
This is convenient but insecure. On some systems, your password becomes visible to system status programs such as ps that may be invoked by other users to display command lines.
I would suggest You to use the mysql_config_editor utility, to store your db credentials.
mysql_config_editor set --login-path=YOUR_LOGIN_PATH --host=YOUR_HOST --user=YOUR_DB_USER --password
Then it will ask for password interactively
After that you can connect to your db:
mysql --login-path=YOUR_LOGIN_PATH YOUR_DB
Example (set credentials):
mysql_config_editor set --login-path=root --host=localhost --user=root --password
Example (connect to db in your script):
mysql --login-path=root YOUR_DB

\! something command not works expected in remote mysql conecction

I wanna run some commands from mysql shell remote, but when I run the command for example
mysql -u root -h remoteip -p
mysql> \! nano somefile.txt
mysql> \! python somescript.py
This command try to access to my michine, no the remote machine,
is there some way to run a native commands from mysql shell?
some like
mysql> select execute('python /tmp/script.py');
Note:
please dont tell me that use ssh because is special case when I just have access to mysql remote shell no more.
try this
mysqldump -u username -p -h remote.site.com DBNAME > backup.sql
to acheive the same you must have to create mysql use in remote server and should provide your machine ipadress.
As well make sure you can access mysql from your local machine.
GRANT SELECT,INSERT,UPDATE ON <dbname>.* TO
'<user>'#'ipaddressofm/cfromwhich you want to acess' IDENTIFIED BY '<password>';

mysql syntax error near expected new line in terminal mac

I am running the following command as given on web to connect the mysql data base but it gives syntax error new line expected here is the command i am entering.
mysql -<hivelettest.c0e9graawyhr.us-west-2.rds.amazonaws.com -p 3306 -u <user> -p <pass>
The <> parts of the command were given to show where the username and password should go. They shouldn't be in the command:
mysql -hmydbserver.co.uk -P3306 -u username -pmypassword
Try this, and make sure that there is no space between -u or -p. And if you are using PORT you need to write it differently. The password parameter must then be --password=
mysql -hmydbserver.co.uk -P 3306 -uUsername --password=yourpassword
Since 3306 is the standard one you could leave it out, then you can write it like this.
mysql -hmydbserver.co.uk -uUsername -pYourpassword
If you want to pass with a command, do it like this.
mysql -hmydbserver.co.uk -P 3306 -uUsername --password=yourpassword nameofdatabase
-e "SELECT * FROM tablename"

Problems with Mysqldump command

I have a Debian VPS with mysql installed and i want to export a database.
After have successful login in to mysql.
I run the follow command:
mysqldump -u user -p mydatabase > db.sql
but I got the following error:
->
It doesn't export the database and I canĀ“t type anything.
you don't need to login to mysql. Just type:
mysqldump -u user -p mydatabase > db.sql
on the command line, meaning the shell. It will ask you for a password and then writes the dump to db.sql