MySQL常用操作(2)MySQL用户管理、常用sql语句、 MySQL数据库备份恢复

                MySQL用户管理

创建一个普通用户并且授权

1.grant all on *.* to 'user1' identified by 'passwd';

grant all on *.* to 'user1' identified by '123456';

(创建user1用户,all表示所有权限(读、写,增、删、改、查等);*.*,前面的*表示所有的数据库,后面的*表示所有的表;identified by后面跟密码,要用单引号''引起来)

grant all on *.* to 'user1'@'指定来源ip' identified by 'passwd';

grant all on *.* to 'user1'@'127.0.0.1' identified by '123456';

(创建了一个user1的用户,并且指定了它只能通过127.0.0.1这个ip登录)


grant all on *.* to 'user1'@'%' identified by 'passwd';

(%表示通配,让所有ip都能连接)


创建用户,登录时使用sock登录,不用指定-h+ip:

grant all on *.* to 'user1'@'localhost' identified by '123456';


创建用户也可以指定数据库,和具体赋予用户什么权限:

grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.136.133' identified by 'passwd';


查看已创建的用户被赋予什么权限:

(1)已经登录了创建的用户使用:show grants

(2)在其他用户下,查看指定的用户被赋予的权限:

show grants for 用户名@'被指定的ip';

show grants for [email protected]'127.0.0.1';



通过grants:

show grants for [email protected]'192.168.136.133'; 出来的内容都在mysql命令中执行一遍,只修改ip,则同一个用户就可以有多个ip登录。(且密码不变)




                        常用sql语句

1.select count(*) from mysql.user;  //查看行数

2.select * from mysql.db; //查看表的所有内容

