[toc]html
擴展 SQL語句教程 http://www.runoob.com/sql/sql-tutorial.html 什麼是事務?事務的特性有哪些? http://blog.csdn.net/yenange/article/details/7556094 根據binlog恢復指定時間段的數據 http://www.centoscn.com/mysql/2015/0204/4630.html mysql字符集調整 http://xjsunjie.blog.51cto.com/999372/1355013mysql
使用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 相關視頻linux
連接:http://pan.baidu.com/s/1miFpS9M 密碼:86dxsql
首先啓動mysql,而後進入數據庫
[root@xavi ~]# /etc/init.d/mysqld start Starting MySQL... SUCCESS! [root@xavi ~]# mysql -uroot -pxavilinux 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 1 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>
all表示全部的權限(如讀、寫、查詢、刪除等操做);建立user用戶並授予其全部權限*.(第一個表示全部數據庫,第二個*表示全部表) 這裏的user1特指localhost上的user1,用戶和主機的IP之間有一個@符號 identified by :設定密碼,用單引號括起來。centos
mysql> grant all on *.* to 'user1'@'127.0.0.1' identified by '123456'; Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to 'user2'@'%' identified by '123456a'; Query OK, 0 rows affected (0.00 sec)
mysql> quit Bye [root@xavi ~]# mysql -uuser1 -p123456 Warning: Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'user1'@'localhost' (using password: YES)
[root@xavi ~]# mysql -uuser1 -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 3 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>
mysql> grant all on *.* to 'user1'@'localhost' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye [root@xavi ~]# mysql -uuser1 -p123456 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>
mysql> show grants //查看當前用戶受權 -> ; +-----------------------------------------------------------------------------------------------------------------------+ | Grants for user1@localhost | +-----------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'user1'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' | +-----------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> show grants for user1@'127.0.0.1'; +-----------------------------------------------------------------------------------------------------------------------+ | Grants for user1@127.0.0.1 | +-----------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'user1'@'127.0.0.1' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' | +-----------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
這一部分開始操做是一隻報錯app
mysql> grant all on db2.* to 'user2'@'192.168.188.129' identified by '111222'; ERROR 1044 (42000): Access denied for user 'user1'@'localhost' to database 'db2' mysql> grant all on db1.* to 'user3'@'%' identified by 'passwd'; ERROR 1044 (42000): Access denied for user 'user1'@'localhost' to database 'db1' mysql> grant all on db1.* to 'user3'@'%' identified by 'passwd'; ERROR 1044 (42000): Access denied for user 'user1'@'localhost' to database 'db1' mysql> revoke all on *.* from user1@localhost; ERROR 1045 (28000): Access denied for user 'user1'@'localhost' (using password: YES) mysql> revoke all on *.* from 'user1'@'localhost' identified by '123456'; 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 ''123456'' at line 1 mysql> revoke all on *.* from 'user1'@'localhost' identified by '123456'; 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 ''123456'' at line 1 mysql> show grants for user1@'127.0.0.1';
mysql> quit Bye [root@xavi ~]# mysql -uroot -pxavilinux 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 2 Server version: 5.6.35 MySQL Community Server (GPL)
mysql> grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.133.1' identified by 'passwd'; Query OK, 0 rows affected (0.00 sec)
mysql> show grants for user2@'192.168.133.1'; +------------------------------------------------------------------------------------------------------------------+ | Grants for user2@192.168.133.1 | +------------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'user2'@'192.168.133.1' IDENTIFIED BY PASSWORD '*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0' | | GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'192.168.133.1' | +------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec) mysql> GRANT USAGE ON *.* TO 'user2'@'192.168.133.2' IDENTIFIED BY PASSWORD '*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'192.168.133.2'; Query OK, 0 rows affected (0.00 sec)
mysql> show grants for user2@'192.168.133.2'; +------------------------------------------------------------------------------------------------------------------+ | Grants for user2@192.168.133.2 | +------------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'user2'@'192.168.133.2' IDENTIFIED BY PASSWORD '*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0' | | GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'192.168.133.2' | +------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec)
mysql> select count(*) from mysql.user; +----------+ | count(*) | +----------+ | 10 | +----------+ 1 row in set (0.00 sec)
mysql> select * from mysql.db\G; *************************** 1. row *************************** Host: % Db: test User: Select_priv: Y Insert_priv: Y Update_priv: Y Delete_priv: Y Create_priv: Y Drop_priv: Y Grant_priv: N References_priv: Y Index_priv: Y
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 create table user\G; *************************** 1. row *************************** Table: user Create Table: CREATE TABLE `user` ( `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '', `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '', `Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '', `Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', `Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N', . . . PRIMARY KEY (`Host`,`User`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Users and global privileges' 1 row in set (0.01 sec)
mysql> use db1; 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 create table t1; +-------+---------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------------------------------------+ | t1 | CREATE TABLE `t1` ( `id` int(4) DEFAULT NULL, `name` char(40) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +-------+---------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> select db from mysql.db; +---------+ | db | +---------+ | test | | test\_% | | db1 | | db1 | +---------+ 4 rows in set (0.01 sec) mysql> select db,user from mysql.db; +---------+-------+ | db | user | +---------+-------+ | test | | | test\_% | | | db1 | user2 | | db1 | user2 | +---------+-------+ 4 rows in set (0.00 sec)
mysql> select * from mysql.db where host like '192.168.%';
mysql> mysql> select * from mysql.db where host like '192.168.%'\G; *************************** 1. row *************************** Host: 192.168.133.1 Db: db1 User: user2 Select_priv: Y Insert_priv: Y Update_priv: Y Delete_priv: N Create_priv: N Drop_priv: N Grant_priv: N References_priv: N Index_priv: N Alter_priv: N Create_tmp_table_priv: N Lock_tables_priv: N Create_view_priv: N Show_view_priv: N Create_routine_priv: N Alter_routine_priv: N Execute_priv: N Event_priv: N Trigger_priv: N *************************** 2. row *************************** Host: 192.168.133.2 Db: db1 User: user2
mysql> desc db1.t1; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | name | char(40) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.01 sec) mysql> select * from db1.t1; Empty set (0.01 sec) 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)
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)
mysql> truncate table db1.t1; Query OK, 0 rows affected (0.04 sec) mysql> select * from db1.t1; Empty set (0.00 sec)
mysql> drop table db1.t1; Query OK, 0 rows affected (0.03 sec) mysql> select * from db1.t1; ERROR 1146 (42S02): Table 'db1.t1' doesn't exist
mysql> drop database db1; Query OK, 0 rows affected (0.00 sec) mysql> select * from db1; ERROR 1046 (3D000): No database selected
備份和恢復MySQL數據庫是很是重要的,要牢固掌握less
mysql> quit Bye [root@xavi ~]# mysqldump -uroot -pxavilinux mysql > /tmp/mysqlbak.sql; Warning: Using a password on the command line interface can be insecure.
[root@xavi ~]# mysql -uroot -pxavilinux -e "create database mysql2" Warning: Using a password on the command line interface can be insecure. [root@xavi ~]# mysql -uroot -pxavilinux mysql2 < /tmp/mysqlbak.sql Warning: Using a password on the command line interface can be insecure.
[root@xavi ~]# mysql -uroot -pxavilinux mysql2 Warning: Using a password on the command line interface can be insecure. Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.6.35 MySQL Community Server (GPL) mysql> select database(); +------------+ | database() | +------------+ | mysql2 | +------------+ 1 row in set (0.00 sec)
mysql> quit Bye [root@xavi ~]# mysqldump -uroot -pxavilinux mysql user > /tmp/user.sql Warning: Using a password on the command line interface can be insecure. [root@xavi ~]# less /tmp/user.sql
[root@xavi ~]# less /tmp/user.sql mysql Ver 14.14 Distrib 5.6.35, for linux-glibc2.5 (x86_64) using EditLine wrapper 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. Usage: mysql [OPTIONS] [database] -?, --help Display this help and exit. -I, --help Synonym for -? --auto-rehash Enable automatic rehashing. One doesn't need to use 'rehash' to get table and field completion, but startup and reconnecting may take a longer time. Disable with --disable-auto-rehash. (Defaults to on; use --skip-auto-rehash to disable.) -A, --no-auto-rehash No automatic rehashing. One has to use 'rehash' to get table and field completion. This gives a quicker start of mysql and disables rehashing on reconnect.
[root@xavi ~]# mysql -uroot -pxavilinux mysql2 < /tmp/user.sql Warning: Using a password on the command line interface can be insecure.
[root@xavi ~]# mysqldump -uroot -pxavilinux mysql2 >/tmp/user.sql Warning: Using a password on the command line interface can be insecure. [root@xavi ~]# less /tmp/mysql_all.sql
[root@xavi ~]# mysqldump -uroot -pxavilinux -d mysql2 > /tmp/mysql2.sql Warning: Using a password on the command line interface can be insecure. [root@xavi ~]# less /tmp/mysql2.sql [root@xavi ~]# less /tmp/mysql2.sql -- MySQL dump 10.13 Distrib 5.6.35, for linux-glibc2.5 (x86_64) -- -- Host: localhost Database: mysql2 -- ------------------------------------------------------ -- Server version 5.6.35 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `columns_priv` -- DROP TABLE IF EXISTS `columns_priv`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `columns_priv` ( `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '', `Db` char(64) COLLATE utf8_bin NOT NULL DEFAULT '', `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '', `Table_name` char(64) COLLATE utf8_bin NOT NULL DEFAULT '', `Column_name` char(64) COLLATE utf8_bin NOT NULL DEFAULT '', `Timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `Column_priv` set('Select','Insert','Update','References') CHARACTER SET utf8 NOT NULL DEFAULT '', PRIMARY KEY (`Host`,`Db`,`User`,`Table_name`,`Column_name`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Column privileges'; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Table structure for table `db` -- DROP TABLE IF EXISTS `db`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `db` (