mysql connection whitespace after user flag - mysql

I have a question, What is the significance of the whitespace after the user flag -u inside a mysql connection string?
mysql -u myname -pmypass mydb
I mean to say, the above command works just fine, if I don't pass a whitespace after -u and just write the above command as below.
mysql -umyname -pmypass mydb
I tried searching for an explaination by using the following search terms.
mysql login whitespace after user flag
mysql connection console whitespace after user flag
I found the mention of whitespace after the password flag -p, Here. But the page no where talks about the whitespace after the user flag -u.
....there must be no space between -p or --password= and the password following it.

The -p option takes an optional argument:
mysql --help --verbose
...
-p, --password[=name]
Password to use when connecting to server. If password is
not given it's asked from the tty.
So, a command line that contains:
-p mydb
would be ambiguous ... does this means to:
Prompt the user for a password, and use the mydb database ?
Use the password mydb, and not set a default database ?
This is why the documentation insists on a particular rule for the password option: the value, if given, must be attached (no space).
For options that always take an argument, like -u, there are no special considerations, so the white space is not significant.
See the manual for a full documentation of command line options:
http://dev.mysql.com/doc/refman/5.7/en/command-line-options.html

Related

Mysql command line showing access denied for #'172.X' when using --password but not when interactive

Not sure why this is happening.
My user has the right password/privileges (where host is '%')
If I run the command as such:
mysql -h myhost -u myuser -p
Password:
It connects.
However if I do:
mysql -h myhost -u myuser --password=
I get Access Denied for user 'myuser'#'172.xxx' <-- internal IP
Not sure why this is happening and I can't seem to figure out how to resolve it. Adding it the host for 172.% doesn't seem to work either.
Thanks in advance.
From man mysql (emphasis mine):
SYNOPSIS
   mysql [options] db_name
--password[=password], -p[password]
The password to use when connecting to the server. If you use the short option form (-p), you cannot have a space between the option and the password. If you omit the password value following the --password or -p option on the command line, mysql prompts for one. [...]
Let's concentrate on the first command you mentioned:
mysql -h myhost -u myuser -p Password
You said that this works, but in fact, it shouldn't. According to the manual, there must be no space between -p and the password. If there is one (as in your case), Password will just be interpreted as additional command line parameter (in this case: the last command line parameter).
On the other hand, according to the manual, the last command line parameter is the name of the database to use.
Thus, according to my understanding of the manual, the following should happen when the command shown above is entered:
The MySQL client should connect to myhost and then (on the command line) ask for the password for myuser (because -p is given without the actual password from its point of view). If the password is entered correctly, it should login myuser and switch immediately to the database which has Password (i.e. the last command line parameter) as its name.
If it connects immediately without prompting for myuser's password (as you wrote), then there probably is no password for myuser yet.
In every case, after the actual login is done, you should get an error message because there probably is no database which has Password (i.e. the last command line parameter) as its name. Could you please tell us if you get such an error message after login, and if there is a database which has Password (i.e. the last command line parameter) as its name?
If we know the reason why the command shown above works although it shouldn't, then we will know the reason why your second command (mysql -h myhost -u myuser --password=) fails.
EDIT 1 (after you have edited your question)
This behavior is expected. Saying --password= is formally the same as saying --password=''; in other words, you are telling the MySQL client that it should use the empty string as password.
If you want it to prompt for the password, then just use --password (without the equal sign) instead. Your command line line then would be:
mysql -h myhost -u myuser --password

bash: How do I use a connection string to connect to a MySQL database?

Can I pass this string
mysql2://user1:rXbNgLjBHrdxYT#localhost/onebody?socket=/opt/bitnami/mysql/tmp/mysql.sock
to the mysql command inside bash, to make a connection?
If so, how...
Not literally. mysql CLI takes several options into which the string needs to be split (see man mysql).
Assuming "onebody" is the DB name, I guess it is:
mysql --protocol=socket --socket=/opt/bitnami/mysql/tmp/mysql.sock --database=onebody --user=user1 --password=rXbNgLjBHrdxYT.
If you omit the password value following the --password or -p option
on the command line, mysql prompts for one.
Specifying a password on the command line should be considered insecure. See Section 6.1.2.1, “End-User Guidelines for Password Security”. You can use an option file to avoid giving the password on the command line.

Unable to direct insert in mysql database from linux xshell

i have run below command to insert data into my mysql database. sql query is ok. but after run the query, a prompt is coming to enter the password. but in my database no password is set. SO if i press enter key without writing anything then it is inserting data. How can i avoid to this prompt. That means i want that command will be directly working. No prompt will come for Enter Password.
Enter password:
linux-pott:/opt/lampp/htdocs # mysql -uroot -p -e 'insert into dialer_rate(date_time,time,mno,trx_type,trx_result,trx_value) values("2015-02-19","12:14","air","N/A","Not_Running",0) ' bkash
Enter password:
linux-pott:/opt/lampp/htdocs #
remove the option -p in your cmd:
linux-pott:/opt/lampp/htdocs # mysql -u root -e 'insert into dialer_rate(date_time,time,mno,trx_type,trx_result,trx_value) values("2015-02-19","12:14","air","N/A","Not_Running",0) ' bkash
From the official documentation:
For a long option that takes a value, separate the option name and the value by an “=” sign. For a short option that takes a value, the option value can immediately follow the option letter, or there can be a space between: -hlocalhost and -h localhost are equivalent. An exception to this rule is the option for specifying your MySQL password. This option can be given in long form as --password=pass_val or as --password. In the latter case (with no password value given), the program prompts you for the password. The password option also may be given in short form as -ppass_val or as -p. However, for the short form, if the password value is given, it must follow the option letter with no intervening space. The reason for this is that if a space follows the option letter, the program has no way to tell whether a following argument is supposed to be the password value or some other kind of argument. Consequently, the following two commands have two completely different meanings:

