###1.连接mysql
格式:mysql -h 主机地址 -P 端口号 -u 用户名 -p用户密码 打开dos窗口,输入命令mysql -h localhost -P3306 -u root -p654321
1.如果mysql服务器安装在本台主机上,可以省略 -h localhost
2.-p后面直接输入密码,不能带有空格 3.如果是刚安装好的mysql,超级用户root是没有密码的,直接输入命令 mysql -h localhost -u root -p 就可以进入mysql,接着修改/设置密码参看我这篇 4.退出mysql命令:exit(回车) ###2.修改密码 有两种方法 ####方法1:以root用户登录进去了mysql> set password for root@localhost=password('654321');Query OK, 0 rows affected, 1 warning (0.04 sec)mysql>
####方法2:未登录
注意,最好是在mysql安装目录bin目录下,执行如下命令 mysqladmin -u 用户名 -p旧密码 password 新密码E:\mysql-5.7.14-winx64\mysql-5.7.14-winx64\bin>mysqladmin -u root -p654321 password 777888999mysqladmin: [Warning] Using a password on the command line interface can be insecure.Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.E:\mysql-5.7.14-winx64\mysql-5.7.14-winx64\bin>
由上可知,我的mysql安装在E:\mysql-5.7.14-winx64\目录下
注意事项: 1.-p后面是原密码,这里没有空格 2.password后面是新密码,这里有空格 ###3.增加新用户 ####1.创建新用户(不带密码) 进入mysql后,采用如下命令查看目前有多少个databases 如上可知,test数据库是我创建的,其它均为系统自带。 information_schema:称它为信息数据库,它提供了访问数据库"元数据"的方式,元数据是指:数据库名、表名、列的数据类型、访问权限等。简单的说,该表就是保存其它数据库的 元数据。 performance_schema:它是一个存储引擎,主要是保存数据库服务器性能参数。例如:提供进程等待信息、保存历史事件等。 所有的用户都存放在mysql数据库中。 使用mysql数据库命令mysql> use mysql;Database changedmysql>
创建新用户
mysql> create user panda;Query OK, 0 rows affected (0.00 sec)mysql>
如上可知,创建了一个叫做panda的用户,该用户可以在任意安装了mysql的客户端(例如:navicat),无需密码就能访问目标服务器上的mysql数据库。
查看刚刚新建的用户采用如下命令:mysql> select user from user;+------------+| user |+------------+| panda || reply_user || mysql.sys || root |+------------+4 rows in set (0.00 sec)mysql>
由如上信息可知,mysql数据库中有4个用户,很明显panda用户是刚刚新建的。
显示当前用户采用如下命令mysql> select user();+----------------+| user() |+----------------+| root@localhost |+----------------+1 row in set (0.00 sec)mysql>
很明显当前用户是root。
####2.创建新用户(带上密码) 很明显,授权panda登陆自己的mysql服务器是不安全的,任何人都可以在任何地方使用panda来访问/修改自己的数据库。因此,需要创建带密码的用户。采用如下命令mysql> create user lxl identified by '123456';Query OK, 0 rows affected (0.00 sec)mysql>
####3.指定用户在指定的主机上访问数据库
如上的lxl用户在任何一台主机上都可以访问自己的数据库,也不是很安全,这里设置lxl用户只能在指定的主机上访问自己的数据库。mysql> create user zhangSan@192.168.2.11 identified by '123456';Query OK, 0 rows affected (0.00 sec)mysql>
如上是 指定用户 zhangSan只能在192.168.2.11这台主机上采用密码 123456 来访问自己的数据库。
这里在看下刚刚新建的用户mysql> select user,authentication_string from user;+------------+-------------------------------------------+| user | authentication_string |+------------+-------------------------------------------+| root | *8D641143581B4D519215B231D2305B0CE66CF3A2 || mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE || reply_user | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 || panda | || lxl | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 || zhangSan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |+------------+-------------------------------------------+6 rows in set (0.00 sec)mysql>
由上可知,用户panda是没有密码的。用户lxl和zhangSan的密码是一样的(经过加密了)。
####4.授予用户权限 命令:grant 权限1,权限2...权限n on 数据库名称 表名称 to 用户名@用户地址 identified by '密码' 权限有哪些 1>普通数据用户 普通数据用户具有增、删、改、查的权利 例如,给如上 lxl 用户赋予 增删改查的权限mysql> grant select,insert,update,delete on test.* to lxl;Query OK, 0 rows affected (0.07 sec)
查询用户具备哪些权限,使用如下命令
mysql> show grants for lxl;+---------------------------------------------------------------+| Grants for lxl@% |+---------------------------------------------------------------+| GRANT USAGE ON *.* TO 'lxl'@'%' || GRANT SELECT, INSERT, UPDATE, DELETE ON `test`.* TO 'lxl'@'%' |+---------------------------------------------------------------+2 rows in set (0.00 sec)mysql>
####5.删除用户
方式1:采用drop user 用户名mysql> select user from user;+-----------+| user |+-----------+| lxl || panda || mysql.sys || root || zhangSan |+-----------+5 rows in set (0.00 sec)mysql> drop user zhangSan@localhost;Query OK, 0 rows affected (0.00 sec)mysql> select user from user;+-----------+| user |+-----------+| lxl || panda || mysql.sys || root |+-----------+4 rows in set (0.00 sec)mysql>
方式2:采用delete from user where user=用户名
mysql> delete from user where user='lxl';Query OK, 1 row affected (0.00 sec)mysql> select user from user;+-----------+| user |+-----------+| panda || mysql.sys || root |+-----------+3 rows in set (0.00 sec)mysql> select user from db;+-----------+| user |+-----------+| lxl || mysql.sys |+-----------+2 rows in set (0.00 sec)mysql>
由上信息可知,采用delete删除用户时,仅仅将该用户在user表里删除了,但是该用户依然存在db表中。
总结:drop user,将该用户的信息全部删掉。delete只会清除user表,还会存在于db表,即如果delete之后,再创建一个最小权限的用户,它将会继承之前的权限。 ####6.修改用户权限 命令:revoke 权限 on 数据库.表 from 用户mysql> show grants for panda;+-----------------------------------------------------------------+| Grants for panda@% |+-----------------------------------------------------------------+| GRANT USAGE ON *.* TO 'panda'@'%' || GRANT SELECT, INSERT, UPDATE, DELETE ON `test`.* TO 'panda'@'%' |+-----------------------------------------------------------------+2 rows in set (0.00 sec)mysql> revoke delete on test.* from panda;Query OK, 0 rows affected (0.00 sec)mysql> show grants for panda;+---------------------------------------------------------+| Grants for panda@% |+---------------------------------------------------------+| GRANT USAGE ON *.* TO 'panda'@'%' || GRANT SELECT, INSERT, UPDATE ON `test`.* TO 'panda'@'%' |+---------------------------------------------------------+2 rows in set (0.00 sec)mysql>
###4.创建数据库
####4.1 创建数据库 命令:create database 数据库名称mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys || test |+--------------------+5 rows in set (0.02 sec)mysql> create database study;Query OK, 1 row affected (0.00 sec)mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || study || sys || test |+--------------------+6 rows in set (0.00 sec)mysql>
####4.2 显示数据库
命令:show databases;(注意最后有个s) ####4.3 删除数据库 命令:drop database 数据库名称;mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || study || sys || test |+--------------------+6 rows in set (0.00 sec)mysql> drop database study;Query OK, 0 rows affected (0.01 sec)mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys || test |+--------------------+5 rows in set (0.00 sec)mysql>
####4.4 数据库的一些信息
1.查询mysql版本mysql> select version();+-----------+| version() |+-----------+| 5.7.13 |+-----------+1 row in set (0.01 sec)mysql>
2.显示当前日期
mysql> select now();+---------------------+| now() |+---------------------+| 2016-09-17 12:23:32 |+---------------------+1 row in set (0.03 sec)mysql>
###5.数据表
####1.显示表 命令:show tables;mysql> use test;Database changedmysql> show tables;+----------------+| Tables_in_test |+----------------+| t_book || t_stock || t_thing || t_user |+----------------+4 rows in set (0.00 sec)mysql>
####2.创建表
命令:create table 表名(<字段名><类型名(位数)>[<字段名><类型名(位数)>,......]);mysql> show tables;+----------------+| Tables_in_test |+----------------+| t_book || t_stock || t_thing || t_user |+----------------+4 rows in set (0.00 sec)mysql> create table t_location -> ( -> location_id int(10) primary key, -> location_name varchar(100) -> );Query OK, 0 rows affected (0.11 sec)mysql> show tables;+----------------+| Tables_in_test |+----------------+| t_book || t_location || t_stock || t_thing || t_user |+----------------+5 rows in set (0.00 sec)mysql>
####3.删除表
mysql> show tables;+----------------+| Tables_in_test |+----------------+| t_book || t_location || t_stock || t_thing || t_user |+----------------+5 rows in set (0.00 sec)mysql> drop table t_location;Query OK, 0 rows affected (0.06 sec)mysql> show tables;+----------------+| Tables_in_test |+----------------+| t_book || t_stock || t_thing || t_user |+----------------+4 rows in set (0.00 sec)mysql>
####4.向表中插入数据
命令:insert into 表名(<字段名1>,<字段名2>,...) values(值1,值2,...)mysql> insert into t_location(location_id,location_name) values(1000000000,'成都');Query OK, 1 row affected (0.02 sec)mysql> select * from t_location;+-------------+---------------+| location_id | location_name |+-------------+---------------+| 1000000000 | 成都 |+-------------+---------------+1 row in set (0.00 sec)mysql>
####5.增加字段
命令:alter table 表名 add 字段 类型;mysql> alter table t_location add grade int(1);Query OK, 0 rows affected (0.13 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> select * from t_location;+-------------+---------------+-------+| location_id | location_name | grade |+-------------+---------------+-------+| 1000000000 | 成都 | NULL |+-------------+---------------+-------+1 row in set (0.00 sec)mysql>
####6.修改字段
命令:alter table 表名 change 原字段 新字段 类型;mysql> alter table t_location change grade bank int(1);Query OK, 0 rows affected (0.06 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> select * from t_location;+-------------+---------------+------+| location_id | location_name | bank |+-------------+---------------+------+| 1000000000 | 成都 | NULL |+-------------+---------------+------+1 row in set (0.00 sec)mysql>
####7.删除字段
命令:alter table 表名 drop 字段名;mysql> alter table t_location drop bank;Query OK, 0 rows affected (0.12 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> select * from t_location;+-------------+---------------+| location_id | location_name |+-------------+---------------+| 1000000000 | 成都 |+-------------+---------------+1 row in set (0.00 sec)mysql>
####8.更新数据
命令:update 表名 set 字段1=新值1,... where 条件;mysql> update t_location set location_name='北京';Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from t_location;+-------------+---------------+| location_id | location_name |+-------------+---------------+| 1000000000 | 北京 |+-------------+---------------+1 row in set (0.02 sec)mysql>
####9.修改表名
命令:rename table 原表名 to 新表名;mysql> rename table t_location to t_address;Query OK, 0 rows affected (0.07 sec)mysql> show tables;+----------------+| Tables_in_test |+----------------+| t_address || t_book || t_stock || t_thing || t_user |+----------------+5 rows in set (0.00 sec)mysql>
###6.数据备份
####1.导出数据库 命令:mysqldump -u 用户名 -p密码 数据库名称 > 导出的文件名称; 首先进入mysql的安装路径,一直到bin下,接着使用如上命令D:\softDown\mysql\mysql-5.7.13-winx64>cd binD:\softDown\mysql\mysql-5.7.13-winx64\bin>mysqldump -u root -p123456 test > test.sqlmysqldump: [Warning] Using a password on the command line interface can be insecure.D:\softDown\mysql\mysql-5.7.13-winx64\bin>
这样导出的文件就在bin目录下面。
####2.导出单张表数据 命令:mysqldump -u 用户名 -p密码 数据库名称 表名称 > 导出的文件名称; 实际上就是在数据库名称后面添加上表名称 就行。D:\softDown\mysql\mysql-5.7.13-winx64\bin>mysqldump -u root -p123456 test t_user > test_user.sqlmysqldump: [Warning] Using a password on the command line interface can be insecure.D:\softDown\mysql\mysql-5.7.13-winx64\bin>