Uncategorized

MySQL force user change password

Since version 5.6.6 MySQL has introduced the capability to force an user to change their password.
You can archive that by issue ALTER USER PASSWORD EXPIRE statement.

ALTER USER 'marcelo'@'localhost' PASSWORD EXPIRE;

Next time marcelo tries to login from localhost it will block all statements rather then SET PASSWORD;

mysql> \s
ERROR 1820 (HY000): You must SET PASSWORD before executing this statement

To remove this block, just issue an :

SET PASSWORD = PASSWORD('pwd');
#Or use the hash received from SELECT PASSWORD('pwd');
SET PASSWORD = '*975B2CD4FF9AE554FE8AD33168FBFC326D2021DD';

But be careful, if you expire the password for an account, you won’t be able to login with any client version before 5.6.10(GA):

ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.

As Peter Zaitsev wrote on his post in some rpm installation, MySQL install the root user with a temporary password and force you to change it in the next login.

More information can be find at MySQL Password Expiration Documentation and MySQL ALTER USER Documentation

How to rename table in MYSQL

To rename a table in MySQL you just need to run a command named RENAME TABLE, the syntax is very easy to use,

RENAME TABLE tb1 TO tb2;

The RENAME TABLE command will rename the table atomically, which means your table will be locked during the command.
You can also rename more than one table in one command:

RENAME TABLE tb1 TO tb2, tb3 TO tb4;

To rename the table you will need ALTER and DROP privileges on the old table and CREATE and INSERT on the new one.

You just need to be careful if you use triggers, on the bellow example, after a INSERT on table Cities, I count +1 to NOfCities on the iserted Countries, what happens if I rename the table Contries?

mysql> SHOW TRIGGERS\G
*************************** 1. row ***************************
             Trigger: PlusOneCities
               Event: INSERT
               Table: Cities
           Statement: UPDATE Countries SET NOfCities = NOfCities + 1 WHERE Code = NEW.CountryCode
              Timing: AFTER
             Created: NULL
            sql_mode: 
             Definer: root@localhost
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: latin1_swedish_ci
1 row in set (0.00 sec)

mysql> INSERT INTO Cities SET Name = 'City1', CountryCode = 'BRA', District = 'Brasilia', Population = 10000;
Query OK, 1 row affected (0.04 sec)

mysql> RENAME TABLE Countries TO Country;
Query OK, 0 rows affected (0.03 sec)

mysql> INSERT INTO Cities SET Name = 'City2', CountryCode = 'BRA', District = 'Brasilia', Population = 10000;
ERROR 1146 (42S02): Table 'world.Countries' doesn't exist

See, we need to manualy change all triggers.

You can find more information on MySQL Documentation

How to use IN – AND – OR operators in MySQL

MySQL has 3 very useful operators named IN –  OR – AND. They behave very differently and is very common to misuse them.
When I use one and when I use the other?

we will use the city table to this tutorial:

+-------------+----------+------+-----+---------+----------------+
| Field       | Type     | Null | Key | Default | Extra          |
+-------------+----------+------+-----+---------+----------------+
| ID          | int(11)  | NO   | PRI | NULL    | auto_increment |
| Name        | char(35) | NO   |     |         |                |
| CountryCode | char(3)  | NO   | MUL |         |                |
| District    | char(20) | NO   |     |         |                |
| Population  | int(11)  | NO   |     | 0       |                |
+-------------+----------+------+-----+---------+----------------+

Let’s see a simple rule to clarify this:

MySQL AND Operator

We use the AND operator to add a new condition which will need to be true:

SELECT * FROM City WHERE CountryCode = 'BRA' AND Population > 1000000 AND District LIKE 'São Paulo';

This query will return Cities which CountryCode is BRA and Population is bigger than 1000000 and Discrict is São Paulo, this three condition need to be true.

MySQL OR Operator

We use the OR operator to add a new condition which or one or other needs to be true:

SELECT * FROM City WHERE Population > 1000000 OR District LIKE 'São Paulo';

This query will return Cities which have a population bigger than 1000000 or have São Paulo as their district .

MySQL IN Operator

We use the IN operator the same way which we use the OR operator but for the same column:

SELECT * FROM City WHERE CountryCode = 'USA' OR CountryCode = 'BRA' OR CountryCode = 'IRL';
SELECT * FROM City WHERE CountryCode IN ('USA', 'BRA', 'IRL');

Both queries will return the same result, all cities which are in USA, BRA or IRL.

How to rename a database in MySQL?

Today we will talk how do you can easily rename a database in MySQL.
At the MySQL version 5.1.7 was added a command to do this job named RENAME DATABASE

RENAME {DATABASE | SCHEMA} db_name TO new_db_name;

But it was removed on the MySQL version 5.1.23 because maybe it would result in data lost’s.

Then, let’s see some ways in which we can do this:

1. Using the RENAME TABLE command

As long as two databases are on the same file system, you can use RENAME TABLE to move a table from one database to another:

RENAME TABLE current_db.tbl_name TO other_db.tbl_name;

RENAME TABLE Syntax na Documentacao do MySQL

mysql> show tables;
+---------------------+
| Tables_in_oldSchema |
+---------------------+
| tb1                 |
| tb10                |
| tb2                 |
| tb3                 |
| tb4                 |
| tb5                 |
| tb6                 |
| tb7                 |
| tb8                 |
| tb9                 |
+---------------------+
10 rows in set (0.00 sec)

Now, let’s do a query to get our ALTER TABLE command:

mysql> SELECT CONCAT('RENAME TABLE ', GROUP_CONCAT( table_schema,'.',table_name, ' TO ','new_schema.',table_name,' '),';') as stmt FROM information_schema.TABLES WHERE table_schema LIKE 'oldSchema' GROUP BY table_schema\G
*************************** 1. row ***************************
stmt: RENAME TABLE oldSchema.tb10 TO new_schema.tb10 ,oldSchema.tb1 TO new_schema.tb1 ,oldSchema.tb9 TO new_schema.tb9 ,oldSchema.tb8 TO new_schema.tb8 ,oldSchema.tb7 TO new_schema.tb7 ,oldSchema.tb6 TO new_schema.tb6 ,oldSchema.tb5 TO new_schema.tb5 ,oldSchema.tb4 TO new_schema.tb4 ,oldSchema.tb3 TO new_schema.tb3 ,oldSchema.tb2 TO new_schema.tb2 ;
1 row in set (0.01 sec)

UPDATE:
if the output of the above query isn’t complete, that is because GROUP_CONCAT() function limits the result length, to change it run the below query:

SET SESSION group_concat_max_len = 4294967295;

It’s this, now we just need to run the returned statement as a new query to move all tables from the oldSchema to new one, don’t forget, first you need to create your new database after this you can run the returned statement as a new query.

2. using mysqldump

mysqldump [OPTIONS] --database oldSchema > oldSchema.sql
mysql new_schema < oldSchema.sql

Good, it’s this for today. enjoy!!!