十三周二次课(6月20日)
13.4 MySQL用户管理
场景,为了安全,新建的站点,创建新的用户,或者给予使用已有账户,给予权限
MySQL创建用户以及授权
mysql> grant all on *.* to 'aiker'@'127.0.0.1' identified by '12345678';
Query OK, 0 rows affected (0.01 sec)mysql> grant all on *.* to ‘aiker’ identified by ‘passwd’;grant all on *.* to ‘aiker’@’127.0.0.1’ identified by ‘passwd’;
grant 授权
all (查看,创建,删除等等)
aiker’@’127.0.0.1’ 指定用户@指定来源IP (指定用户可以写%,表示所有的IP)如果指定了来源IP,那么只能通过来源IP登录
. 所有库,所有表
命令输错,直接输入“ ; ”分号退出
使用用户aiker 登录mysql 看下
[root@localhost ~]# mysql -uaiker -p12345678
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'aiker'@'localhost' (using password: YES)
[root@localhost ~]#
不能登录,为什么不行呢,因为默认用的是socket
授权一个ip
[root@localhost ~]# mysql -uaiker -p12345678 -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.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> quit
Bye
[root@localhost ~]#
用socket去连
先登录root,再定义localhost
[root@localhost ~]# mysql -uroot -paiker12345678
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 4
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> mysql> grant all on *.* to 'aiker'@'localhost' identified by '12345678';
Query OK, 0 rows affected (0.00 sec)mysql>
退出来,打错了用 分号 ; 退出来,除了用 quit 之外还可以用exit ctrl d
mysql> uit-> quit-> ;
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 'uit
quit' at line 1
mysql> quit
Bye
[root@localhost ~]#
再来试下aiker 登录,就可以了
[root@localhost ~]# mysql -uaiker -p12345678
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.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> exit
Bye
[root@localhost ~]#
用来查看指定用户的授权是什么,show grants; (指定root)
show grants for gavin@172.16.20.222;(指定172.16.20.222)
[root@localhost ~]# mysql -uroot -paiker12345678
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 6
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;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*1836D7557E753782F1509748BD403456701A0D2F' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)mysql> mysql> show grants for aiker@'127.0.0.1';
+-----------------------------------------------------------------------------------------------------------------------+
| Grants for aiker@127.0.0.1 |
+-----------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'aiker'@'127.0.0.1' IDENTIFIED BY PASSWORD '*B012E8731FF1DF44F3D8B26837708985278C3CED' |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)mysql>
再来把gavin 做一个授权, 再show grants;
show grants;看的是root
mysql> grant SELECT,UPDATE,INSERT on db1.* to 'gavin'@'172.16.20.222' identified by 'passwd';
Query OK, 0 rows affected (0.00 sec)mysql> mysql> show grants;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*1836D7557E753782F1509748BD403456701A0D2F' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)mysql>
展示gavin的 授权
mysql> show grants for gavin@'172.16.20.222';
+------------------------------------------------------------------------------------------------------------------+
| Grants for gavin@172.16.20.222 |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'gavin'@'172.16.20.222' IDENTIFIED BY PASSWORD '*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0' |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'gavin'@'172.16.20.222' |
+------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)mysql> mysql> GRANT USAGE ON *.* TO 'gavin'@'172.16.20.220' IDENTIFIED BY PASSWORD '*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0'-> ;
Query OK, 0 rows affected (0.00 sec)mysql> GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'gavin'@'172.16.20.220';
Query OK, 0 rows affected (0.00 sec)mysql> mysql> show grants for gavin@'172.16.20.220';
+------------------------------------------------------------------------------------------------------------------+
| Grants for gavin@172.16.20.220 |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'gavin'@'172.16.20.220' IDENTIFIED BY PASSWORD '*59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0' |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'gavin'@'172.16.20.220' |
+------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)mysql>
13.5 常用sql语句
先到mysql下
[root@localhost ~]# mysql -uroot -paiker12345678
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.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> use db1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql>
查看表的行数 select count(*) from mysql.user;
mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
| 10 |
+----------+
1 row in set (0.02 sec)mysql>
查看所有的内容
mysql> select * from mysql.db;
+---------------+---------+-------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
| Host | Db | User | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Create_tmp_table_priv | Lock_tables_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Execute_priv | Event_priv | Trigger_priv |
+---------------+---------+-------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
| % | test | | Y | Y | Y | Y | Y | Y | N | Y | Y | Y | Y | Y | Y | Y | Y | N | N | Y | Y |
| % | test\_% | | Y | Y | Y | Y | Y | Y | N | Y | Y | Y | Y | Y | Y | Y | Y | N | N | Y | Y |
| 172.16.20.222 | db1 | gavin | Y | Y | Y | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N |
| 172.16.20.220 | db1 | gavin | Y | Y | Y | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N |
+---------------+---------+-------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
4 rows in set (0.00 sec)mysql>
查看db库的所有内容
查看表,用户
mysql> select db from mysql.db;
+---------+
| db |
+---------+
| test |
| test\_% |
| db1 |
| db1 |
+---------+
4 rows in set (0.00 sec)mysql> select db,user from mysql.db;
+---------+-------+
| db | user |
+---------+-------+
| test | |
| test\_% | |
| db1 | gavin |
| db1 | gavin |
+---------+-------+
4 rows in set (0.00 sec)mysql>
模糊查询。like 模糊匹配
mysql> select * from mysql.db where host like '172.16.20.%';
+---------------+-----+-------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
| Host | Db | User | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Create_tmp_table_priv | Lock_tables_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Execute_priv | Event_priv | Trigger_priv |
+---------------+-----+-------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
| 172.16.20.222 | db1 | gavin | Y | Y | Y | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N |
| 172.16.20.220 | db1 | gavin | Y | Y | Y | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N | N |
+---------------+-----+-------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+------------------+----------------+---------------------+--------------------+--------------+------------+--------------+
2 rows in set (0.00 sec)mysql> select * from mysql.db where host like '172.16.20.%'\G; // \G是按照竖行排行
*************************** 1. row ***************************Host: 172.16.20.222Db: db1User: gavinSelect_priv: YInsert_priv: YUpdate_priv: YDelete_priv: NCreate_priv: NDrop_priv: NGrant_priv: NReferences_priv: NIndex_priv: NAlter_priv: N
Create_tmp_table_priv: NLock_tables_priv: NCreate_view_priv: NShow_view_priv: NCreate_routine_priv: NAlter_routine_priv: NExecute_priv: NEvent_priv: NTrigger_priv: N
*************************** 2. row ***************************Host: 172.16.20.220Db: db1User: gavinSelect_priv: YInsert_priv: YUpdate_priv: YDelete_priv: NCreate_priv: NDrop_priv: NGrant_priv: NReferences_priv: NIndex_priv: NAlter_priv: N
Create_tmp_table_priv: NLock_tables_priv: NCreate_view_priv: NShow_view_priv: NCreate_routine_priv: NAlter_routine_priv: NExecute_priv: NEvent_priv: NTrigger_priv: N
2 rows in set (0.00 sec)ERROR:
No query specifiedmysql>
插入1, ‘abc’到db1.t1表
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.09 sec)mysql> mysql> select * from db1.ti;
ERROR 1146 (42S02): Table 'db1.ti' doesn't exist
mysql> select * from db1.t1;
Empty set (0.09 sec)mysql> insert into db1.t1 values (1, 'abc');
Query OK, 1 row affected (0.01 sec)mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 1 | abc |
+------+------+
1 row in set (0.00 sec)mysql> mysql> insert into db1.t1 values (1, '234');
Query OK, 1 row affected (0.01 sec)mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 1 | abc |
| 1 | 234 |
+------+------+
2 rows in set (0.00 sec)mysql> mysql> insert into db1.t1 values (1, '234');
Query OK, 1 row affected (0.02 sec)mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 1 | abc |
| 1 | 234 |
| 1 | 234 |
+------+------+
3 rows in set (0.00 sec)mysql>
更改db1.t1表 的字符串为name 的数据 和 字符串为id 的数据
mysql> update db1.t1 set name='aaa' where id=1;
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3 Changed: 3 Warnings: 0mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 1 | aaa |
| 1 | aaa |
| 1 | aaa |
+------+------+
3 rows in set (0.00 sec)mysql> mysql> update db1.t1 set id=2 where name='aaa';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 2 | aaa |
| 2 | aaa |
| 2 | aaa |
+------+------+
3 rows in set (0.00 sec)mysql>
删除t1表
mysql> delete from db1.t1 where id=2;
Query OK, 3 rows affected (0.00 sec)mysql> select * from db1.t1;
Empty set (0.00 sec)mysql>
重新插入一个ti表
mysql> insert into db1.t1 values (1, '234');
Query OK, 1 row affected (0.00 sec)mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 1 | 234 |
+------+------+
1 row in set (0.00 sec)mysql>
truncate db1.t1; 仅仅是清除表里的数据,清空数据
mysql> truncate db1.t1;
Query OK, 0 rows affected (0.28 sec)mysql> select * from db1.t1;
Empty set (0.00 sec)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.00 sec)mysql>
drop table t1; 连表带壳全部清除 丢掉表
mysql> drop table t1;
Query OK, 0 rows affected (0.01 sec)mysql> select * from db1.t1;
ERROR 1146 (42S02): Table 'db1.t1' doesn't exist
mysql>
drop database db1; 把数据库给干掉了
mysql> drop database db1;
Query OK, 0 rows affected (0.00 sec)mysql>
myisam引擎的库的好处是,能自动去统计行数
尽量少用 * 这样操作,如果是大表会很耗时
13.6 MySQL数据库备份恢复
[root@localhost ~]# mysqldump -uroot -paiker12345678 mysql`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,`thread_id` bigint(21) unsigned NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;Dump completed on 2018-01-30 23:00:48
[root@localhost ~]#
这些都是就是这个mysql库里的东西,备份就是把这些东西 重定向到指定的文件中去
[root@localhost ~]# mysqldump -uroot -paiker12345678 mysql > /tmp/mysqlbak.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]#
mysqlbak.sql就是我们备份的 msyql库文件
创建一个新的mysql2库
[root@localhost ~]# mysql -uroot -paiker12345678 -e "create database mysql2"
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]#
要给它恢复回去就用这个命令
[root@localhost ~]# mysql -uroot -paiker12345678 mysql2 < /tmp/mysqlbak.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -paiker12345678 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 -AWelcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
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>
这个mysql2 和mysql一样
mysql> select database();
+------------+
| database() |
+------------+
| mysql2 |
+------------+
1 row in set (0.00 sec)mysql> show tables;
+---------------------------+
| Tables_in_mysql2 |
+---------------------------+
| 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> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase 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.01 sec)mysql>
mysql> quit
Bye
备份 表user
[root@localhost ~]# mysqldump -uroot -paiker12345678 mysql user > /tmp/user.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]# [root@localhost ~]# less /tmp/user.sqlTable structure for table `user`DROP TABLE IF EXISTS `user`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `user` (`Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
/tmp/user.sql
恢复 一个表,恢复mysql2 里面的user表
[root@localhost ~]# mysql -uroot -paiker12345678 mysql2 < /tmp/user.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]#
备份所有库
[root@localhost ~]# mysqldump -uroot -paiker12345678 -A > /tmp/mysql_all.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]#
只备份表结构,不备份数据 mysqldump -uroot -paiker12345678 -d mysql2 > /tmp/mysql2.sql
[root@localhost ~]# mysqldump -uroot -paiker12345678 -d mysql2 > /tmp/mysql2.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]# less /tmp/mysql2.sql
总结
备份库
mysqldump -uroot -p123456 mysql > /tmp/mysql.sql
恢复库 //恢复是,必须保证目录一致
mysql -uroot -p123456 mysql < /tmp/mysql.sql
备份表
mysqldump -uroot -p123456 mysql user > /tmp/user.sql
恢复表
mysql -uroot -p123456 mysql < /tmp/user.sql
备份所有库
mysqldump -uroot -p -A >/tmp/123.sql
只备份表结构
mysqldump -uroot -p123456 -d mysql > /tmp/mysql.sql
在mysql下进行导入数据
进入到mysql ,切换至需要导入的数据库,执行source 文件所在路径;例如:
sql>source /root/testbak.sql
扩展
SQL语句教程
http://www.runoob.com/sql/sql-tutorial.html
什么是事务?事务的特性有哪些?
概念
事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit)。事务通常由高级数据库操纵语言或编程语言(如SQL,C++或Java)书写的用户程序的执行所引起,并用形如begin transaction和end transaction语句(或函数调用)来界定。事务由事务开始(begin transaction)和事务结束(end transaction)之间执行的全体操作组成。
例如:在关系数据库中,一个事务可以是一条SQL语句,一组SQL语句或整个程序。
特性
事务是恢复和并发控制的基本单位。
事务应该具有4个属性:原子性、一致性、隔离性、持续性。这四个属性通常称为ACID特性。
原子性(atomicity)。一个事务是一个不可分割的工作单位,事务中包括的操作要么都做,要么都不做。
一致性(consistency)。事务必须是使数据库从一个一致性状态变到另一个一致性状态。一致性与原子性是密切相关的。
隔离性(isolation)。一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。
持久性(durability)。持续性也称永久性(permanence),指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。
根据binlog恢复指定时间段的数据
如果不小心对数据库进行误操作,而又没有及时备份怎么办?这恐怕是广大的coder经常遇到的一类问题。
我今天就因为不小心删除了某个数据库,但最后的备份是1个礼拜前的,唯一能解决的办法就是通过mysqlbinlog来恢复了。解决方案如下:
如果MySQL服务器启用了二进制日志,你可以使用mysqlbinlog工具来恢复从指定的时间点开始(例如,从你最后一次备份)直到现在或另一个指定的时间点的数据。
关于启用二进制日志的信息,参见5.11.3节,“二进制日志”。对于mysqlbinlog的详细信息,参见mysql手册8.6节,“mysqlbinlog:用于处理二进制日志文件的实用工具”。
要想从二进制日志恢复数据,你需要知道当前二进制日志文件的路径和文件名。
一般可以从配置文件(一般情况,Linux下为my.cnf ,windows系统下为my.ini,取决于你的系统)中找到路径。如果未包含在选项文件中,当服务器启动时,可以在命令行中以选项的形式给出。
启用二进制日志的选项为–log-bin。
要想确定当前的二进制日志文件的文件名,输入下面的MySQL语句:
SHOW BINLOG EVENTS \G;
或者还可以从命令行输入下面的内容:
mysql –user=root -pmypasswd -e ‘SHOW BINLOG EVENTS \G’ 将密码mypasswd替换为你的MySQL服务器的root密码。
比如得到的日志文件名为:
mysql-bin.000001 1. 指定恢复时间 对于MySQL5.1.54,可以在mysqlbinlog语句中通过–start-date和–stop-date选项指定DATETIME格式的起止时间。
举例说明,比如在今天下午14:02(今天是2012年3月15日),不小心执行SQL语句删除了一个数据表,但发现没有最新的备份(当然,这只是开发环境,并不是正式的生产环境,正式环境还得定时做数据备份)。要想恢复表和数据,可以通过mysqlbinlog恢复指定时间的备份,输入:
mysqlbinlog –stop-date=”2012-03-15 14:02:00″ /data1/log/mysql/mysql-bin.000001 | mysql -u root -pmypasswd
该命令将恢复截止到在–stop-date选项中以DATETIME格式给出的日期和时间的所有数据。
#备注,可以使用mysqlbinlog 先查看binlog日志文件的所有操作确定时间点
如果你没有检测到输入的错误的SQL语句,可能你想要恢复后面发生的数据库活动。
根据这些,你可以用起使日期和时间再次运行mysqlbinlog:
mysqlbinlog –start-date=”2012-03-15 00:01:00″ /data1/log/mysql/mysql-bin.000001 | mysql -u root -pmypasswd
在该行中,从今天凌晨0:01登录的SQL语句将运行,组合执行前夜的转储文件和mysqlbinlog的两行可以将所有数据恢复到今天凌晨0:01前一秒钟。
你应检查日志以确保时间确切。下一节介绍如何实现。
- 指定时间段恢复 通过mysqlbinlog –start-date 和–stop-date恢复指定时间段的数据库活动记录,如下:
mysqlbinlog –start-date=”2012-03-09 02:00:00″ –stop-date=”2012-03-15 14:00:00″ /data1/log/mysql/mysql-bin.000001 > /tmp/mysql_restore_030915.sql
通过这种方式,就能获取最后一个备份的文件时间2012-03-09 02:00:00到今天删除数据库之前2012-03-15 14:02这段时间的数据库活动事务操作
mysql字符集调整
字符集是一套符号和编码的规则,不论是在oracle数据库还是在mysql数据库,都存在字符集的选择问题。对于数据库来说,字符集又是比较重要的,因为数据库存储的数据大部分都是各种文字,字符集对于数据库的存储、处理性能以及数据迁移都有重要的影响。
如果在数据库创建阶段没有正确选择字符集,那么可能在后期需要更换字符集,而字符集的更换是代价比较高的操作,也存在一定的风险,所以我们建议在应用开始阶段,就按照需求正确的选择合适的字符集,尽量避免后期不必要的调整。
mysql编译安装时,指定字符集的方法:
./configure --with-charset=utf8
mysql的字符集有4个级别的默认设置:服务器级、数据库级、表级和字段级。分别在不同的地方设置,作用也不相同。
1、服务器字符集设定,在mysql服务启动的时候确定。
可以在my.cnf中设置:
[mysql]### 默认字符集为utf8default-character-set=utf8[mysqld]
### 默认字符集为utf8default-character-set=utf8### (设定连接mysql数据库时使用utf8编码,以让mysql数据库为utf8运行)init_connect='SET NAMES utf8'或者在启动选项中指定:
mysqld --default-character-set=utf8
如果没有特别的指定服务器字符集,默认使用latin1(ISO-8859-1的别名)作为服务器字符集。上面三种设置的方式都只指定了字符集,没有去做校对,我们可以用show variables like 'char%';命令查询当前服务器的字符
集和校对规则。
mysql>show variables like 'char%';+--------------------------+----------------------------+| Variable_name | Value |+--------------------------+----------------------------+| character_set_client | utf8 || character_set_connection | utf8 || character_set_database | utf8 || character_set_filesystem | binary || character_set_results | utf8 || character_set_server | utf8 || character_set_system | utf8 || character_sets_dir | /usr/share/mysql/charsets/ |+--------------------------+----------------------------+
注:如果增加default-character-set=utf8后,MYSQL启动报错。可以用character_set_server=utf8来取代default-character-set=utf8,就能正常启动了。这是因为MYSQL不同版本识别的问题。
2、数据库级
创建数据库时指定字符集
mysql>CREATE DATABASE my_db default charset utf8 COLLATE utf8_general_ci;
#注意后面这句话 "COLLATE utf8_general_ci",大致意思是在排序时根据utf8编码格式来排序
如果指定了数据库编码,那么在这个数据库下创建的所有数据表的默认字符集都会是utf8了
修改MYSQL数据库编码,如果是MYSQL数据库编码不正确,可以在MYSQL执行如下命令:
ALTER DATABASE my_db DEFAULT CHARACTER SET utf8;
以上命令就是将MYSQL的my_db数据库的编码设为utf8
3、 表级
创建表时指定字符集
mysql>create table my_table (name varchar(20) not null default '')type=myisam default charset utf8;
#这句话就是创建一个表,指定默认字符集为utf8
修改MYSQL表的编码:
ALTER TABLE my_table DEFAULT CHARACTER SET utf8;
以上命令就是将一个表my_table的编码改为utf8
4、 字段级
alter table test add column address varchar(110) after stu_id;
在stu_id后增加一个字段address
alter table test add id int unsigned not Null auto_increment primary key;
修改字段的编码:
ALTER TABLE `test` CHANGE `name` `name` VARCHAR( 45 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL
以上命令就是将MYSQL数据库test表中name的字段编码改为utf8
在命令行下插入汉字时如下代码:
set names utf8;有时候这一句很关键!insert into charset values('王达');
注意:alter修改的方法不能更新已有记录的字符集,只是对新创建的表和记录生效。对已有记录字符集的调整,需要先将数据导出,经过适当调整后重新导入才可以完全修改编码。
导出导入的字符调整方法:
导出表结构
mysqldump -uroot -pmysql --default-character-set=latin1 -d my_db> createtab.sql
手工修改createtab.sql表结构定义中的字符集为新的字符集
1、导出所有记录
mysqldump -uroot -pmysql --quick --no-create-info --extended-insert --default-character-set=latin1 --host=localhost my_db> data.sql
2、打开data.sql,将set names latin1修改成set names utf8
:%s/latin1/utf8/g
全文替换
3、使用新的字符集创建新的数据库
create database mydata default charset utf8;
4、创建表,执行createtab.sql
mysql -uroot -pmysql mydata<creattab.sql
5、导入数据
mysql -uroot -pmysql mydata<data.sql
注意一点就是目标字符集要大于等于源字符集,否则会丢失一部分不支持的汉字数据。
附:旧数据升级办法
以原来的字符集为latin1为例,升级成为utf8的字符集。原来的表: old_table (default charset=latin1),新表:new_table(default charset=utf8)。
第一步:导出旧数据
mysqldump --default-character-set=latin1 -hlocalhost -uroot -B my_db --tables old_table > old.sql
第二步:转换编码
iconv -t utf8 -f latin1 -c old.sql > new.sql
在这里,假定原来的数据默认是latin1编码。
第三步:导入
修改old.sql,增加一条sql语句: "SET NAMES utf8;",保存。
mysql -hlocalhost -uroot my_db < new.sql
大功告成!
Mysql collate规则:
*_bin: 表示的是binary case sensitive collation,也就是说是区分大小写的
*_cs: case sensitive collation,区分大小写
*_ci: case insensitive collation,不区分大小写
使用xtrabackup备份innodb引擎的数据库
innobackex工具备份mysql数据
xtrbackup只能用于备份innodb引擎的数据库,而innobackex 既可以备份innodb引擎的数据库,也可以备份myisam引擎的数据库。备份时也可分为全量备份和增量备份
一、安装innobackex
使用官网的yum源,方便安装
[root@axiang-02 ~]# rpm -ivh https://www.percona.com/redir/downloads/percona-release/redhat/percona-release-0.1-4.noarch.rpm
[root@axiang-02 ~]# yum install percona-xtrabackup
二、全量备份mysql
2.1、创建并授权备份用户
我们可以直接授权all权限,但是不符合安全原则
mysql -uroot -paxianglinux
grant reload,lock tables,replication client on *.* to 'backupuser'@'localhost' identified by 'axianglinux';
flush privileges;
权限为reload,lock tables,replication client
2.2、全部备份
[root@axiang-02 ~]# mkdir /data/backup
[root@axiang-02 ~]# innobackupex --defaults-file=/etc/my.cnf --user=backupuser --password='axianglinux' -S /tmp/mysql.sock /data/backup
defaults-file=/etc/my.cnf指定配置文件位置是为了获得datadir位置
备份完成后,会在指定的保存目录中生成一个时间戳目录
如图,备份失败也会出现
三、全量备份恢复
3.1、模拟误删除MySQL数据目录故障
[root@axiang-02 ~]# /etc/init.d/mysqld stop
[root@axiang-02 ~]# mv /data/mysql /data/mysql.bak
[root@axiang-02 ~]# mkdir /data/mysql
[root@axiang-02 ~]# chown -R mysql:mysql /data/mysql如果权限没给 -R 则恢复后无法开启MySQL服务。如上图
3.2、恢复
[root@axiang-02 ~]# ls /data/backup/
2017-08-28_20-47-25[root@axiang-02 ~]# innobackupex --use-memory=512M --apply-log /data/backup/2017-08-28_20-47-25/
[root@axiang-02 ~]# innobackupex --defaults-file=/etc/my.cnf --copy-back /data/backup/2017-08-28_20-47-25/
[root@axiang-02 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
四、增量备份
之前我们已经进行了全量备份,经过操作,一段时间后重新全量备份的话,需要耗费的资源较多,这时我们就可以使用增量备份了。
两次增量备份
[root@axiang-02 ~]# rm -rf /data/backup/*
[root@axiang-02 ~]# innobackupex --defaults-file=/etc/my.cnf --user=backupuser --password='axianglinux' -S /tmp/mysql.sock /data/backup
[root@axiang-02 ~]# mysql -uroot -paxianglinux -e 'drop database bbs;'
[root@axiang-02 ~]# innobackupex --defaults-file=/etc/my.cnf --user=backupuser --password='axianglinux' -S /tmp/mysql.sock --incremental /data/backup --incremental-basedir=/data/backup/2017-08-28_23-18-20
[root@axiang-02 ~]# mysql -uroot -paxianglinux -e 'drop database blog;'
[root@axiang-02 ~]# innobackupex --defaults-file=/etc/my.cnf --user=backupuser --password='axianglinux' -S /tmp/mysql.sock --incremental /data/backup --incremental-basedir=/data/backup/2017-08-28_23-24-32
在每个备份的时间戳目录下面都有一个检查点。可以确定恢复顺序
五、顺序恢复
模拟故障,移除原有数据
[root@axiang-02 ~]# /etc/init.d/mysqld stop
[root@axiang-02 ~]# mv /data/mysql /data/mysql.backup
[root@axiang-02 ~]# mkdir /data/mysql
[root@axiang-02 ~]# chown -R mysql:mysql /data/mysql
恢复
innobackupex --apply-log --redo-only /data/backup/2017-08-28_23-18-20
innobackupex --apply-log --redo-only /data/backup/2017-08-28_23-18-20 --incremental-dir=/data/backup/2017-08-28_23-24-32
innobackupex --apply-log /data/backup/2017-08-28_23-18-20 --incremental-dir=/data/backup/2017-08-28_23-27-39
innobackupex --copy-back /data/backup/2017-08-28_23-18-20/
chown -R mysql:mysql /data/mysql
/etc/init.d/mysqld start
相关视频
链接:http://pan.baidu.com/s/1miFpS9M 密码:86dx
链接:http://pan.baidu.com/s/1o7GXBBW 密码:ue2f
转载于:https://blog.51cto.com/235571/2127890
十三周二次课(6月20日)相关推荐
- 十一周二次课(6月1日)
11.28 限定某个目录禁止解析php 因为httpd开放了php模块,所以如果被人上传了文件(php类型),httpd就有可能会进行执行,一旦执行,就会让对方获得我们服务器的root权限,或者是被恶 ...
- 八周二次课(5月14日)
10.28 rsync工具介绍 rsync是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.rsync使用所谓的"rsync算法"来使本地和远程两个主机之间的文 ...
- 分享Silverlight/WPF/Windows Phone一周学习导读(11月14日-11月20日)
分享Silverlight/WPF/Windows Phone一周学习导读(11月14日-11月20日) 本周Silverlight学习资源更新 Silverlight App.xaml用途 Jaso ...
- 天津市人民优步Uber司机奖励政策(9月14日~9月20日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- En-Tan-Mo(ETM)项目周报(6月14日~6月20日)
亲爱的ETM小伙伴: 近日, Facebook发布数字货币Libra, 是伟大征途还是飞蛾扑火? 区块链革命还是全球骗局? Anyway, ETM首席科学家Dr.Aaron Yuan是这样看的: 不可 ...
- 07月20日(周三)腾龙私募内部资料精研【独家】
◆腾龙私募个股内线透视(07月20日) 新开源(300109):乳腺癌微创活检技术在欧盟获批CE 未来目标"两癌"筛查 事件:新开源今日公告,公司与瑞典NeoDynamics AB ...
- 【财经期刊FM-Radio|2021年01月20日】
title: [财经期刊FM-Radio|2021年01月20日] 微信公众号: 张良信息咨询服务工作室 [今日热点新闻一览↓↓] 纳指涨超1.5%,中概大涨,腾讯音乐涨21%,金银油回涨,以太币新高 ...
- 新斗罗大陆手游服务器维护,《新斗罗大陆》4月20日合服公告
标签: 海神 亲爱的魂师大人,为了给大家带来更好的游戏体验,同时也为了响应广大玩家的建议,我们决定将于2021年4月20日(周二)10:30-18:30,对以下服进行合服处理.合服期间服务器将暂时关闭 ...
- 王传福称华为手机是比亚迪造的;贝索斯将于7月20日飞往太空;苹果泄露女生私密照赔偿数百万美元 | EA周报...
EA周报 2021年6月11日 每个星期7分钟,元宝带你喝一杯IT人的浓缩咖啡,了解天下事.掌握IT核心技术. 周报看点 1.王传福称华为手机是比亚迪造的:华为手机 90% 金属框比亚迪造 2.亚马逊 ...
- 梦幻诛仙手游服务器维护中,《梦幻诛仙手游》12月20日凌晨5点~8点停服维护公告...
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 亲爱的少侠: 为了更好的游戏体验,我们计划在12月20日凌晨5点开始停服维护,预计停机时间5点~8点(开机时间会因工作进度推迟或者提前). [新增内容] ...
最新文章
- 摩拜联合微信全国免押金骑行 这样的CP组合可以多来一点
- TASKCTL敏捷调度理念的诠释
- 【实用】ABAP“FOR ALL ENTRIES”的Hints 优化
- python的变量作用域
- 十亿级同步,百亿级调用,千亿级访问量的开放技术平台如何炼成?
- Golang 中使用多维 map
- python照片过人脸_python图片人脸检测
- Linux二进制保护(文末福利)
- RecyclerView异步加载图片
- 测试驱动开发(TDD)的理论基础
- K-Means算法与FCM算法
- 【Unity Shaders】ShadowGun系列之二——雾和体积光
- 使用Markdown如何修改图片大小
- 自己总结出三种进入加密QQ空间的方法
- 2020年6月TIOBE编程语言排名公布
- mysql中长整型是longint_整型int和长整型long
- 三分钟快速了解typeScript的装饰器
- 该爬破解验证码,爬企信宝必须破解滑块验证
- 将CentOS/ubuntu的目录添加到windows的网络位置/将centOS目录影射为windows网络驱动器
- java五子棋的算法_初学java,写了一个五子棋算法的类,请大家多多指教.