第13章 MySQL經常使用操做

13.1 設置、更改root用戶密碼

若是沒有啓動mysql 首先要啓動mysqlpython

/etc/init.d/mysql startmysql

首次直接使用mysql會提示‘該命令不存在’,緣由是尚未將該命令加入環境變量,若是要使用該命令,須要使用其絕對路徑:/usr/local/mysql/bin/mysql,爲了方便,先將其加入系統環境變量:sql

[root@xuexi-001 ~]# export PATH=$PATH:/usr/local/mysql/bin/

至此,mysql命令路徑暫時加入環境變量,系統重啓後該變量會失效,若要永久生效,須要將其加入環境變量配置文件:shell

[root@xuexi-001 ~]# vim /etc/profile
……
export PATH=$PATH:/usr/local/mysql/bin/

刷新配置文件(不然不生效):
[root@1 ~]# source /etc/profile

設置 & 更改密碼

首次登錄mysql,root用戶沒有密碼,直接登陸:數據庫

[root@xuexi-001 ~]# mysql -uroot
#-u:=user,指定用戶名
Welcome to the MySQL monitor.  Commands end with ; or \g.
……
mysql> quit
#退出

說明: 登陸mysql以後能夠進行與mysql相關的一些操做,可是設置mysql用戶的密碼須要執行如下操做!vim

設置密碼

[root@xuexi-001 ~]# mysqladmin -uroot password '123456'  

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

說明: 設置密碼後直接登陸會報錯(ERROR),須要輸入密碼登陸。安全

