更改mysql數據庫root的密碼
首次進入數據庫是不用密碼的:
[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中?之前阿銘介紹過:
[root@localhost ~]# PATH=$PATH:/usr/local/mysql/bin
這樣就可以了,但重啟Linux后還會失效,所以需要讓它開機加載:
[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用戶設定密碼?按如下操作:
[root@localhost ~]# mysqladmin -uroot password 'yourpassword'
這樣就設置了 ‘root’ 賬號的密碼了,不妨再來用上面的命令登陸一下試試看:
[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.
一些基本的MySQL操作命令
1. 查詢當前的庫
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