MySQL cli Syntax

Some MySQL cli syntax examples

How to create a MySQL database and import tables, data… from a sql file.

Login to the MySQL command line interface

[bash]mysql -u<username> -p[/bash]

[sql]mysql -uroot -p
mysql> create database some_database;
mysql> use some_database;
mysql> source some_database.sql;[/sql]

The third line runs the named sql file for the database you names.

Create a new user

[sql]mysql -uroot -p
mysql> USE ‘some_database’;
mysql> CREATE USER ‘new_user’@’localhost’ IDENTIFIED BY ‘user_password’;
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT SELECT,INSERT,UPDATE,DELETE ON some_database.* TO ‘new_user’@’localhost’;
[/sql]

For some reason MySQL chooses to say 0 rows affected, when it succeeds and ads a row for the user. Anyway if you get that message, it worked.

Change password for a user

[sql]mysql -uroot -p
mysql> SET PASSWORD FOR ‘username’@’localhost’ = PASSWORD(‘new_password’);[/sql]

Restart mysql: [bash]sudo /etc/init.d/mysql restart[/bash]