(这两条搜索所有内容 的命令少用,如果表的内容多,耗费资源。

  mysql的引擎:MyISAM (自动的统计(行))和InnoDB(不会自动统计(行))

如果表中我们设置引擎为MyISAM则在搜索时会很快。

3.select db,user from mysql.db;//搜索mysql.db表中的字段

4.select * from mysql.db where host like '192.168.%'; //模糊搜索


5. insert into db1.t1 values (1, 'abc'); //向db1库t1表插入数据

(插入内容有字符串要加单引号'')

6. update db1.t1 set name='aaa' where id=1;//更新内容(改)

7.truncate table db1.t1; //删除db1.t1表中的数据,但是表结构不变(创表的字段不删,仅删除内容)

8.drop table db1.t1; //删除表


9. drop database db1; //删除数据库

在命令行下执行mysql里的命令:(如创建一个库)

mysql -uroot -p123456 -e "create database mysql2"


                     MySQL数据库备份恢复

 (在linux命令行操作,非在mysql命令下)

备份的命令:mysqldump 

1.备份库:(备份mysql下root用户的mysql库)

mysqldump-uroot -p127.0.0.1 mysql >/tmp/mysqlbak.sql

(mysqldump-uroot -p127.0.0.1 mysql 其实就是搜到内容)


恢复库

mysql -uroot -p123456  mysql < /tmp/mysqlbak.sql


(备份和恢复库或者表,其实就是先把内容搜索出来,让后重定向或者反向重定向)


2.备份表

 mysqldump -uroot -p123456 mysql user > /tmp/user.sql

备份后查看备份文件:cat /tmp/user.sql

(可以看到一些数据来源,和命令)


恢复表(回复表时只需要指定库就行,不用加上表名)

 mysql -uroot -p123456 mysql < /tmp/user.sql


3.备份所有库 

mysqldump -uroot -p -A >/tmp/123.sql

(-A 表示所有)

只备份表结构 

mysqldump -uroot -p123456 -d mysql > /tmp/mysql.sql

(-d 表示表结构)

(只备份表结构就是可以把表的数据过滤,只备份表的结构,创建表时的)


!!!!mysqldump 备份很大的数据量时,会显得很慢!

































原文地址:http://blog.51cto.com/13589255/2091603

时间: 2024-08-27 03:24:45

MySQL常用操作(2)MySQL用户管理、常用sql语句、 MySQL数据库备份恢复的相关文章

MySQL用户管理、常用sql语句、数据库备份恢复

MySQL用户管理 MySQL分为普通用户与root用户.这两种用户的权限不一样.新建普通用户在MySQL数据库中,建立用户有3种方式:使用CREATE USER语句来创建新的用户:直接在mysql.user表中INSERT用户:使用GRANT语句来新建用户: grant命令grant all on *.* to 'user1'@'192.168.71.131' identified by '123456'; all 表示所有的权限. 表示所有的库,前面的表示库名,后面的表示所有的表'user1

MySQL(用户管理,常用sql语句,数据库备份恢复,MySQL调优,恢复误操作数据)

一.MySQL用户管理. 一个MySQL数据库里可以跑多个库,总不能给所有人的程序员root用户,则可以给他们单独的用户访问数据库. 创建用户:(grant all on *.* to 'user1'是把所有库的权限给'user1,他的来源Ip是127.0.0.1,他的密码是lty123456') (第一个*是库名,如果你写成mysql.*那就是对mysql库的所有权限) (来源ip也可以写成 % ,表示来源的所有ip) (grant这种语句是不会记录到命令历史里去的,因为不安全.) mysql

【转】MySQL用户管理及SQL语句详解

[转]MySQL用户管理及SQL语句详解 1.1 MySQL用户管理 1.1.1 用户的定义 用户名+主机域 mysql> select user,host,password from mysql.user; +--------+------------+-------------------------------------------+ | user | host | password | +--------+------------+---------------------------

MySQL用户管理、常用sql语句、数据库备份

13.4 MySQL用户管理 创建用户并授权 指定登录IP [[email protected] ~]# mysql -uroot -pEnter password: Welcome to the MySQL monitor.mysql> grant all on . to 'user1'@'127.0.0.1' identified by '123456';#创建user1用户并授予其所有权限"."(通配符)#第一个表示db_name:第二个表示tb_name#同时指定其来源I

MySQL用户管理及SQL语句详解

1.1 MySQL用户管理 1.1.1 用户的定义 用户名+主机域 mysql> select user,host,password from mysql.user; +--------+------------+-------------------------------------------+ | user | host | password | +--------+------------+-------------------------------------------+ | r

1.1 MySQL用户管理及SQL语句详解

1.1 MySQL用户管理 1.1.1 用户的定义 用户名+主机域 mysql> select user,host,password from mysql.user; +--------+------------+-------------------------------------------+ | user | host | password | +--------+------------+-------------------------------------------+ | r

mysql用户管理、常用sql语句、mysql数据库备份恢复

mysql用户管理 1.新增用户user1,并设置密码为123456 mysql> grant all on *.* to 'user1'@'127.0.0.1' identified by '123456'; #创建user1用户并授予其所有权限"*.*"(通配符) #第一个*:表示所有的数据库 #第二个*:表示所有的表 #127.0.0.1表示来源IP,指的只有这个IP可以连接:'%':代表所有的ip #identified by 设置密码 2.对user1用户进行授权管理

MySQL用户管理、sql常用语句、mysql备份与恢复

MySQL用户管理 创建用户 grant all on *.* to 'user1'@'localhost' identified by '123456'; grant all on db1.* to 'user2'@'%' identified by '123456'; //创建user2用户,所有ip都能登录,指定权限为db1库下的所有表: flush privileges; 刷新授权 .:表示所有库和表:user1:用户名:localhost:登录ip,默认localhost为本机登录ip

13.4 mysql用户管理 13.5 常用sql语句 13.6 mysql数据库备份恢复

13.4 mysql用户管理 grant all on . to 'user1' identified by 'passwd';mysql> grant all on . to 'user1' identified by 'passwd';Query OK, 0 rows affected (0.01 sec) grant SELECT,UPDATE,INSERT on db1. to 'user2'@'192.168.15.132' identified by 'passwd';mysql>