Create a database with mysqladmin - mysql

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 =

Related

Can't run mysql commands as root linux user

As linux root user:
root#local:~# mysql
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using
password: NO)
I'm confused. Shouldn't the root user be able to run mysql without additional authentication? How do I fix this?
Your root account in MySql has a password. Try this
mysql -p
and enter the password you, or somebody set.
If that doesn't work try
mysql -h localhost -u root -p
It that doesn't work you'll need to reset your MySql root password. That's the topic of several tutorials on the 'toobz
If the MySQL root user has a password, using the default authentication plugin, then you must provide a password to connect, full stop. You can configure an account with no password, but that's a habit you should avoid.
You can provide a password by any of the following means:
Entering it interactively with the -p option
Entering it in plaintext in the commandline like -p<mypassword>
Storing it in plaintext in your ~/.my.cnf file in the [client] section (see https://dev.mysql.com/doc/refman/8.0/en/option-files.html)
Storing it in an obfuscated form in your ~/.mylogin.cnf file (see https://dev.mysql.com/doc/refman/8.0/en/mysql-config-editor.html).
There's also a way to provide a password through environment variables, but that's discouraged now because it's really not secure.
ALTER USER 'root'#'localhost' IDENTIFIED WITH auth_socket;

Using mysql_config_editor within shell script

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.

Cannot log in to mysql using root

So I'm setting up mysql for the first time.
I read somewhere that I have to run this
mysql -u root -p
To log in as root, in order to be able to create tables/databases using the mysql command line.
When I run this, I get prompted for a password - I hit enter (I thought the default password was blank).
I get this error
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
What's going on?
If you have a fresh installation of MySql you should be able to issue the following command and it should allow your connection.
xyz#ubuntu$ mysql -u root mysql
You then should set the root password and then create a user with the correct level of access.
look HERE for help connecting.

mysql cli; password works when prompted, but not when passes as command argument

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

Access denied for user 'root'#'localhost' with certain passwords only

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"