Read user input and send it to MySQL [duplicate] - mysql

This question already has answers here:
How to feed mysql queries from bash
(7 answers)
Closed 5 years ago.
So I am attempting to make my first Linux script and its just to setup a MySQL user and I'm trying to do that all with a script, but I can not seem to find how I can send the MySQL command from the script. This is what I have right now.
#!/bin/bash
read -p "Please enter your desired MySQL username: " USER
echo $USER
read -p "Please enter your desired MySQL password: " PASS
echo $PASS
mysql CREATE USER $USER#'localhost' IDENTIFIED BY $PASS;
any help would be appreciated
Thanks
-Jamie

You cannot mixup mysql queries in bash script.So, you have to save the mysql queries to a mysql file i.e .sql file and then run the mysql command
To append the create user line to a .sql file use
echo 'mysql CREATE USER $USER#'localhost' IDENTIFIED BY $PASS' >> filename.sql
Now the .sql file will contain the sql queries and you can run
$ mysql -h "server-name" -u "root" "-pXXXXXXXX" "database-name" < "filename.sql"

Related

Create a batch-file which executes a local saved (MY)SQL Text file [duplicate]

This question already has answers here:
Batch file to connect mysql and run commands
(3 answers)
Closed 6 years ago.
I want to create a batch-file which executes a local saved (MY)SQL Text file (sql procedure) The tables created by the execution of the sql-file will use data from a server I have access to (hostname: server1; Port: 3306, (my) username: User2 Password:I´m using no password so the password is empty, Connection Name: ConnectiontoSQLServer, Name of Schema: ABC )
->How will the command in the batch-file look like?
Thanks for your support!
I tried this now, but it´s not working:
mysql --host=server1 --port=3306 --user=User2 --password= --database=ABC < C:\Users\krohn\Documents\Arbeit\SQL Queries\Tabellen CRM-Work\Tabellenaktualisierung.sql
-> What´s wrong?
You can "execute" sql files on command line:
mysql -h server1 -u User2 < C:\Users\X\...

Run mysql commands in bash script without logging in or adding -u root to every command

I'm writing a bash script to do some db stuff. New to MySQL. I'm on Mac and have MySQL installed via homebrew.
Am using username "root" right now and there isn't a pw set. I included the pw syntax below just to help others out that may have a pw.
My goal is to have mysql commands be as "clean" as possible in my bash script
Not a hige deal, but would like to do this if possible.
Example
# If I can do it without logging in (*ideal)
mysql CREATE DATABASE dbname;
# Or by logging in with - mysql -u root -pPassword
CREATE DATABASE dbname;
# Instead of
mysql -u root -pPassword -e"CREATE DATABASE dbname";
Tried to simplify it. I have a handful of things I gotta do, so would rather keep my code cleaner if possible. I tried logging in with the bash script, but the script stopped once logged into MySQL and didn't run any commands.
Another option I was considering (but don't really like) would be just to keep username and pw string in a var and call it for every commmand like so
# Set the login string variable
login_details="-u root -p password -e"
# example command
mysql $login_details"CREATE DATABASE dbname";
So any ideas?
Write a new bash script file and run this file after putting all your commands into it. Don't forget to give right username and password in your bash script.
For bash script:
#!/bin/bash
mysql -u root -pSeCrEt << EOF
use mysql;
show tables;
EOF
If you want to run single mysql command.
mysql -u [user] -p[pass] -e "[mysql commands]"
Example:
mysql -h 192.168.1.10 -u root -pSeCrEt -e "show databases"
To execute multiple mysql commands:
mysql -u $user -p$passsword -Bse "command1;command2;....;commandn"
Note: -B is for batch, print results using tab as the column separator, with each row on a new line. With this option, mysql does not use the history file. Batch mode results in nontabular output format and escaping of special characters. -s is silent mode. Produce less output. -e is to execute the statement and quit

MySQL prompts for the password when using heredoc [duplicate]

This question already has answers here:
How to provide password to a command that prompts for one in bash?
(8 answers)
Closed 8 years ago.
I am trying to execute a shell script to login to mysql as root and execute some commands, and in order to avoid putting the root's password in the command line, I am using heredoc format as shown below.
However, mysql prompts me for the password despite the fact that I am giving it the right password.
Exactly the same syntax works perfect on some other hosts but not here.
Why?
mysql#myhost:MySQL> mysql -uroot -p -s -P3306 -e 'SELECT NOW();' <<EOF
> MyPassword
> EOF
Enter password: <---- why does it require manual password entry in here?
2014-12-23 14:57:25
Assuming you use a BASH or KSH command line interface:
read -s pwd;mysql -uroot --password=${pwd} -s -P3306 -e 'SELECT NOW();'
For the read command, you should use the -s option because, as it happens with using heredoc, this solution would also display the password as it is being typed, if you wouldn't use said option.
This solution works because the mysql client obfuscates the password so it does not show in the output from process-listing commands, like ps. Run this:
read -s pwd;mysql -uroot --password=${pwd} -s -P3306
Then, if you open an additional command line interface and execute ps, you will see that it does not show the password, but and obfuscated version of it, in the process list.
This was a famous bug of the mysql client, back in the day, but was fixed in 2002.
Using read -s to put the password in a variable has the added benefit of not showing the password in the command-line interface's history.

How to download mysql database from server using command prompt in windows [duplicate]

This question already has answers here:
how can I export mysql database using ssh?
(3 answers)
Closed 9 years ago.
I want to download mysql database from server using command prompt in windows.
I tried to use ssh to connect to the server.
But, it is not working. Is there any command to this?
Exporting MySQL Data
This example shows you how to export a database. It is a good idea to export your data often as a backup.
Using SSH, execute the following command:
mysqldump -p -u username database_name > dbname.sql
You will be prompted for a password, type in the password for the username and press Enter. Replace username, password and database_name with your MySQL username, password and database name.
The file dbname.sql now holds a backup of your database and is ready for download to your computer.
To export a single table from your database you would use the following command:
mysqldump -p --user=username database_name tableName > tableName.sql
Again you would need to replace the username, database and tableName with the correct information.
Once done the table specified would then be saved to your account as tableName.sql
Have a look Backup or Schema/Data Comparison tools in dbForge Studio for MySQL. Command line and conencting through secure SSH connections are supported.

BASH script not reading input mysql password [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to execute Mysql command from a shell script?
I'm trying to execute a .sql file using a bash script. But I am having a problem connecting to MySQL. Here's what I have so far:
#! /bin/sh
PWD="thepassword"
mysql -p -u theuser < Randomsqlfile.sql
echo $PWD
When the password is prompted, nothing fills out.
Make this:
mysql -utheuser -pthepassword <Randomsqlfile.sql