二十四節課預習
13.1 設置更改root密碼
13.2 連接mysql
13.3 mysql常用命令
13.4 mysql用戶管理
13.5 常用sql語句
13.6 mysql數據庫備份恢復
使用xtrabackup備份innodb引擎的數據庫 innobackupex 備份 Xtrabackup 增量備份 http://zhangguangzhi.top/2017/08/23/innobackex%E5%B7%A5%E5%85%B7%E5%A4%87%E4%BB%BDmysql%E6%95%B0%E6%8D%AE/#%E4%B8%89%E3%80%81%E5%BC%80%E5%A7%8B%E6%81%A2%E5%A4%8Dmysql
相關視頻
鏈接:http://pan.baidu.com/s/1miFpS9M 密碼:86dx
鏈接:http://pan.baidu.com/s/1o7GXBBW 密碼:ue2f
擴展
mysql5.7 root密碼更改 http://www.apelearn.com/bbs/thread-7289-1-1.html
myisam 和innodb引擎對比 http://www.pureweber.com/article/myisam-vs-innodb/
mysql 配置詳解: http://blog.linuxeye.com/379.html
mysql調優: http://www.aminglinux.com/bbs/thread-5758-1-1.html
同學分享的親身mysql調優經歷: http://www.apelearn.com/bbs/thread-11281-1-1.html
SQL語句教程 http://www.runoob.com/sql/sql-tutorial.html
什麽是事務?事務的特性有哪些? http://blog.csdn.net/yenange/article/details/7556094
根據binlog恢復指定時間段的數據 https://blog.csdn.net/lilongsy/article/details/74726002
mysql字符集調整 http://xjsunjie.blog.51cto.com/999372/1355013
root 是MySQL的超級管理員用戶 和Linux系統的root管理員類似
同時也要區分這個root和系統的用戶不是一個用戶,當然也可以創建普通用戶連接MySQL
默認root 的MySQL用戶密碼是空的,所以我們為了安全需要設置一個密碼
先看MySQL有沒有啟動 ,如果沒啟動先啟動起來
[root@linux-001 src]# ps aux |grep mysql
[root@linux-001 mysql]# /etc/init.d/mysqld start
Starting MySQL SUCCESS!
開始啟動時候我報了個錯
Starting MySQL. ERROR! The server quit without updating PID file (/data/mysql/linux-001.pid).
我就看配置文件和啟動文件最終
[root@linux-001 mysql]# vi /etc/init.d/mysqld
mysqld_pid_file_path=/data/mysql/
定義後就可以正常啟動了
[root@linux-001 mysql]# ls /usr/local/mysql/bin/mysql
/usr/local/mysql/bin/mysql
[root@linux-001 mysql]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
要想讓他加到環境變量
export PATH=/usr/local/mysql/bin:$PATH
[root@mysql mysql]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
給root用戶設密碼
[root@mysql mysql]# mysqladmin -uroot password ‘123456‘
Warning: Using a password on the command line interface can be insecure.
重啟
[root@mysql mysql]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL... SUCCESS!
[root@mysql mysql]# mysql -uroot 不加密碼阻止進入了
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)
[root@mysql mysql]# mysql -uroot -p 加-p輸入密碼
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
有的時候需要更改已知的mysql密碼
[root@mysql mysql]# mysqladmin -uroot -p‘123456‘ password ‘Aa123456‘
Warning: Using a password on the command line interface can be insecure.
[root@mysql mysql]# mysql -uroot -p 要輸入新的密碼Aa123456
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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的使用過程中也可能會碰到mysql密碼被遺忘的情況
[root@mysql mysql]# vim /etc/my.cnf
[mysqld]
skip-grant-tables
[root@mysql mysql]# /etc/init.d/mysqld restart 重啟
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@mysql mysql]# mysql -uroot 不用密碼也可以登錄
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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的用戶密碼存在mysql數據庫的user表中
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 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> desc user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Password | char(41) | NO | | | |
| Select_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Insert_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Update_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Delete_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Drop_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Reload_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Shutdown_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Process_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| File_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Grant_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| References_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Index_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Alter_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Show_db_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Super_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_tmp_table_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Lock_tables_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Execute_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Repl_slave_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Repl_client_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_view_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Show_view_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_routine_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Alter_routine_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_user_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Event_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Trigger_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_tablespace_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| ssl_type | enum(‘‘,‘ANY‘,‘X509‘,‘SPECIFIED‘) | NO | | | |
| ssl_cipher | blob | NO | | NULL | |
| x509_issuer | blob | NO | | NULL | |
| x509_subject | blob | NO | | NULL | |
| max_questions | int(11) unsigned | NO | | 0 | |
| max_updates | int(11) unsigned | NO | | 0 | |
| max_connections | int(11) unsigned | NO | | 0 | |
| max_user_connections | int(11) unsigned | NO | | 0 | |
| plugin | char(64) | YES | | mysql_native_password | |
| authentication_string | text | YES | | NULL | |
| password_expired | enum(‘N‘,‘Y‘) | NO | | N | |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
43 rows in set (0.00 sec)
mysql> select user,password from user where user=‘root‘;
+------+-------------------------------------------+
| user | password |
+------+-------------------------------------------+
| root | *4A488726AE5A0B0F0DB967998EE12D87F25C9610 |
| root | |
| root | |
| root | |
+------+-------------------------------------------+
4 rows in set (0.01 sec)
mysql> update user set password=password(‘123456‘) where user=‘root‘; //更新密碼為123456
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
//刪除my.conf中的skip-grant
[root@mysql mysql]# vim /etc/my.cnf
[root@mysql mysql]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@mysql mysql]# mysql -uroot
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)
[root@mysql mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
mysql -uroot -p123456
mysql -uroot -p123456 -h127.0.0.1 -P3306
mysql -uroot -p123456 -S/tmp/mysql.sock
mysql -uroot -p123456 -e “show databases”
mysql主要有4種連接方式
1.mysql -uroot -p123456
[root@mysql 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 7
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> quit
Bye
2.mysql -uroot -p123456 -h127.0.0.1 -P3306
//ip和端口連接方式
[root@mysql mysql]# netstat -ntlup | grep :3306
tcp6 0 0 :::3306 :::* LISTEN 13870/mysqld
[root@mysql mysql]# mysql -uroot -p‘123456‘ -h 127.0.0.1 -P 3306
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 8
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> quit
Bye
3.mysql -uroot -p123456 -S/tmp/mysql.sock
//socket方式,其實與第一種是一樣的 [root@mysql mysql]# ps aux | grep mysql | grep socket
mysql 13870 0.1 44.6 1302936 450260 pts/1 Sl 14:36 0:08 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=mysql.err --pid-file=/data/mysql/mysql.pid --socket=/tmp/mysql.sock
[root@mysql mysql]# mysql -uroot -p‘123456‘ -S ‘/tmp/mysql.sock‘
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 9
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> quit
Bye
4.mysql -uroot -p123456 -e sql_command
//這種方式可以直接在命令行上執行sql查詢,可以用在bash腳本的編寫中
[root@mysql mysql]# mysql -uroot -p‘123456‘ -e ‘select user,password from mysql.user‘
Warning: Using a password on the command line interface can be insecure.
+------+-------------------------------------------+
| user | password |
+------+-------------------------------------------+
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| | |
| | |
+------+-------------------------------------------+
查詢庫 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;
[root@mysql 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 11
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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.01 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 |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
28 rows in set (0.00 sec)
查看表的結構
mysql> desc user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Password | char(41) | NO | | | |
| Select_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Insert_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Update_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Delete_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Drop_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Reload_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Shutdown_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Process_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| File_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Grant_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| References_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Index_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Alter_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Show_db_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Super_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_tmp_table_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Lock_tables_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Execute_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Repl_slave_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Repl_client_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_view_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Show_view_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_routine_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Alter_routine_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_user_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Event_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Trigger_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| Create_tablespace_priv | enum(‘N‘,‘Y‘) | NO | | N | |
| ssl_type | enum(‘‘,‘ANY‘,‘X509‘,‘SPECIFIED‘) | NO | | | |
| ssl_cipher | blob | NO | | NULL | |
| x509_issuer | blob | NO | | NULL | |
| x509_subject | blob | NO | | NULL | |
| max_questions | int(11) unsigned | NO | | 0 | |
| max_updates | int(11) unsigned | NO | | 0 | |
| max_connections | int(11) unsigned | NO | | 0 | |
| max_user_connections | int(11) unsigned | NO | | 0 | |
| plugin | char(64) | YES | | mysql_native_password | |
| authentication_string | text | YES | | NULL | |
| password_expired | enum(‘N‘,‘Y‘) | NO | | N | |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
43 rows in set (0.01 sec)
創建數據庫
mysql> create database dbtest;
Query OK, 1 row affected (0.00 sec)
創建在數據庫中創建表
//註意是反引號 一定記得是反引號
mysql> create table tb1(`id` int(4),`name` char(40));
Query OK, 0 rows affected (0.03 sec)
查看建表語句
// \G代表橫向顯示 //如需建表時設定字符集,可以在create table語句後加上‘ENGINE=InnoDB DEFAULT CHARSET=utf8’,utf-8替換為相應的字符集
mysql> show create table tb1\G
*************************** 1. row ***************************
Table: tb1
Create Table: CREATE TABLE `tb1` (
`id` int(4) DEFAULT NULL,
`name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
查看當前用戶
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
查看當前數據庫
Database changed mysql> select database(); +------------+ | database() | +------------+ | mysql | +------------+ 1 row in set (0.01 sec)
查看數據庫版本
mysql> select version(); +-----------+ | version() | +-----------+ | 5.6.36 | +-----------+ 1 row in set (0.00 sec)
查看數據庫狀態
mysql> show status;
查看數據庫參數
mysql> show variables\G
查看具體的數據庫參數
//%號和like結合使用,%是通配符
mysql> show variables like ‘max_connect%‘;
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 100 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.00 sec)
臨時設定參數
mysql> set global max_connect_errors=1000; Query OK, 0 rows affected (0.02 sec) mysql> show variables like ‘max_connect%‘; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | max_connect_errors | 1000 | | max_connections | 151 | +--------------------+-------+ 2 rows in set (0.00 sec)
//退出後重啟mysql服務,參數失效 mysql> quit Bye
[root@mysql mysql]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
[root@mysql 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.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql> show variables like ‘max_connect%‘;
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 100 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.00 sec)
永久修改參數值
root@mysql mysql]# vim /etc/my.cnf
//在[mysqld]下添加相應的參數 max_connect_errors=1000
[root@mysql mysql]# /etc/init.d/mysqld restart
[root@mysql mysql]# mysql -uroot -p‘123456‘
mysql> show variables like ‘max_connect%‘;
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 1000 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.00 sec)
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 [email protected];
mysql安裝完以後默認只有一個root管理用戶,最高權限。不利於安全管理。需要根據需要創建不同用戶。
//創建用戶並授予全部權限
[root@mysql mysql]# mysql -uroot -p‘123456‘
mysql> grant all on *.* to ‘user01‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.00 sec)
[root@mysql mysql]# mysql -uuser01 -p
Enter password:
ERROR 1045 (28000): Access denied for user ‘user01‘@‘localhost‘ (using password: YES)
//系統中存在空帳戶導致,刪除多余空帳戶
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> select User,Password from user;
+--------+-------------------------------------------+
| User | Password |
+--------+-------------------------------------------+
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| | |
| | |
| user1 | *59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0 |
| user2 | *59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0 |
| user3 | *59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0 |
| user01 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+--------+-------------------------------------------+
10 rows in set (0.00 sec)
mysql> delete from user where user=‘‘;
Query OK, 2 rows affected (0.00 sec)
//該句一定要執行 mysql> FLUSH PRIVILEGES; mysql> exit
[root@mysql mysql]# mysql -u user01 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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.
//授予部分權限 //授予user08用戶mysql數據庫,user表查詢,更新和插入的權限
[root@mysql 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 16
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> grant SELECT,UPDATE,INSERT on mysql.user to ‘user08‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.01 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 database;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘database‘ at line 1
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dbtest |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
//因為對user表有權限,其他表都看不到
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| tb1 |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
29 rows in set (0.00 sec)
//限制可以從哪個ip登錄訪問 //@後的部分可以指定可以訪問的來源,%表示任意地址 //註意@前後的引號
mysql> grant all on *.* to ‘user09‘@‘127.0.0.1‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@mysql mysql]# mysql -uuser09 -p -h127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> quit
Bye
[root@mysql mysql]# mysql -uuser09 -p -hlocalhost
Enter password:
ERROR 1045 (28000): Access denied for user ‘user09‘@‘localhost‘ (using password: YES)
[root@mysql mysql]# mysql -uuser09 -h192.168.76.135
ERROR 1045 (28000): Access denied for user ‘user09‘@‘192.168.76.135‘ (using password: NO)
[root@mysql mysql]# mysql -uroot -p‘123456‘
mysql> show grants for [email protected];
+------------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected] |
+------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO ‘user09‘@‘127.0.0.1‘ IDENTIFIED BY PASSWORD ‘*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9‘ |
+------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO ‘user09‘@‘%‘ IDENTIFIED BY PASSWORD ‘*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9‘;
Query OK, 0 rows affected (0.00 sec)
mysql>
[root@mysql mysql]# mysql -uuser09 -p123456 -h127.0.0.1
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 22
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
[root@mysql mysql]# mysql -uuser09 -p123456 -hlocalhost;
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 23
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
[root@mysql mysql]# mysql -uuser09 -p123456 -h192.168.76.135
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 24
Server version: 5.6.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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;
+----------------------------------------------------------------------------------------------------------------+
| Grants for user09@% |
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.%‘;
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;
二十四節課預習