Connect to mysql in CLI - distinguish db name and password - mysql

I am trying the following mysql command:
zcat ***.sql.gz | mysql -u root -p dbname
Maybe the question applies equally to the following, simpler command:
mysql -u root -p dbname
You can find this kind of snippet all over the internet, and it usually works. It should prompt you for the db user's password (in this case, the db root pw), and then use "dbname" as the database name. Only today it did NOT ask me for the root pw, and instead just said
Access denied for user 'root'#'localhost' (using password: YES)
I assume that in this case mysql thought that "dbname" is the password. I just don't understand why in the usual case it understands that "dbname" is the database name.
I have also seen this variation:
mysql -u root -pthepassword dbname
Here the interesting thing is that -u is followed by a space, but -p is not.
The question: How does mysql distinguish the dbname from the password?

Related

DbDeployer - MySQL instance - Grants for new databases (or root user)

This question relates to 3rd party tool dbdeployer, located Dbdeployer at Github
The section in question:
Users:
root, with the default grants as given by the server version being installed.
I have an instance installed on port 5730 and port 5731 respectively. (Corresponds to MySQL 5.7.30 and 5.7.31).
I can connect like this:
mysql -u msandbox -p -h 127.0.0.1 -P 5730
mysql -u msandbox -p -h 127.0.0.1 -P 5731
mysql -u mycustomusername -p -h 127.0.0.1 -P 5730
I created a file for grants like shown in the article:
use the option --post-grants-sql-file to load the instructions.
> cat << EOF > orchestrator.sql
CREATE DATABASE IF NOT EXISTS orchestrator;
CREATE USER orchestrator IDENTIFIED BY 'msandbox';
GRANT ALL PRIVILEGES ON orchestrator.* TO orchestrator;
GRANT SELECT ON mysql.slave_master_info TO orchestrator;
EOF
$ dbdeployer deploy single 5.7 \
--post-grants-sql-file=$PWD/orchestrator.sql
This works fine for a new empty database deployed by the SQL script (and its grants), but I now have an existing instance, and want to create a new database from within the mysql instance.
The article claims that root should be available, but:
mysql -u root -p -h 127.0.0.1 -P 5731
Access denied for user 'root'#'localhost' (using password: YES)
I have the local instance installed on 3306, but this is not supposed to be the user I need to login with.
When I do this:
mysql -u root -p -h localhost -P 5731
I am able to login, _however this seems to ignore the port (when connecting as localhost) because I see different databases (those on port 3306 and not those from 5730/5731)!
This also confirms my suspicion that port gets ignored :
SHOW GRANTS FOR mycustomusername;
ERROR 1141 (42000): There is no such grant defined for user 'mycustomusername' on host '%'
SHOW GLOBAL VARIABLES LIKE '%port%';
.... truncated ....
port | 3306
I need to use root#host5731 and root#host5730 but there does not seem a way to use root here?
I need to do one (either) of the following:
Use root user at these ports,
Get a way to let msandbox or mycustomusername to be able to have ability to do GRANT ALL PRIVILEGES on a new database.
Why?
I cannot remove/recreate a new MySQL instance to add new databases (using the SQL file method) --post-grants-sql-file when I already have existing databases.
Dbdeployer instances and setup installed and configures the password for root to be the same password as the username specified (default username msandbox).
You cannot do this (even though some answers on the github repo claim you can)
dbdeployer deploy single 5.7.31 -u root -p somepassword
Rather what happens (and not clearly mentioned anywhere easily accessible) is that you can do the following:
dbdeployer deploy single 5.7.31 -u someuser -p somepassword
Dbdeployer setup then deploys this someuser AND root to have the same password (somepassword).
More information:
I found that I could do this:
cd /var/dbdeployer/instance/location/of/mysql.5.7.31/
./use -u root
(Not specifying the password here.)
Inspecting the ./use script, it greps the password from your configuration (which is the password for someuser.
This then gives us the ability to login via root to change grants:
mysql -u root -p -h 127.0.0.1 -P 5731
I have now changed the password from inside:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'new-password';
This prevents you from externally using ./use -u root as the password is now different than the other user.

What does mysql -u root -p do?

I am trying to figure out what the mysql -u root -p command does.
I have googled the command but I can't find any good results.
mysql -u root -p means, that you trying to connect to MySQL shell with parameters - -u parameter specified MySQL user name.
-u, --user=name User for login if not current user.
In your case it's root user.
-p, --password[=name]
Password to use when connecting to server. If password is
not given it's asked from the tty.
You can type mysql --help from the command line for more information about all available parameters.
Good luck.
It logs you into mysql as the root user. After -p (Immediately after it incidentally, no spaces) you would include the password.
`mysql -u root -p`
Its initiate a connection of MySQL.
-u means that we are going to connect with a username root
-p means that we will enter username's password
Check man mysql
Your command tries to connect to MySQL on localhost with user "root" and asking for a password

Can't login into mysql

I've just downloaded MySql using this tutorial after installing it before.
I've thought that it would solve my problems but, whenever I try to login into MySql via the terminal I'm prompted to enter the password though I've already logged in with the following command:
mysql -u user -p password
Here is what happening:
$> mysql -u user -p MYPASSWORD
Enter password:
And after I insert my password again...
$> mysql -u user -p MYPASSWORD
Enter password:
ERROR 1049 (42000): Unknown database 'MYPASSWORD'
I'd like to know if there's a solution to this weird problem.
The command is:
mysql -u user -ppassword
So in your case:
mysql -u user -p123456
By adding a space between -p and your password, you're actually setting the database to use, which is why you get the error unknown database.
The other solution would be:
mysql -u user -p
In that case, your password will be asked by the terminal. It is a bit more secure as your password does not stay in plain text in your terminal history. But if your password is 123456, I guess you're not too concerned by security ... ;)
Under normal circumstances, I would use:
mysql -uroot -p
Enter password:
you can try it.... ;)

How can I fix the MySQL 'access denied' errors after db restore?

I exported all databases of a MySQL server by:
mysqldump -u root -p --all-databases > /tmp/dbs.sql
Then I copied the file (by scp) on another server, which has the same MySQL version, and imported it with:
mysql -u root -p < dbs.sql
I can access to MySQL only as root. With other users, I obtain:
~$ mysql -u jag -p
Enter password:
ERROR 1045 (28000): Access denied for user 'jag'#'localhost' (using password: YES)
However, selecting all users in mysql.user table, I can see that all user accounts where imported. So, how can I overcome this problem, without resetting all user passwords?
You need to specify username and password, you can try this:
mysql -u USERNAME -pPASSWORD -h HOSTNAMEORIP DATABASENAME
Note that there is no space between -p parameter and password!
You can check this: http://dev.mysql.com/doc/refman/5.0/en/connecting.html
After following all the similar answers for this issue, I've solved it in CentOS with this:
http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html
please make sure to grant privileges to that user u want to restore with, in this case 'jag'

error during mysql db connection in localhost

when I am trying to connect mySql data base from cmd it giving some error like -
c:\xampp\mysql\bin>mysqladmin -u root password 123123
mysqladmin: connect to server at localhost failed
error:Access denied for user root#localhost (using password : NO)
Shouldn't that be:
mysqladmin -u root -p PASSWORD
?
The syntax looks wrong to me, just like Roberto said. You could also use this
mysqladmin -u root -h localhost -p
It should then ask you for the password. This way you can be sure that only a wrongly entered PW prevents you from connecting.
It should be:
mysqladmin -u root -p
and it will prompt you for a password. If you need to enter the password directly via the command line ( say you need to write a script), you should be able to do that with:
mysqladmin -u root -p123123