[root@xuexi-001 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.
mysql>

注: -p=passwd,使用密碼登陸,在此能夠將密碼直接輸入在命令行(跟在-p後面,不加空格:-p'123456'<此處單引號能夠不加,可是當密碼中有特殊符號時必須加,因此在命令行輸入密碼時養成習慣:加單引號>),也能夠不在命令行輸入,只跟-p選項,而後根據提示信息:「Enter password」,輸入密碼進行登陸(此方法不會暴露用戶密碼,安全)。dom

更改密碼

  • 當知道用戶密碼時,進行密碼更改:
先編輯mysql配置文件:
[root@xuexi-001 ~]# vim /etc/my.cnf
[mysqld]
skip-grant
#忽略受權!
datadir=/data/mysql
socket=/tmp/mysql.sock

重啓mysql服務:
[root@xuexi-001 ~]# /etc/init.d/mysqld restart
Shutting down MySQL... SUCCESS! 
Starting MySQL..................... SUCCESS!

說明: 完成該操做以後就能夠任意登陸mysql了(無需密碼),因此此時mysql安全性不好,平時配置文件中必定不要添加該參數!!!socket

[root@xuexi-001 ~]# mysql -uroot
Welcome to the MySQL monitor.  
mysql> use mysql;
#切換mysql庫
Database changed
mysql> select * from user\G;
#查看用戶的表信息,該表中存放的是用戶相關信息(密碼、受權…)
#G選項的做用是使輸出信息有序顯示,不加該選項,顯示內容會很亂  
mysql> select password from user;
#查看用戶密碼,顯示結果Wie加密字符串!  
mysql> update user set password=password('123456') where user='root';
Query OK, 4 rows affected (0.11 sec)
Rows matched: 4  Changed: 4  Warnings: 0
#將密碼更改成‘123456’
mysql> quit
Bye

密碼更改爲功ide

恢復配置文件:
[root@xuexi-001 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock

重啓mysql服務:
[root@xuexi-001 ~]# /etc/init.d/mysqld restart 
Shutting down MySQL.. SUCCESS! 
Starting MySQL........... SUCCESS! 

登陸:
[root@xuexi-001 ~]# mysql -uroot -p'123456'
Welcome to the MySQL monitor.
mysql> quit
Bye

步驟: vim /etc/my.cnf-->添加skip-grant-->mysql restart-->登陸-->use mysql-->update user set password=...-->vim /etc/my.cnf-->刪除skip-grant-->mysql restart。

13.2 鏈接mysql(本地、遠程)

遠程鏈接:使用IP/port鏈接

[root@xuexi-001 ~]# mysql -uroot -p123456 -h127.0.0.1 -P3306
Welcome to the MySQL monitor.
mysql> quit
Bye

注: -h:=host,指定IP;-P:=port,指定端口。

本地鏈接:使用socket鏈接

[root@xuexi-001 ~]# mysql -uroot -p123456 -S/tmp/mysql.sock
Welcome to the MySQL monitor.
mysql> quit
Bye

注: -S:=socket,指定socket。此方法只適用於本地鏈接,等同於「mysql -uroot -p123456」。

顯示全部數據庫

[root@1 ~]# mysql -uroot -p'123456' -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

注: 該方法使用於shell腳本中。

13.3 MySQL經常使用命令

查看庫信息:

  • 查詢庫 show databases;
  • 切換庫 use mysql;
  • 查看庫裏的表 show tables;
  • 查看錶裏的字段 desc tb_name;
  • 查看建表語句 show create table tb_name\G;
  • 查看當前用戶 select user();
  • 查看當前使用的數據庫 select databsase();
  • 建立庫 create database db1;
  • 建立表 use db1; create table t1(id int(4), name char(40));
  • 查看當前數據庫版本 select version();
  • 查看數據庫狀態 show status;
  • 查看各參數 show variables; show variables like 'max_connect%';
  • 修改參數 set global max_connect_errors=1000;
  • 查看隊列 show processlist; show full processlist;

注: 以上命令均須要在mysql下執行;在mysql中每行命令末尾加上分號,表示該行命令執行結束。 tb_name即table name()表名。

示例:

[root@xuexi-001 mysql~]# mysql -uroot -p'123456'
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| time_zone                 |
| time_zone_leap_second     |
+---------------------------+
28 rows in set (0.00 sec)

mysql> desc time_zone;
+------------------+------------------+------+-----+---------+----------------+
| Field            | Type             | Null | Key | Default | Extra          |
+------------------+------------------+------+-----+---------+----------------+
| Time_zone_id     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| Use_leap_seconds | enum('Y','N')    | NO   |     | N       |                |
+------------------+------------------+------+-----+---------+----------------+
2 rows in set (0.11 sec)

mysql> show create table time_zone\G;
#G=grep篩選文字內容,規律顯示出來
*************************** 1. row ***************************
       Table: time_zone
Create Table: CREATE TABLE `time_zone` (
  `Time_zone_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Use_leap_seconds` enum('Y','N') NOT NULL DEFAULT 'N',
  PRIMARY KEY (`Time_zone_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Time zones'
1 row in set (0.03 sec)

ERROR: 
No query specified

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

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

mysql> select * from user\G;
建立庫:
mysql> create database db1;
Query OK, 1 row affected (0.02 sec)

建立表:
mysql> use db1;  
#先切換到指定庫下
Database changed
mysql> create table t1(`id` int(4),`name` char(40));
#括號中是定義字段及字段格式,使用反引號引發來
Query OK, 0 rows affected (1.51 sec)

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35    |
+-----------+
1 row in set (0.06 sec)

mysql> show status;
+-----------------------------------------------+-------------+
| Variable_name                                 | Value       |
+-----------------------------------------------+-------------+
| Aborted_clients                               | 0           |
| Aborted_connects                              | 0           |
+-----------------------------------------------+-------------+

mysql> show variables\G;

mysql> show variables like 'max_connect%'\G;
#like表示匹配;%是通配符

更改參數:
mysql> set global max_connect_errors=110;
Query OK, 0 rows affected (0.04 sec)
#在此只是臨時更改,若是要永久更改,須要編輯配置文件

查看隊列:
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
|  5 | root | localhost | db1  | Query   |    0 | init  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.01 sec)

mysql> drop table t1;
Query OK, 0 rows affected (0.32 sec)

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

擴展:MySQL5.7之更改root密碼

與MySQL 5.6版本不一樣,在安裝MySQL 5.7過程當中(初始化)會自動生成root用戶密碼(隨機),那麼在安裝完成後如何更改root用戶密碼?步驟以下:

查看默認密碼:

[root@xuexi-001 mysql~]# cat /root/.mysql_secret
# The random password set for the root userat Fri Jan 10 20:00:34 2014 (local time): 3A)2DdJLkcFP

更改root密碼:已知默認密碼

使用默認密碼登陸:
[root@xuexi-001 mysql~]# /usr/local/mysql/bin/mysql -uroot -p'3A)2DdJLkcFP'
Welcome to the MySQL monitor.  
Your MySQL connection id is 3
Server version: 5.7.17

設置新密碼:
方法1:
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
方法2:
mysql> SET PASSWORD FOR 'root'@localhost = PASSWORD('123456');
mysql> quit
Bye

更改root密碼:不知道默認密碼

編輯配置文件:
[root@xuexi-001 mysql~]# vi /etc/my.cnf

[mysqld]
skip-grant-tables
datadir=/data/mysql
socket=/tmp/mysql.sock
#增長參數:skip-grant-tables

重啓:  
[root@xuexi-001 mysql~]# /etc/init.d/mysqld restart

登陸:此時不須要密碼
[root@1 mysql]# /usr/local/mysql/bin/mysql -uroot 

更改密碼:
mysql> update user set authentication_string=password('12456') where user='root';
mysql>quit

[root@xuexi-001 mysql~]# vi /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock

重啓: 
[root@xuexi-001 mysql~]# /etc/init.d/mysqld restart

13.4 MySQL用戶管理

建立用戶並受權

指定登陸IP

[root@xuexi-001 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.
mysql> grant all on *.* to 'user1'@'127.0.0.1' identified by '123456';
#建立user1用戶並授予其全部權限「*.*」(通配符)
#第一個*表示db_name;第二個*表示tb_name
#同時指定其來源IP127.0.0.1(即,只可經過此IP登陸)
#此處能夠使用通配符%,表明全部IP(通常不使用)
#設定密碼:identified by
mysql> quit
Bye

指定登陸socket

[root@xuexi-001 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.
mysql> grant all on *.* to 'user2'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye

用戶登陸

使用IP登陸

[root@xuexi-001 ~]# mysql -uuser1 -p123456 -h127.0.0.1
Welcome to the MySQL monitor.
mysql> quit
Bye

使用socket登陸

[root@xuexi-001 ~]# mysql -uuser2 -p'123456'
Welcome to the MySQL monitor. 
mysql> exit
Bye

說明: 由於指定登陸主機爲localhost,因此該用戶默認使用(監聽)本地mysql.socket文件,不須要指定IP便可登陸。

對具體權限進行受權

  • grant all on . to 'user1' identified by 'passwd';
  • grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.133.1' identified by 'passwd';
  • grant all on db1.* to 'user3'@'%' identified by 'passwd';
  • show grants;
  • show grants for user2@192.168.133.1;
[root@xuexi-001 ~]# mysql -uroot -p'123456'
Welcome to the MySQL monitor.
mysql> create database db1;
Query OK, 1 row affected (0.04 sec)
mysql> grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.8.132' identified by '123456';
#建立user2用戶,並授予其針對db1庫SELECT,UPDATE,INSERT權限

mysql> grant all on db1.* to 'user'@'%' identified by '123456';
#建立user3,並針對全部IP授予其db1庫全部權限

權限相關命令

[root@xuexi-001 ~]# mysql -uroot -p'123456'
Welcome to the MySQL monitor.
mysql> show grants;
#查看當前用戶的權限

mysql> show grants for user2@192.168.5.130;
#查看指定用戶的權限

更改權限

[root@xuexi-001 ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.6.36 MySQL Community Server (GPL)

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> show grants for user2@192.168.5.130;
+------------------------------------------------------------------------------------------------------------------+
| Grants for user2@192.168.5.130                                                                                   |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'user2'@'192.168.5.130' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'192.168.5.130'                                               |
+------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> GRANT USAGE ON *.* TO 'user2'@'192.168.5.131' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' ;
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'192.168.5.131';
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for user2@'192.168.5.131';
+--------------------------------------------------------------------+
| Grants for user2@192.168.5.131                                     |
+--------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'user2'@'192.168.5.131'                      |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'192.168.5.131' |
+--------------------------------------------------------------------+
2 rows in set (0.00 sec)

只知道用戶名不知道用戶密碼時,能夠查看以前用戶的grant ,而後複製

GRANT USAGE ON *.* TO 'user2'@'192.168.5.131' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' ;

GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'192.168.5.131';

這兩行,而後更改一下IP 就能夠了。

注: 更改用戶權限時,權限行的內容都要更改。

13.5 經常使用sql語句

  • select count(*) from mysql.user; #查看指定庫的內容的行數
  • select * from mysql.db; #查看庫的全部內容
  • select db from mysql.db;
  • select db,user from mysql.db;
  • select * from mysql.db where host like '192.168.%'; #查看某些IP對應的庫內容,like表示匹配
  • insert into db1.t1 values (1, 'abc');#向表中插入內容
  • update db1.t1 set name='aaa' where id=1;#更改表中指定內容
  • truncate table db1.t1;
  • drop table db1.t1;
  • drop database db1;
[root@xuexi-001 ~]# mysql -uroot -p'123456';
Welcome to the MySQL monitor.
mysql> use db1;
Database changed
#選擇庫

mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
|       12 |
+----------+
1 row in set (0.04 sec)
#查看指定庫的內容的行數

mysql> select * from mysql.db\G;
#查看庫的全部內容

mysql> select db,user from mysql.db;
#查看庫裏面的字段

mysql> select * from mysql.db where host like '192.168.%'\G;
#查看某些IP對應的庫內容,like表示匹配

mysql> create table t1(`id` int(4),`name` char(40));
Query OK, 0 rows affected (0.39 sec)
#在db1庫下建立表t1

mysql> select * from db1.t1;
Empty set (0.03 sec)
#查看錶中信息:空表

mysql> insert into db1.t1 values(1,'abc');
Query OK, 1 row affected (0.09 sec)
#向表中插入內容
mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | abc  |
+------+------+
1 row in set (0.00 sec)

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

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | aaa  |
+------+------+
1 row in set (0.00 sec)
#更改表中指定內容

mysql> delete from db1.t1 where id=1;
Query OK, 2 rows affected (0.10 sec)
#刪除表中指定內容
mysql> select * from db1.t1;
Empty set (0.00 sec)

mysql> truncate db1.t1;
Query OK, 0 rows affected (0.09 sec)
#清空一個表中內容

mysql> drop table t1;
Query OK, 0 rows affected (0.04 sec)
#刪除表
mysql> drop database db1;
Query OK, 0 rows affected (0.13 sec)
#刪除庫

mysql> use mysql;
mysql> delete from user where User='user1' and Host='127.0.0.1';
Query OK, 1 row affected (0.06 sec)
#刪除用戶,在刪除用戶前須要先指定表

13.6 MySQL數據庫備份恢復

備份庫

備份指定庫:
[root@xuexi-001 ~]# mysqldump -uroot -p123456 mysql > /tmp/mysqlbak.sql

備份全部庫:
[root@xuexi-001 ~]# mysqldump -uroot -p123456 -A > /tmp/mysql_all.sql

恢復庫

[root@xuexi-001 ~]# mysql -uroot -p123456 < /tmp/mysqlbak.sql

備份表

備份指定表:
[root@xuexi-001 ~]# mysql -uroot -p123456 mysql user > /tmp/user.sql

只備份表結構:
[root@xuexi-001 ~]# mysqldump -uroot -p123456 -d mysql > /tmp/mysql_tb.sql

恢復表

[root@xuexi-001 ~]# mysql -uroot -p123456 mysql user < /tmp/user.sql
相關文章
相關標籤/搜索