更改mysql數據庫root的密碼

首次進入數據庫是不用密碼的:html

[root@localhost ~]# /usr/local/mysql/bin/mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.40-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

退出的話,直接輸入quit或者exit便可。細心的讀者也許會發現,阿銘在上一條命令中,使用的是絕對路徑,這樣不方便,可是單獨只是輸入一個 「mysql」 命令是不行的,由於 「/usr/local/mysql/bin」 沒有在 PATH 這個環境變量裏。如何把它加入環境變量PATH中?以前阿銘介紹過:python

[root@localhost ~]# PATH=$PATH:/usr/local/mysql/bin

這樣就能夠了,但重啓Linux後還會失效,因此須要讓它開機加載:mysql

[root@localhost ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@localhost ~]# source /etc/profile
[root@localhost ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.40-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

阿銘再來解釋一下上一條命令 -u 的含義,它用來指定要登陸的用戶,後邊能夠有空格,也能夠無空格,root用戶是mysql自帶的管理員帳戶,默認沒有密碼的,那麼如何給root用戶設定密碼?按以下操做:sql

[root@localhost ~]# mysqladmin -uroot password 'yourpassword'

這樣就設置了 ‘root’ 帳號的密碼了,不妨再來用上面的命令登錄一下試試看:shell

[root@localhost ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password:NO)

報錯了,這是在提示咱們,root帳號是須要密碼登錄的。數據庫

[root@localhost ~]# mysql -uroot -p'yourpassword'
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.1.40-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

須要加一個 -p 選項,它後面能夠直接跟密碼,後面不能夠有空格,不過密碼最好用單引號括起來,不括也能夠,可是密碼中若是有特殊字符就會有問題了,因此最好是括起來吧。固然, -p後面也是能夠不加密碼,而是和用戶交互的方式,讓咱們輸入密碼:服務器

[root@localhost ~]# mysql -uroot -p
Enter password:

鏈接數據庫

剛剛講過經過使用 mysql -u root -p 就能夠鏈接數據庫了,但這只是鏈接的本地的數據庫 「localhost」, 但是有不少時候都是去鏈接網絡中的某一個主機上的mysql。網絡

[root@localhost ~]# mysql -uroot -p -h192.168.137.10 -P3306
Enter password:

其中後邊的 -P(大寫) 用來指定遠程主機mysql的綁定端口,默認都是3306, -h 用來指定遠程主機的IP.ide

一些基本的MySQL操做命令

1. 查詢當前的庫post

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.06 sec)

mysql的命令,結尾處須要加一個分號。

2. 查詢某個庫的表

首先須要切換到某個庫裏去:

mysql> use mysql;
Database changed

而後再把表列出來:

mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
23 rows in set (0.06 sec)

3. 查看某個表的所有字段

mysql> desc slow_log;
+----------------+------------------+------+-----+-------------------+-----------------------------+
| Field          | Type             | Null | Key | Default           | Extra                       |
+----------------+------------------+------+-----+-------------------+-----------------------------+
| start_time     | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| user_host      | mediumtext       | NO   |     | NULL              |                             |
| query_time     | time             | NO   |     | NULL              |                             |
| lock_time      | time             | NO   |     | NULL              |                             |
| rows_sent      | int(11)          | NO   |     | NULL              |                             |
| rows_examined  | int(11)          | NO   |     | NULL              |                             |
| db             | varchar(512)     | NO   |     | NULL              |                             |
| last_insert_id | int(11)          | NO   |     | NULL              |                             |
| insert_id      | int(11)          | NO   |     | NULL              |                             |
| server_id      | int(10) unsigned | NO   |     | NULL              |                             |
| sql_text       | mediumtext       | NO   |     | NULL              |                             |
+----------------+------------------+------+-----+-------------------+-----------------------------+
11 rows in set (0.04 sec)

也能夠使用兩一條命令,顯示比這個更詳細,並且能夠把建表語句所有列出來:

mysql> show create table slow_log\G;
*************************** 1. row ***************************
       Table: slow_log
