Will start with what I am trying to accomplish. I wrote up a menu script to add a new database and echo back to screen the results. But can't seem to get it to login with a variable.
Heres the part I am having problems with:
#!/bin/bash
while [[ "$yn" != "Yes" && "$yn" != "Y" && "$yn" != "y" && "$yn" != "yes" ]]; do
echo " Please, type password for root user. #"
read -r mysqlrp
echo " You have entered $mysqlrp as your MySQL password #"
echo " Is this correct? (Yes or No) #"
read yn
done
mysql -u root -p$mysqlrp
have also tried:
mysql -u root -p${mysqlrp}
as well as mysql -u root -p'${mysqlrp}'
I get the following:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
even though when I try without the script works fine.
Please help,
Thanks in advance,
Joe
To supply a password directly in the command line string, you should use mysql --password=[password]. See this article.
And to prompt the user for a password, you should probably use something like this.
#!/bin/bash
read -p "Username: " uname
stty -echo
read -p "Password: " passw; echo
stty echo
You must not ever print the password. And it should not be seen while typing it in either.
#!/bin/bash
read -p "Username: " uname
stty -echo
read -p "Password: " passw; echo
stty echo
mysql --user=$uname --password=$passw
That script works for me. If it doesn't for you, please check that your mysql permissions allow you to login from localhost.
In a shellscript, you can do
set -x
to see what the command being executed looks like.
You should prolly do mysql --password="$mysqlrp". '$mysqlrp' -> '$mysqlrp'.
In your real script, I suppose you probably want to do
read -p "Password, plz:" -s mysqlrp
In order to properly answer the question, the actual error you get would be helpful. [UPDATE: answered in comment.]
Also, note that if your query is longrunning, your password will be visible in "ps -ef"
Related
I'm trying to create a shell script to automatically backup my whole database. My script expects the password in a new line because I don't want to store it anywhere. Here is my code:
#!/bin/bash
echo -n Password:
read -s PASS
echo
docker exec db /usr/bin/mysqldump --all-databases -u admin --password='\''$PASS\'' > backup-$(date "+%Y-%m-%d").sql
The problem is, that I need to put the variable into single quotes because of the $ signs in the password, but I can't read variables in single quotes.
I know there are several similar questions out there, but I could not solve is with them.
So if the password is '$ecretPa$$word', then the actual executed program should be something like this:
docker exec db /usr/bin/mysqldump --all-databases -u admin --password='$ecretPa$$word' > backup-2020-03-03.sql
I managed to make this work. Maybe the problem was not with the escaping but with the way I tried to run this.
My final code:
#!/bin/bash
echo -n Password:
read -s PASS
echo
cmd="docker exec db /usr/bin/mysqldump --all-databases -u admin --password='$PASS' > backup-$(date "+%Y-%m-%d").sql"
eval "$cmd";
I am trying to login to a new mysql server using powershell on my windows workstation. I already have it working in Cygwin but I need to test it in powershell.
mysql -h adams.server.com -u schema_owner_adam --ssl-mode=VERIFY_IDENTITY -p
I pasted my password in, that didn't work, then I started typing it in letter by letter multiple times like a dummy, every time it threw back the error:
ERROR 1045 (28000): Access denied for user 'schema_owner_adam'#'1.2.4.8' (using password: YES)
After double-checking that I was not in fact a dummy, and going back to cygwin and being able to login fine there, I started getting suspicious of powershell and when I tried sending my password through like this:
-u schema_owner_adam:xxxxxx£on12CV
it came back with:
ERROR 1045 (28000): Access denied for user 'schema_owner_adam:xxxxxxoon12CV'#'1.2.4.8' (using password: YES)
Spot the difference!
I assume that powershell is munging the £ character in my password in the same way when I type it into the password prompt.
How can I sort this one?
I am in the UK, maybe I need to set UTF-8 somehow?
PS C:\> echo $OutputEncoding
IsSingleByte : True
BodyName : us-ascii
EncodingName : US-ASCII
HeaderName : us-ascii
WebName : us-ascii
WindowsCodePage : 1252
IsBrowserDisplay : False
IsBrowserSave : False
IsMailNewsDisplay : True
IsMailNewsSave : True
EncoderFallback : System.Text.EncoderReplacementFallback
DecoderFallback : System.Text.DecoderReplacementFallback
IsReadOnly : True
CodePage : 20127
Honestly, I drop to CMD when accessing MySql command line and I get less odd behavior.
ISE will not allow interactive applications to run, so try it in a normal Powershell window first.
If that doesn't work drop from Powershell to CMD by typing:
C:>cmd
I would suggest putting an ampersand & before the command and then put the comand in double quotes "
Also the £ in the password won't be helping so try putting a tilde/back tick (which is an escape character in powershell) before the £ so the password would look like:
xxxxxx`£on12CV
In case you weren't aware, if you are using the -p option then you must put the password immediately afterwards without a space (https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html) whereas if you use the --password= option you can have a space.
Unfortunately I haven't got my laptop in front of me and my tablet doesn't have mysql on it so I can't test the command but try this:
$server = "adams.server.com"
$user = "schema_owner_adam"
$password = "xxxxxx`£on12CV"
&"mysql.exe" -h $server -u $user --ssl-mode=VERIFY_IDENTITY -p$password
Or if the password is still not working try replacing the last line with this:
&"mysql.exe" -h $server -u $user --ssl-mode=VERIFY_IDENTITY --password=$password
Alternativly the code for the other way you wrote is:
$server = "adams.server.com"
$user = "schema_owner_adam"
$password = "xxxxxx`£on12CV"
$userandpassword = $user + ":" + $password
&"mysql.exe" -h $server -u $userandpassword --ssl-mode=VERIFY_IDENTITY
Following your comment below, try this line of code that doesn't use any variables:
&"mysql.exe" --host="adams.server.com" --user="schema_owner_adam" --ssl-mode=VERIFY_IDENTITY --password="xxxxxx`£on12CV"
Let me know if this works and if not let me know what error it gives you. Then when I'm in front of a laptop later I'll have a proper look at it.
As another post (Script for MySQL backup to multiple files), I received help to create a Powershell script that creates backup of MySQL databases and generates multiple files, one for each database. As can be seen, the script makes a pipeline between a command mysql and mysqldump.
My intention now is to eliminate the user information and password directly in the script. As another link (How to perform a mysqldump without a password prompt?), I created the my.cnf configuration file MYSQL_HOME, passing the information on [mysqldump], and used the flag --defaults-extra-file. The problem is that this flag does not work for mysql.exe, so could not use this solution.
To avoid leaving the user and password information directly in the script, I used another post (How to handle command-line arguments in PowerShell), which shows how to configure parameters input into Powershell scripts. With that, my script looked like this:
param (
[string]$username = $(throw "-username is required."),
[string]$password = $(Read-Host "Input password, please" )
)
$BACKUPDATE = Get-Date -UFormat "%Y-%m-%d_%H%M%S"
$BKPFOLDER='E:\bkp'
$MYSQL_HOME="C:\MYSQL"
Set-Location "$MYSQL_HOME\bin"
& .\mysql.exe -N -s -r -u $username -p$password -e 'show databases' | % {
& .\mysqldump.exe -u $username -p$password --single-transaction $_ |
Out-File "$BKPFOLDER\${_}_$BACKUPDATE.sql" -Encoding Ascii
}
When I run the following command:
test.ps1 -username bkpuser -password mypass
I get the following message:
ERROR 1045 (28000): Access denied for user 'bkpuser'#'localhost' (using password: YES)
But there is no access permission problem, because if I replace the values of $usename and $password to call the mysql and mysqldump by correct values (excluding the parameter), the command works.
What should I change?
PowerShell's parser can't determine where commandline argument ends and vairable name starts. You can see this clearly in ISE, because $password should be red, but it's blue:
Just add space between -p and $password or use "=": --user=$username --password=$password
i have my project in jenkins, when you build it, it finds the script/cibuild script and executes whatever you have in there. I run the script from my project directory and all is fine, i run the build from jenkins, i get errors? i dont understand?
my script/cibuild script:
#!/bin/sh
# This script file is the entry point to ci.miranetworks.net build/test process.
# It is executed by jenkins, from the root directory
#
echo "1. cd into script "
cd script
echo "2. run createmysqldb test_traffic test_user password"
./createmysqldb test_traffic test_user password
echo "3. cd back into root dir "
cd ..
echo "4. create table with sql with: "
mysql -u test_user --password=password test_traffic < ./phoenix/data/sql/lib.model.schema.sql
export WORKSPACE=phoenix
export SYMFONY=$WORKSPACE/lib/vendor/symfony/lib
(cd $WORKSPACE
echo "6. Clearing the cache"
./symfony cc
echo "7. Run unit test"
./symfony php test/unit/RbcTest.php
)
echo "8. All done and exiting"
exit 0
so when i log onto mysql command with mysql -u test_user --password=password test_traffic it is succesfull then i do GRANT ALL and i also get:
ERROR 1045 (28000): Access denied for user 'test_user'#'localhost' (using password: YES)
although in createmysqldb i do: Q2="GRANT ALL ON . TO '$2'#'localhost' IDENTIFIED BY '$3';"
can anyone explain why i get an access denied?
I have another issue with the same script but want to sort out this one first :)
thanks
As far as in MySQL i know you need to change
ALL ON . TO ...
into
ALL ON .* TO ...
to grant access to all tables ?
I'm writing a bash script to do some operations against a database on my debian squeeze Server.
I have noticed that if I enter a wrong password for root, the prompt will be closed and I won't be asked to try again... that's not very convenient!
So I was trying to create a loop that attempts to connect to MYSQL and save the password for later if successful.
I tried this, but it doesn't work.
Instead, I receive this error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
read -s -p "Enter MYSQL root password: " mysqlRootPassword
while [[ -n `mysql -u root -p$mysqlRootPassword` ]]; do
read -p "Can't connect, please retry: " mysqlRootPassword
done
I am not very experienced in bash scripting, any help would be awesome!
I don't think you need the [[ -n backtic ... ]]; test nested like that. Try:
read -s -p "Enter MYSQL root password: " mysqlRootPassword
while ! mysql -u root -p$mysqlRootPassword -e ";" ; do
read -s -p "Can't connect, please retry: " mysqlRootPassword
done
while evaluates any command group upto a closing ; do and checks the return code of last command executed to determine if the loop should be executed. Because you are looking for a failure, you have to precede the test with a logical NOT (!) OR you can use the syntactic equivalent, i.e.
until mysql -u root -p$mysqlRootPassword -e ";" ; do
read -s -p "Can't connect, please retry: " mysqlRootPassword
done
which you can think of as 'until mysql works correctly, keep trying to get the right password'.
Unfortunately, I don't have access to a mysql installation, so this is untested.
I hope this helps.