I am having trouble understanding why this line works in command line:
mysql --login-path=local
I get teh typical mysql> _ prompt as expected.
However when I use the following in my shell script:
#!/bin/bash
mysql --login-path=local -e "SELECT contractor FROM contractor" | while read contractor; do
echo "Contractor: $contractor"
done
This is what happens when I try to run that small script:
zak#yserver:$ sh contractors.sh
ERROR 1045 (28000): Access denied for user 'zak'#'localhost' (using password: NO)
The problem is, when I set up local I used the root user (I know bad idea -- but this is just a proof of concept not production) like so:
mysql_config_editor set --login-path=local --host=localhost --user=root --password
From what I have read, I am using this correctly -- And it works on command line .. Just not in my shell script -- Is there a problem with my syntax?
After much thought -- I am having trouble understanding why 1) it's using the zak username -- And 2) it's using PASSWORD NO - When I clearly set the username as root and entered a password on the prompt.
update
it also fails if I use bash instead of sh -- IE
zak#yserver:$ bash contractors.sh
ERROR 1045 (28000): Access denied for user 'zak'#'localhost' (using password: NO)
I'm not sure why it works for you on the command line, but the mysql_config_editor credentials are stored in the system user's home directory.
So if you run:
mysql_config_editor set --login-path=local --host=localhost --user=root --password
...as user zak, then the .mylogin.cnf credentials file will be stored in /home/zak/ and should have permissions set to allow only zak to access it.
If you don't pass any username or password to mysql, then it uses your system user, which explains your error message (`Access denied for user 'zak'#'localhost').
If you set the credentials as root, then they will be saved in /root. You can copy them to /home/zak/, change permissions to 600 and ownership to zak:zak and then they should work for your user as well.
Or you can just run the mysql_config_editor command as zak.
Related
I have to create a database from a bash script ,it works but I want to know if I can introduce the user password on the script
#!/bin/bash
pass ='root'
mysql -u root -p$pass -e "create database wordpress"
but i get
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
You can do that but you may not want to. The entire command line is visible via ps (for at least some time, perhaps for the entire duration of the command) and so the password would be exposed that way. Make sure you're OK with the security implications of that if you do it.
Assuming you do want to make sure you have no space between -p and your password, and when assigning the password to a variable in bash make sure there are no spaces around the =
I have to execute MySQL command without entering the MySQL prompt any interaction. For this I have to specify password at command-line , such as
mysql -u 'user' -p'password' command;
Though my user which is root is not having password (checked in mysql.user table) I am unable to use it for running the MySQL commands at commandline and not entering prompt.
I have tried using below things:
mysql -u root 'followed by query'
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
mysql -u root -p 'followed by query'
Prompts for password and when I just press enter it goes into prompt.
Please suggest how to execute this without any interaction (Using Enter key).
Query should be specified with -e option so you should try something like this:
mysql -u root -e 'followed by query'
First of all make sure MySQL user that you are using has access to database that you are trying to connect. To get execute MySQL command with out entering in MySQL prompt Also change your query like below.
mysql --user=mysql_username --password=mysql_password -e "select * from database.table";
Can’t figure this out, and all of my searching hasn’t helped.
When I login to mysql and I enter
$ mysql -u database_user -p
Enter password: ******* #here I enter my_password
this works. the next thing I see is the mysql> command prompt. However, if I enter
$ mysql -u database_user -pmy_password
or
$ mysql -u database_user --password=my_password
Both of which, according to the documentation, should be allow me access. I get the access denied response.
ERROR 1045 (28000): Access denied for user 'database_user'#'localhost' (using password: YES)
I've tried a lot of variations specifying database or host:
$ mysql -u database_user -pmy_password -d database_xxx -h localhost
But I keep getting the Access Denied error.
I want to pass my password as a command line argument so I can write some scripts to automate some tasks.
Has anyone else run into this issue or know why if I'm prompted for a password, I'm good, but if I pass it in as an argument, I cannot login.
Your password doesn't happen to have a '$' in it, like pa$$word? Or another character that might mean something to the shell?
In that case you will need to enclose your password with single quotes '
$ mysql -u database_user -p'my_password'
While I'm not sure exactly why you are having that problem, I strongly recommend against passing the password on the command line. The best practice is put the password in a secure file and use the --defaults-extra-file option. That way the password is not displayed in plaintext in the process table.
For your example, you could create a /etc/mysql_login.cnf file and permission it such that only you can read it, then put this in the file:
[client]
user=database_user
password=my_password
Then call the mysql cli like this:
mysql --defaults-extra-file=/etc/mysql_login.cnf
I just setup a fresh install of Ubuntu 14.04 with MySQL 5.6.19. After completing the installation I executed mysql_secure_installation which walked me through some security settings AND had me set a root password. My root password currently contains # and $ symbols. When I try and execute
mysql -u root -pAP#$$Here
I get the following error
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Why am I getting this error?
If I run mysql_secure_installation on the command line again and change the root password by removing the # and $ symbols I am able to connect in just fine when I run
mysql -u root -pAPassHere
On related but separate issue, I also tried to connect by issuing the command:
mysql -u root
I was expecting to be prompted for the password, but instead I got the following related error
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Why am I not being asked for a password?
Note that some symbols like $ have special meaning in the shell, and the shell expands variable into their values before passing the string to the program.
For example, try this:
$ echo AP#$$Here
AP#6282Here
The $$ is replaced with the process id of the bash shell from which I ran this command. If you want to suppress variable expansion, put the string in single-quotes:
$ echo 'AP#$$Here'
AP#$$Here
This works with the MySQL client too:
$ mysql -u root -p'AP#$$Here'
Note that you must still have no space between -p and the quoted password.
You can also get the MySQL client to prompt you interactively by using -p with no string following:
$ mysql -p
Enter password:
Finally, it may be convenient for you to enter your password in your $HOME/.my.cnf and then you won't have to type it. This also bypasses shell variable expansion, and takes the value from the config file literally.
$ cat >> $HOME/.my.cnf
[client]
user = root
password = AP#$$Here
^D
$$ will be interprepted by the shell as a variable ($$ will return the PID of the current process I think) so you'll want to surround your password in quotes like:
mysql -uroot -p 'foo$$bar'
to be prompted by the password (a more secure option as it won't be stored in your shell's history) connect specifying the password flag but with no value:
mysql -uroot -p
and then you'll be prompted for the password
you'll only get prompted for a password with the -p switch without anything following it. try wrapping your username and passwords with " and using something like this:
mysql --user=root --password="AP#$$Here"
When I try to connect to the sql server and enter the following in command prompt:
shell> mysql --user=username--password=password db_name
I get error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: Y
ES)
What does this error mean?
Looks like a space is missing between username and --password
If you don't know the root password, with Debian or Ubuntu, there is an easy way to reset it :
First, get the exact version of your server using
sudo dpkg --get-selections | grep 'mysql-server-'
Then, just use sudo dpkg-reconfigure mysql-server-5.x
(btw, replace 5.x with you real version number)
On a fresh install, the default root password is blank, so should be able to log in using just
mysql -u root
You should obviously add a root password after installation
mysqladmin -u root password [newpassword]
In most cases, you should also set up dedicated accounts with limited rights before working with a DB.
On Windows -
Search for services
Stop the service named MySQL[#] (in my case it was MySQL80).
Start the service again.
Open Command Prompt and type:
mysql [database name] -u [user name] -p
It worked for me when no other solutions worked. Hope this solves your issue as well.
It means your password is wrong or the account "root" has no access to the database on host "localhost".
Look at the right side bar. There are multiple questions equivalent to yours.