Create Table: CREATE TABLE `slow_log` (
  `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP,
  `user_host` mediumtext NOT NULL,
  `query_time` time NOT NULL,
  `lock_time` time NOT NULL,
  `rows_sent` int(11) NOT NULL,
  `rows_examined` int(11) NOT NULL,
  `db` varchar(512) NOT NULL,
  `last_insert_id` int(11) NOT NULL,
  `insert_id` int(11) NOT NULL,
  `server_id` int(10) unsigned NOT NULL,
  `sql_text` mediumtext NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log'
1 row in set (0.01 sec)

4. 查看當前是哪一個用戶

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

5. 查看當前所使用數據庫

mysql> select database();
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.01 sec)

6. 建立一個新庫

mysql> create database db1;
Query OK, 1 row affected (0.05 sec)

7. 建立一個新表

mysql> use db1;
Database changed
mysql> create table t1 (`id` int(4), `name` char(40));
Query OK, 0 rows affected (0.02 sec)

要注意的是,字段名須要用反引號括起來。

8. 查看當前數據庫版本

mysql> select version();
+------------+
| version()  |
+------------+
| 5.1.40-log |
+------------+
1 row in set (0.01 sec)

9. 查看當前mysql狀態

mysql> show status;
+-----------------------------------+----------+
| Variable_name                     | Value    |
+-----------------------------------+----------+
| Aborted_clients                   | 0        |
| Aborted_connects                  | 5        |
| Binlog_cache_disk_use             | 0        |
| Binlog_cache_use                  | 0        |
| Bytes_received                    | 303      |
| Bytes_sent                        | 7001     |

因爲內容太長,阿銘沒有所有列出來,若是有興趣能夠網上找資料查一下每一行的含義。

10. 查看mysql的參數

mysql> show variables;
+-----------------------------------------+---------------------+
| Variable_name                           | Value               |
+-----------------------------------------+---------------------+
| auto_increment_increment                | 1                   |
| auto_increment_offset                   | 1                   |
| autocommit                              | ON                  |
| automatic_sp_privileges                 | ON                  |
| back_log                                | 50                  |
| basedir                                 | /usr/local/mysql/   |

限於篇幅,阿銘省略了不少參數沒有顯示,其中不少參數都是能夠在/etc/my.cnf中定義的,而且有部分參數是能夠在線編輯的。

11. 修改mysql的參數

mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 10    |
| max_connections    | 151   |
+--------------------+-------+
2 rows in set (0.00 sec)

mysql> set global max_connect_errors = 1000;
Query OK, 0 rows affected (0.01 sec)

mysql> show variables like 'max_connect_errors';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| max_connect_errors | 1000  |
+--------------------+-------+
1 row in set (0.01 sec)

在mysql命令行, 「%」 相似於shell下的 *, 表示萬能匹配。使用 「set global」 能夠臨時修改某些參數,可是重啓mysqld服務後還會變爲原來的,因此要想恆久生效,須要在配置文件 my.cnf 中定義。

12. 查看當前mysql服務器的隊列

這個在平常的管理工做中使用最爲頻繁,由於使用它能夠查看當前mysql在幹什麼,能夠發現是否有鎖表:

mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
| 13 | root | localhost | db1  | Query   |    0 | NULL  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.01 sec)

13. 建立一個普通用戶並受權

mysql> grant all on *.* to user1 identified by '123456';
Query OK, 0 rows affected (0.01 sec)

all 表示全部的權限(讀、寫、查詢、刪除等等操做), *.* 前面的 * 表示全部的數據庫,後面的 * 表示全部的表,identified by 後面跟密碼,用單引號括起來。這裏的user1指的是localhost上的user1,若是是給網絡上的其餘機器上的某個用戶受權則這樣:

mysql> grant all on db1.* to 'user2'@'10.0.2.100' identified by '111222';
Query OK, 0 rows affected (0.01 sec)

用戶和主機的IP之間有一個@,另外主機IP那裏能夠用%替代,表示全部主機,例如:

mysql> grant all on db1.* to 'user3'@'%' identified by '231222';
Query OK, 0 rows affected (0.00 sec)

一些經常使用的sql

1. 查詢語句

mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
|        8 |
+----------+
1 row in set (0.00 sec)

mysql.user表示mysql庫的user表;count(*)表示表中共有多少行。

mysql> select * from mysql.db;

這個用來表示查詢mysql庫的db表中的全部數據,也能夠查詢單個字段或者多個字段:

mysql> select db from mysql.db;
mysql> select db,user  from mysql.db;

一樣,在查詢語句中能夠使用萬能匹配 「%」

mysql> select * from mysql.db where host like '10.0.%';

2. 插入一行

mysql> insert into db1.t1 values (1, 'abc');
Query OK, 1 row affected (0.02 sec)

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | abc  |
+------+------+
1 row in set (0.00 sec)

3. 更改表的某一行

mysql> update db1.t1 set name='aaa' where id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | aaa  |
+------+------+
1 row in set (0.00 sec)

4. 清空表數據

mysql> truncate table db1.t1;
Query OK, 0 rows affected (0.01 sec)

mysql> select count(*) from db1.t1;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

5. 刪除表

mysql> drop table db1.t1;
Query OK, 0 rows affected (0.00 sec)

6. 刪除數據庫

mysql> drop database db1;
Query OK, 0 rows affected (0.02 sec)

mysql數據庫的備份與恢復

備份:

[root@localhost ~]# mysqldump  -uroot -p'yourpassword' mysql >/tmp/mysql.sql

使用 mysqldump 命令備份數據庫,-u 和 -p 兩個選項使用方法和前面說的 mysql 一樣,然後面的 「mysql」 指的是庫名,而後重定向到一個文本文檔裏。備份完後,你能夠查看 /tmp/mysql.sql 這個文件裏的內容。

恢復和備份正好相反:

[root@localhost ~]# mysql -uroot -p'yourpassword' mysql </tmp/mysql.sql