1. 程式人生 > >設定更改root密碼、連線mysql、mysql常用命令、mysql使用者管理、常用sql語句、mysql資料庫備份恢復

設定更改root密碼、連線mysql、mysql常用命令、mysql使用者管理、常用sql語句、mysql資料庫備份恢復

一、設定更改root密碼

首次直接使用mysql會提示‘該命令不存在’,原因是還沒有將該命令加入環境變數,如果要使用該命令,需要使用其絕對路徑:/usr/local/mysql/bin/mysql,為了方便,先將其加入系統環境變數:

[[email protected] ~]# exprt PATH=$PATH:/usr/local/mysql/bin/

mysql命令路徑暫時加入環境變數,系統重啟後該變數會失效,若要永久生效,需要將其加入環境變數配置檔案

[[email protected] ~]# vim /etc/profile
……
export PATH=$PATH:/usr/local/mysql/bin/
 
重新整理配置檔案(否則不生效):
[
[email protected]
~]# source /etc/profile 設定 & 更改密碼

首次登陸mysql,root使用者沒有密碼,直接登入

[[email protected] ~]# mysql -uroot
#-u:=user,指定使用者名稱
Welcome to the MySQL monitor.  Commands end with ; or \g.
……
mysql> quit

登入mysql之後可以進行與mysql相關的一些操作,但是設定mysql使用者的密碼需要執行以下操作
設定密碼

[[email protected] ~]# mysqladmin -uroot password '123456'  
 
再次登入:
[
[email protected]
~]# mysql -uroot ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

設定密碼後直接登入會報錯(ERROR),需要輸入密碼登入

[[email protected] ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.
mysql>

-p=passwd,使用密碼登入,在此可以將密碼直接輸入在命令列(跟在-p後面,不加空格:-p’123456’<此處單引號可以不加,但是當密碼中有特殊符號時必須加,所以在命令列輸入密碼時養成習慣:加單引號>),也可以不在命令列輸入,只跟-p選項,然後根據提示資訊:“Enter password”,輸入密碼進行登入(此方法不會暴露使用者密碼,安全)

更改密碼
[[email protected] ~]# mysqladmin -uroot -p'123456' password '1234567'
 
[[email protected] ~]# mysql -uroot -p'1234567'
Welcome to the MySQL monitor.
mysql>

忘記密碼,更改密碼

先編輯mysql配置檔案:
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
skip-grant
#忽略授權!
datadir=/data/mysql
socket=/tmp/mysql.sock
 
重啟mysql服務:
[[email protected] ~]# /etc/init.d/mysqld restart
Shutting down MySQL... SUCCESS! 

完成該操作之後就可以任意登入mysql了(無需密碼),所以此時mysql安全性很差,平時配置檔案中一定不要新增該引數

[[email protected] ~]# 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

更改成功

恢復配置檔案:
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
 
重啟mysql服務:
[[email protected] ~]# /etc/init.d/mysqld restart 
Shutting down MySQL.. SUCCESS! 
Starting MySQL........... SUCCESS! 
 
登入:
[[email protected] ~]# mysql -uroot -p'123456'
Welcome to the MySQL monitor.
mysql> quit

二、連線mysql

遠端連線:
使用IP/port連線

[[email protected] ~]# mysql -uroot -p123456 -h127.0.0.1 -P3306
Welcome to the MySQL monitor.
mysql> quit

-h:=host,指定IP;-P:=port,指定埠

本地連線:
使用socket連線

[[email protected] ~]# mysql -uroot -p123456 -S/tmp/mysql.sock
Welcome to the MySQL monitor.
mysql> quit

-S:=socket,指定socket。此方法只適用於本地連線,等同於“mysql -uroot -p123456”

顯示所有資料庫

[[email protected] ~]# 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指令碼中

三、mysql常用命令

在這裡插入圖片描述
切換庫(use mysql)之後執行
在這裡插入圖片描述
以上命令均需要在mysql下執行;在mysql中每行命令末尾加上分號,表示該行命令執行結束。 tb_name即table name()表名
如:

[[email protected] 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()         |
+----------------+
| [email protected] |
+----------------+
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使用者密碼?步驟如下:

檢視預設密碼

[[email protected] mysql]# cat /root/.mysql_secret
# The random password set for the root userat Fri Jan 10 20:00:34 2014 (local time): 3A)2DdJLkcF

已知預設密碼,更改root密碼

使用預設密碼登入:
[[email protected] 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> qui

不知道預設密碼

編輯配置檔案:
[[email protected] mysql]# vi /etc/my.cnf
 
[mysqld]
skip-grant-tables
datadir=/data/mysql
socket=/tmp/mysql.sock
#增加引數:skip-grant-tables
 
重啟:  
[[email protected] mysql]# /etc/init.d/mysqld restart
 
登入:此時不需要密碼
[[email protected] mysql]# /usr/local/mysql/bin/mysql -uroot 
 
更改密碼:
mysql> update user set authentication_string=password('12456') where user='root';
mysql>quit
 
[[email protected] mysql]# vi /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
 
重啟: 
[[email protected] mysql]# /etc/init.d/mysqld restar

四、mysql使用者管理

建立使用者並授權
指定登入IP

[[email protected] ~]# 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

[[email protected] ~]# 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登入

[[email protected] ~]# mysql -uuser1 -p123456 -h127.0.0.1
Welcome to the MySQL monitor.
mysql> quit
Bye

使用socket登入

[[email protected] ~]# mysql -uuser2 -p'123456'
Welcome to the MySQL monitor. 
mysql> exit
Bye

因為指定登入主機為localhost,所以該使用者預設使用(監聽)本地mysql.socket檔案,不需要指定IP即可登入

對具體許可權進行授權

[[email protected] ~]# 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庫所有許可權

許可權相關命令

[[email protected] ~]# mysql -uroot -p'123456'
Welcome to the MySQL monitor.
mysql> show grants;
#檢視當前使用者的許可權
mysql> show grants for [email protected];
#檢視指定使用者的許可權

更改許可權

[[email protected] ~]# mysql -uroot -p'123456'
Welcome to the MySQL monitor.
mysql> GRANT USAGE ON *.* TO 'user2'@'127.0.0.1' IDENTIFIED BY PASSWORD '*6BB4837EB743291105EE4568DDA7DC67ED2CA2AD9';
Query OK, 0 rows affected (0.03 sec)
 
mysql> GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for [email protected];
+--------------------------------------------------------------------------------------------------------------+
| Grants for [email protected]                                                                                   |
+--------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'user2'@'127.0.0.1' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'127.0.0.1'                                               |
+--------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
 
mysql> quit
Bye

更改使用者許可權時,許可權行的內容都要更改

五、常用sql語句

[[email protected] ~]# 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)
#刪除使用者,在刪除使用者前需要先指定表

# 六、MySQL資料庫備份恢復 備份庫
備份指定庫:
[[email protected] ~]# mysqldump -uroot -p123456 mysql > /tmp/mysqlbak.sql
備份所有庫:
[[email protected] ~]# mysqldump -uroot -p123456 -A > /tmp/mysql_all.sql

恢復庫

[[email protected] ~]# mysql -uroot -p123456 < /tmp/mysqlbak.sql

備份表

備份指定表:
[[email protected] ~]# mysql -uroot -p123456 mysql user > /tmp/user.sql
 
只備份表結構:
[[email protected] ~]# mysqldump -uroot -p123456 -d mysql > /tmp/mysql_tb.sql

恢復表

[[email protected] ~]# mysql -uroot -p123456 mysql user < /tmp/user.sql