MYSQL set a default database - mysql

When I connect my MYSQL , I have to choose database to use everytime using :
use mysql_crashcourse;
I noticed the DATABASE() returns null when I check it right after I connect to MYSQL
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| NULL |
+------------+
My question is : how can I set a default MYSQL database ,so that I don't need to provide database info each time I connect or login to MYSQL ?

If you log-in directly from cli.
Create a client session on my.cnf and restart mysql service.
Note every root user logged from localhost will have the default database.
Example:
root#ergesttstsrv:~# cat /etc/mysql/my.cnf
[client]
host=localhost
user=root
password=
database=gesti
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
root#ergesttstsrv:~# service mysql restart
root#ergesttstsrv:~# mysql -u root -p
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.25 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select database();
+------------+
| database() |
+------------+
| gesti |
+------------+
1 row in set (0.00 sec)

If you do not select anything else in the query, the return value is null. If you want to get anything else, you can do for example:
mysql> SELECT 1 IS NULL, 1 IS NOT "something";

Related

Cant login to mysql new user?

I've recently downloaded and started using mysql. I've set it up hopefully correct and my root user also logs in. The mysql service is also running.
I ran the following lines to create a datbase and new user, but I could not log in to my new user. Please help. I don't know what I'm doing wrong.
I've searched for a solution online but get confused as to what is the right steps to follow.
Thank you :)
PS C:\Users\Akhil> mysql -u root -p;
Enter password: *****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 5.6.41-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mytestdb |
| performance_schema |
| sakila |
| test |
| world |
+--------------------+
7 rows in set (0.00 sec)
mysql> use mytestdb
Database changed
mysql> create user myuser identified by 'mypass';
ERROR 1396 (HY000): Operation CREATE USER failed for 'myuser'#'%'
mysql> DROP USER myuser;
Query OK, 0 rows affected (0.00 sec)
mysql> create user myuser identified by 'mypass';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON mytestdb.* TO myuser;
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
PS C:\Users\Akhil> mysql -u myuser -p
Enter password: ******
ERROR 1045 (28000): Access denied for user 'myuser'#'localhost' (using password: YES)
PS C:\Users\Akhil>
Why am I getting access denied is beyond me...Help?
I've solved the error. What I did was Login with root user. Then type the following command:-
DROP USER ''#localhost ;
then it worked like a charm!

Do we need to run flush privileges after changing variable default_password_lifetime in MySQL?

MySQL has a new variables in MySQL-5.7 which keeps the mysql user's password expiration details - after how many days the password expires for a particular user.
Detail of this variables : Doc
When we changed this variable, is it necessary to run flush privileges or the changes will take affect immediately for all the users having default expire policy ?
According to the documentation, it should not be necessary:
6.3.6 Password Expiration Policy
...
When a client successfully connects, the server determines whether the
account password is expired:
The server checks whether the password has been manually expired and, if so, restricts the session.
Otherwise, the server checks whether the password is past its lifetime according to the automatic password expiration policy. If so,
the server considers the password expired and restricts the session.
...
IMPORTANT: The change take effect only for subsequent connections.
Here an example:
$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.18
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SELECT ##GLOBAL.default_password_lifetime;
+------------------------------------+
| ##GLOBAL.default_password_lifetime |
+------------------------------------+
| 0 |
+------------------------------------+
1 row in set (0.00 sec)
mysql> CREATE USER 'johndoe'#'localhost'
-> IDENTIFIED WITH mysql_native_password AS '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4'
-> PASSWORD EXPIRE DEFAULT;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
$ mysql -u johndoe -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.18
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SELECT CURRENT_USER();
+-------------------+
| CURRENT_USER() |
+-------------------+
| johndoe#localhost |
+-------------------+
1 row in set (0.00 sec)
mysql> exit
Bye
$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.18
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SET ##GLOBAL.default_password_lifetime := 1;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT ##GLOBAL.default_password_lifetime;
+------------------------------------+
| ##GLOBAL.default_password_lifetime |
+------------------------------------+
| 1 |
+------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2010-01-01 00:00:01 |
+---------------------+
1 row in set (0.00 sec)
mysql> \! date -s "2010-01-02 $(date +%H:%M:%S)"
Sat Jan 02 00:00:05 UTC 2010
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2010-01-02 00:00:06 |
+---------------------+
1 row in set (0.01 sec)
mysql> exit
Bye
$ mysql -u johndoe -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.18
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SELECT CURRENT_USER();
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
Other information of interest:
6.2.6 When Privilege Changes Take Effect.
I think the answer is no.
because default_password_lifetime is a global variables,which information stored in information_schema.GLOBAL_VARAIBLES,It's a memory engine table!
In mysql,the flush action cause the data in the buffer write back into disk,it's only necessary in MyISAM engine;however, the flush privileges clause is reload the data from disk MyISAM file to memory about account & privileges related table.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-db'

