1、修改mysql登录密码并登录mysql
[root@localhost ~]# mysqladmin -u root -p password '123' #修改mysql登录密码
Enter password:
[root@localhost ~]# mysql -u root -p #登录mysql数据库
Enter password:
2、创建新的数据库
CREATE DATABASE 数据库名
mysql> create database lxy; #创建lxy数据库
Query OK, 1 row affected (0.00 sec)
mysql> create database lyq; #创建lyq数据库
Query OK, 1 row affected (0.00 sec)
3、查看数据库
mysql> show databases; #查看数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| lxy |
| lyq |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
4、切换数据库
mysql> use lxy; #切换到lxy数据库
Database changed
5、创建表
CREATE TABLE 表名 (字段1名称 类型,字段2名称 类型,……,primary key (主键名))
mysql> create table user (id int(10) not null,name char(20) default '', primary key (id));
Query OK, 0 rows affected (0.01 sec) #创建user表
mysql> create table aaa (id int(10) not null,name char(20) default '', primary key (id));
Query OK, 0 rows affected (0.04 sec) #创建aaa表
6、查看表
mysql> show tables; #查看表
+---------------+
| Tables_in_lxy |
+---------------+
| aaa |
| user |
+---------------+
2 rows in set (0.00 sec)
7、查看表结构
mysql> describe user; #查看表的结构
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(10) | NO | PRI | NULL | |
| name | char(20) | YES | | | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
8、插入数据记录
INSERT INTO 表名(字段1, 字段2,……) VALUES(字段1的值, 字段2的值, ……)
mysql> insert into user values (1,'zhangsan');
Query OK, 1 row affected (0.07 sec)
mysql> insert into user values (2,'lisi');
Query OK, 1 row affected (0.01 sec)
9、查询数据记录
mysql> select * from user; #查看所有列
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
| 2 | lisi |
+----+----------+
2 rows in set (0.00 sec)
mysql> select * from user where name='lisi'; #只查询lisi一条记录
+----+------+
| id | name |
+----+------+
| 2 | lisi |
+----+------+
1 row in set (0.00 sec)
mysql> select id from user; #只查询id列
+----+
| id |
+----+
| 1 |
| 2 |
+----+
2 rows in set (0.00 sec)
10、删除数据库
mysql> drop database lyq;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases; #验证删除数据库成功
+--------------------+
| Database |
+--------------------+
| information_schema |
| lxy |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
11、删除表
mysql> drop table aaa;
Query OK, 0 rows affected (0.03 sec)
mysql> show tables; #验证删除表成功
+---------------+
| Tables_in_lxy |
+---------------+
| user |
+---------------+
1 row in set (0.00 sec)
12、修改数据记录
UPDATE 表名 SET 字段名1=值1[,字段名2=值2] WHERE 条件表达式
mysql> update user set name=('wangwu') where id='1';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from user; #验证修改成功
+----+--------+
| id | name |
+----+--------+
| 1 | wangwu |
| 2 | lisi |
+----+--------+
2 rows in set (0.00 sec)
13、删除记录
mysql> delete from user where name='lisi';
Query OK, 1 row affected (0.00 sec)
mysql> select * from user; #验证删除记录成功
+----+--------+
| id | name |
+----+--------+
| 1 | wangwu |
+----+--------+
1 row in set (0.00 sec)
数据库的用户授权
1、授予权限
GRANT 权限列表 ON 数据库名.表名 TO 用户名@来源地址 [IDENTIFIED BY ‘密码’]
■ 权限列表:用于列出授权使用的各种数据库操作,以逗号进行分隔,如“select,insert,update”。使用“all”表示所有权限,可授权执行任何操作。
■ 数据库名.表面:用于执行授权操作的库和表的名称,其中可以使用通配符“*”。例如,使用“mysql.*” 表示授权操作的对象为mysql库中的所有表。
■ 用户名@来源地址:用于指定用户名称和允许访问的客户机地址,即谁能连接、能从那里连接,来源地址可以是域名、ip地址,还可以使用“%”通配符,表示某个区域或网段内的所有地址,如“%.lxy.com"、”192.168.1.%“等。
■ identify by :用于设置用户连接数据库是所使用的密码字符串。在新建用户时,若省略”identify by"部分,则用户密码将为空。
mysql> grant all on *.* to 'xiaoqi'@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
[root@localhost ~]# mysql -u xiaoqi -p #登录xiaoqi用户
mysql> show databases; #并查询数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| lxy |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
[root@localhost ~]# mysql -u root -p
Enter password:
2、查看权限
mysql> show grants for 'xiaoqi'@'localhost';
+------------------------------------------------------------------------------------------------------------------------+
| Grants for xiaoqi@localhost |
+------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'xiaoqi'@'localhost' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
| GRANT ALL PRIVILEGES ON `auth`.* TO 'xiaoqi'@'localhost' |
+------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
3、撤销权限
REVOKE 权限列表 ON 数据库名.表名 FROM 用户名@来源地址
mysql> revoke all on *.* from 'xiaoqi'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'xiaoqi'@'localhost'; #验证撤销权限成功
+---------------------------------------------------------------------------------------------------------------+
| Grants for xiaoqi@localhost |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'xiaoqi'@'localhost' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
| GRANT ALL PRIVILEGES ON `auth`.* TO 'xiaoqi'@'localhost' |
+---------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
数据库的备份与恢复
1备份数据库
#导出指定库中的部分表
mysqldump -u 用户名 -p [数据库名] [表名] >/备份路径/备份文件名
mysqldump -u 用户名 -p --databases 库名1 [库名2 ] …… >/备份路径/备份文件名
mysqldump -u 用户名 -p --all-databases >/备份路径/备份文件名
[root@localhost ~]# mysqldump -u root -p mysql user > /root/mysql-user.sql #导出指定库中的部分表
Enter password:
You have new mail in /var/spool/mail/root
[root@localhost ~]# mysqldump -u root -p --databases lxy > /root/lxy.sql #导出一个或多个完整的库(包括其中所有表)
Enter password:
[root@localhost ~]# mysqldump -u root -p --opt --all-databases > /root/all.data.sql #备份Mysql服务器中所有的库
Enter password:
2、查看备份文件内容
[root@localhost ~]# egrep -v "^--|^/|^$" /root/all.data.sql
3、还原数据库
mysql> drop database lxy; #删除数据库
Query OK, 1 row affected (0.03 sec)
mysql> exit
Bye
[root@localhost ~]# mysql -u root -p < /root/lxy.sql #还原数据库
Enter password:
[root@localhost ~]# mysql -u root -p
Enter password:
mysql> show databases; #数据库回来了 呵呵呵 !!!
+--------------------+
| Database |
+--------------------+
| information_schema |
| lxy |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> use lxy;
Database changed
mysql> show tables;
+---------------+
| Tables_in_lxy |
+---------------+
| user |
+---------------+
1 row in set (0.01 sec)
---------------------
作者:lxy___
来源:CSDN
原文:https://blog.csdn.net/lxy___/article/details/78841896
版权声明:本文为博主原创文章,转载请附上博文链接!