mysql asking twice for password even though it was already provided

I have this script:
read PASS
mysql -u root -p "$PASS" < myfile.sql
If I already provided the password, why am I getting a second prompt to enter the password for mysql?
The syntax is either:
mysql -u root -p"$PASS" < myfile.sql
with no space after -p or:
mysql -u root --password="$PASS" < myfile.sql
The explanation for the first syntax is in the Documentation. For most options, the space between the option and its value is optional. But it says:
An exception to this rule is the option for specifying your MySQL password. This option can be given in long form as --password=pass_val or as --password. In the latter case (with no password value given), the program prompts you for the password. The password option also may be given in short form as -ppass_val or as -p. However, for the short form, if the password value is given, it must follow the option letter with no intervening space. The reason for this is that if a space follows the option letter, the program has no way to tell whether a following argument is supposed to be the password value or some other kind of argument.

Does mysqldump --password really do what it says?

I'm trying to use mysqldump to dump a schema, and it mostly works but I ran into one curiosity: the -p or --password option seems like it is doing something other than setting the password (as the man page and --help output say it should).
Specifically, it looks like it's doing what is indicated here: http://snippets.dzone.com/posts/show/360 - that is, setting the database to dump.
To support my somewhat outlandish claim, I can tell you that if I do not specify the --password (or -p) option, the command prints the usage statement and exits with an error. If I do specify it, I am immediately prompted to enter a password (!), and then the database specified in the --password option is dumped (or an error is given in the usual case that a password not matching any database name was specified).
Here's a transcript:
$ mysqldump -u test -h myhost --no-data --tables --password lose
Enter password:
-- MySQL dump 10.10
mysqldump: Got error: 1044: Access denied for user 'test'#'%' to
database 'lose' when selecting the database
So, what gives? Is this the way this is supposed to work? It surely does not appear to make sense nor does it match the official documentation. And finally, if this just the way it works, how am I meant to specify the password to be used in an automated job? Using expect???
I'm using mysqldump Ver 10.10 Distrib 5.0.22, for pc-linux-gnu (i486).
From man mysqldump:
--password[=password], -p[password]
The password to use when connecting to the server. If you use
the short option form (-p), you cannot have a space between the option
and the password. If you omit the password value following the
--password or -p option on the command line, you are prompted for
one.
Specifying a password on the command line should be considered
insecure. See Section 6.6, "Keeping Your Password Secure".
Syntactically, you are not using the --password switch correctly. As such, the command line parser is seeing your use of "lose" as a stand-alone argument which mysqldump interprets as the database name as it would if you were to attempt a simpler command like mysqldump lose
To correct this, try using --password=lose or -plose or simply use -p or --password and type the password when prompted.
Another option is to create the file ~/.my.cnf (permissions need to be 600).
Add this to the .my.cnf file
[client]
password=lose
This lets you connect as a MySQL user who requires a password without having to actually enter the password. You don't even need the -p or --password.
Very handy for scripting mysql & mysqldump commands.
I found that this happens if your password has special characters in it. The mysql password here has a ! in it, so I have to do ==password='xxx!xxxx' for it to work corrrectly. Note the ' marks.
Try placing a '=' in between --password lose like:
--password=lose
If you use -p, then there can be no space between the -p and the password, i.e. '-plose'.
I am not sure if it works for the --password version, but if you use -p you can specify the password immediately afterwards (the key is not to include a space):
mysqldump -pmypassword ...
Did you try --password=whatever-password-is ?
Perhaps I'm missing the question, but that is what I do to run the tool.
If you use the -p or --password without an argument, you will get a prompt, asking to insert a password.
If you want to indicate a password on the command line, you must use -pYOURPASSWORD or --password=YOURPASSWORD. Notice that there is no space after -p, and there is an "=" sign after --password.
In your example, mysqldump asks for a password, and then treats "lose" as the database name. If that was your password, you should have included a "="
The -p option does not require an argument. You just put -p or --password to indicate that you're going to use a password to access the database. The reason it's dumping the database named whatever you put after -p is that the last argument for mysqldump should be the name of the database you want to dump (or --all-databases if you want them all).
#Nathan's answer is also true. You can specify the password immediately following the -p switch (useful in scripts and such where you can't enter it by hand after executing the command).
--password[=password]
Here is the documentation
Maybe your user "test" doesn't have the permission to access your "lose" database?