I'm trying to delete a database inside an RDS MySQL instance and I get the following error . Can someone kindly help me resolve this issue
[ec2-user#ip-10-10-1-14 ~]$ mysql -h wda2d.cmkridjjwpjp.ap-southeast-2.rds.amazonaws.com -u happy-p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6241
Server version: 5.6.19-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+-----------------------+
| Database |
+-----------------------+
| information_schema |
| database |
| innodb |
| mysql |
| performance_schema |
| richard-db |
| word-db |
| wordpress-db |
+-----------------------+
10 rows in set (0.00 sec)
mysql> DROP DATABASE wordpress-db;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-db' at line 1
mysql> Ctrl-C -- exit!
Aborted
Dashes are not valid identifier characters unless you wrap them in ticks. (Otherwise they represent the math operation of subtraction).
DROP DATABASE `wordpress-db`;
I recommend renaming those tables to follow best practices and use underscores instead.

How to get hold of Amazon MySQL RDS certificates

Amazon RDS documentation (http://aws.amazon.com/rds/faqs/#53) specifies that "Amazon RDS generates an SSL certificate for each [MySQL] DB Instance". I haven't been able to find any documentation on how to find the certificates and the certificates are nowhere to be found in the management console.
Where are the certificates?
I found the solution here: https://forums.aws.amazon.com/thread.jspa?threadID=62110.
Download ca cert file from here: https://s3.amazonaws.com/rds-downloads/mysql-ssl-ca-cert.pem
curl -O https://s3.amazonaws.com/rds-downloads/mysql-ssl-ca-cert.pem
Connect to mysql:
mysql -uusername -p --host=host --ssl-ca=mysql-ssl-ca-cert.pem
Check that your connection is really encrypted:
mysql> SHOW STATUS LIKE 'Ssl_cipher';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| Ssl_cipher | AES256-SHA |
+---------------+------------+
1 row in set (0.00 sec)
Optionally force SSL for a specific user to connect to MySQL
mysql> ALTER USER 'username'#'host|%' REQUIRE SSL
You can get the AWS RDS certificate file information from the AWS Documentation guide itself
http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_MySQL.html
Download the certificate from here
https://rds.amazonaws.com/doc/mysql-ssl-ca-cert.pem
Update - Amazon updated the SSL certificate, you can download the it from here : https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem
Use the following command to login into mysql
root#sathish:/usr/src# mysql -h awssathish.xxyyzz.eu-west-1.rds.amazonaws.com -u awssathish -p --ssl-ca=mysql-ssl-ca-cert.pem
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.6.13-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> GRANT USAGE ON *.* TO ‘awssathish’#’%’ REQUIRE SSL
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql> show variables like "%ssl";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| have_openssl | YES |
| have_ssl | YES |
+---------------+-------+
2 rows in set (0.00 sec)
mysql>
mysql> SHOW STATUS LIKE 'Ssl_cipher';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| Ssl_cipher | AES256-SHA |
+---------------+------------+
1 row in set (0.01 sec)
mysql> exit
Bye
Where
awssathish.xxyyzz.eu-west-1.rds.amazonaws.com
is Endpoint of RDS,
awssathish
is the username of the rds server
I used http://aws-blog.io/2016/rds-over-ssl/
You have to get root pem and pem for the region and concatenate 2 files in one.
https://s3.amazonaws.com/rds-downloads/rds-ca-2015-us-west-2.pem
https://s3.amazonaws.com/rds-downloads/rds-ca-2015-root.pem
And merge files to have single rds-ca-2015-us-west-2-bundle.pem file.
With --ssl-ca provide full path to you pem file.

ERROR 1044 (42000): Access denied for user

I cannot figure out how to log into my database, I keep getting errors and cannot execute any queries on the database, here is an example of my error.
HW-001b63b70b4c:~ imac$ /usr/local/mysql/bin/mysql;
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.13 MySQL Community Server (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
2 rows in set (0.00 sec)
mysql> create database dev;
ERROR 1044 (42000): Access denied for user ''#'localhost' to database 'dev'
mysql>
What about logging in as root:
/usr/local/mysql/bin/mysql -u root