MySQL treats password in script as database - mysql

I am new to Bash scripting. I want to create a script that logs in to MySQL for me:
PASSWORD="MyPassword"
sudo service mysql start
mysql -u root -p $PASSWORD
However, it throws an error telling me that my password ($PASSWORD) is not a database.
Is there any way to do it?
Thanks and sorry if I am asking something RTFM or UTFM.

From man mysql:
If you use the short option form (-p), you cannot have a space between the option and the password.
So either
mysql -u root -p"$PASSWORD"
or
mysql -u root --password "$PASSWORD"

It is insecure to use password on the command line.
From the mysql user guide:
This is convenient but insecure. On some systems, your password becomes visible to system status programs such as ps that may be invoked by other users to display command lines.
I would suggest You to use the mysql_config_editor utility, to store your db credentials.
mysql_config_editor set --login-path=YOUR_LOGIN_PATH --host=YOUR_HOST --user=YOUR_DB_USER --password
Then it will ask for password interactively
After that you can connect to your db:
mysql --login-path=YOUR_LOGIN_PATH YOUR_DB
Example (set credentials):
mysql_config_editor set --login-path=root --host=localhost --user=root --password
Example (connect to db in your script):
mysql --login-path=root YOUR_DB

Related

passing a null password during execution of query in linux

how to pass a password during the execution of a query in Linux if the password is null;MySQL -u root -p
this asks for entering the password every time the query is executed.
It's literally the first thing in manual
shell> mysql --user=user_name --password=your_password db_name
or
mysql -uYOUR_USERNAME root -pYOUR_PASSWORD
--user=user_name, -u user_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.

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

How to unblock with mysqladmin flush hosts

I have gone through similar cases listed here but it doesn't seem to work.
I was using MySQL Workbench to establish a connection with my database which is hosted on another server. Tried a few times and unable to connect followed by this error. I am confused as to where I should even do this flush. On PhpMyadmin under the SQL query? Cos when I tried to input the following command, it returns as syntax error. I am using a windows OS thus no shell shell scripting for me to input this information either. I am accessing the database temporarily via Cpanel/ phpmyadmin now.
Please help to tell where I should input this data and if my syntax is wrong. Thanks for help.
mysqladmin flush-hosts;
or
mysqladmin -umyname -pmypassword flush-hosts;
My error message as follows:
Failed to connect to MYSql at 192...* with user myName
Host 'host-92...*.as13285.net' is blocked because of many connection
errors; unblock with 'mysqladmin flush-hosts'
mysqladmin is not a SQL statement. It's a little helper utility program you'll find on your MySQL server... and "flush-hosts" is one of the things it can do. ("status" and "shutdown" are a couple of other things that come to mind).
You type that command from a shell prompt.
Alternately, from your query browser (such as phpMyAdmin), the SQL statement you're looking for is simply this:
FLUSH HOSTS;
http://dev.mysql.com/doc/refman/5.6/en/flush.html
http://dev.mysql.com/doc/refman/5.6/en/mysqladmin.html
You should put it into command line in windows.
mysqladmin -u [username] -p flush-hosts
**** [MySQL password]
or
mysqladmin flush-hosts -u [username] -p
**** [MySQL password]
For network login use the following command:
mysqladmin -h <RDS ENDPOINT URL> -P <PORT> -u <USER> -p flush-hosts
mysqladmin -h [YOUR RDS END POINT URL] -P 3306 -u [DB USER] -p flush-hosts
you can permanently solution your problem by editing my.ini file[Mysql configuration file]
change variables max_connections = 10000;
or
login into MySQL using command line -
mysql -u [username] -p
**** [MySQL password]
put the below command into MySQL window
SET GLOBAL max_connect_errors=10000;
set global max_connections = 200;
check veritable using command-
show variables like "max_connections";
show variables like "max_connect_errors";
You can easily restart your MySql service. This kicks the error off.

Connect to database through MySql command line client

I am following this tutorial: http://dev.mysql.com/doc/refman/5.7/en/connecting-disconnecting.html
However when I run MySql command line client I automatically get asked for a password when I enter it I get connected automatically to a localhost database, how do I access other database's using something like the following as shown in the tutorial example?
shell> mysql -h host -u user -p
Enter password: ********
shell> mysql nameofdatabase -uusername -ppassword
You can put a space in between -u username but not between the -p and password.
There are multiple ways to access mysql on the command line. If you wish to connect to another database server, you need to add -h <hostname> to your mysql command. Without this, mysql assumes you want to connect to the local mysql server.
If you mean that you want to connect to another database on a host, just specify that database at the end of your command line.
$ mysql -u username -ppassword second_database
Note, there is NO space between -p and password. If -p is followed by a space, mysql will prompt you to enter a password interactively. Which you can do, if that is what you want.
$ mysql -u username -p third_database
Enter password: *********
Another way to connect would be to create a file in your home directory named .my.cnf. This file should contain the following:
[client]
user=username
password=yourpassword
If you have any special characters in your password, you will need to quote it. Having this file allows you to not need to enter any username or password on your command line:
$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
....
mysql>

mysql is prompting for password even though my password is empty

I installed mysql on ubuntu server and did not specify password. When I do
mysql -u root -p
it prompts for password and without providing any input I just hit enter and it works.
Now I need to perform some operation on the database using cron job. My cron job does not work because mysql prompts for a password. I tried doing
mysql -u root -p'' my_database
but that did not work either.
Any suggestion?
Go like this mysql -u root --password="" dbname
Try not asking mysql to prompt for the password, 'mysql -u myuser'. I would suggest you create an account with only the required privileges to do this. Also limit its access to localhost. Put a password on root.
I installed mysql on ubuntu server and did not specify password. When
I do
mysql -u root -p
-p brings up the password prompt. If you do not have a password, then you do not want to do that.
Just write:
mysql -u root
For the love of god, get a password on that account!
For passing the password in the command use -p$PASSWORD. In the following example, user/password is root/root:
mysql -proot -D my-db -u root -h 127.0.0.1 -e "select * from my_table"
IMPORTANT: notice that there is no space between -p and the password in -proot
Check MySQL Documentation for how to reset your password, since I found no way to enter a password either. You could use the following: http://dev.mysql.com/doc/mysql-windows-excerpt/5.0/en/resetting-permissions-windows.html
Which states that you have to create a file with the following query:
UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
FLUSH PRIVILEGES;
And then start up mysqld service with the --init-file parameter (see the documentation for more information about this). This should reset your root password.
Why don't you specify a password for root? For security reasons and your script would work.
Mysql's "root" account should have a password; otherwise anyone with an account on your machine has full access to the database.
Set a password (e.g. with SET PASSWORD)
Add the password to ~/.my.cnf
If you want more sane authentication options, I recommend Postgres.