目录
- 方法一:可直接登录,不需要交互,但是密码明文,不安全
-
- [root@zwb ~]# mysql -u root -p123123 或者 mysql -uroot -p123123
- ................
- mysql> quit或者exit ### 退出
- Bye
-
-
- 方法二:交互形式完成登录,密码不可见
- [root@zwb ~]# mysql -u root -p
- Enter password: ## 输入密码
- ................
-
- mysql> quit或者exit
注:每一条MYSQL操作语句以分号“;”表示结束,输入时可以不区分大小写,但习惯上MYSQL语句中的关键字使用大写。
show databases; 语句:查看当前MySQL服务器中的数据库。初始化的mysql服务器有四个默认数据库。
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | sys |
- +--------------------+
- 4 rows in set (0.00 sec)
①use 数据库名; 先确定查询的数据库
②show tables; 查看当前数据库中包含的表。
- 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 tables; #### 查看数据库中包含的表
- +---------------------------+
- | Tables_in_mysql |
- +---------------------------+
- | columns_priv |
- | db |
- | engine_cost |
- | event |
- | func |
- | general_log |
- | gtid_executed |
- | help_category |
- | help_keyword |
- | help_relation |
- | help_topic |
- | innodb_index_stats |
- | innodb_table_stats |
- | ndb_binlog_index |
- | plugin |
- | proc |
- | procs_priv |
- | proxies_priv |
- | server_cost |
- | 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 |
- +---------------------------+
- 31 rows in set (0.00 sec)
编译安装的mysql数据库的数据文件都存放在/usr/local/mysql/data目录下,每个数据库对应一个子目录,用于存储数据表文件。
describe语句:用于显示表的结构,即组成表的各字段(列)的信息。
①先使用(use 数据库名;)再使用(describe 表名;)来进行查看表的结构
②直接使用(describe 数据库.表名 )来进行查询表结构
- 方法一:
- 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
-
- mysql> describe user;
- +------------------------+-----------------------------------+------+-----+-----------------------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +------------------------+-----------------------------------+------+-----+-----------------------+-------+
- | Host | char(60) | NO | PRI | | |
- | User | char(32) | NO | PRI | | |
- | Select_priv | enum('N','Y') | NO | | N | |
- | Insert_priv | enum('N','Y') | NO | | N | |
- | Update_priv | enum('N','Y') | NO | | N | |
-
-
- 方法二:
- mysql> describe mysql.user;
- +------------------------+-----------------------------------+------+-----+-----------------------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +------------------------+-----------------------------------+------+-----+-----------------------+-------+
- | Host | char(60) | NO | PRI | | |
- | User | char(32) | NO | PRI | | |
- | Select_priv | enum('N','Y') | NO | | N | |
- | Insert_priv | enum('N','Y') | NO | | N | |
- | Update_priv | enum('N','Y') | NO | | N | |
- | Delete_priv | enum('N','Y') | NO | | N | |
- | Create_priv | enum('N','Y') | NO | | N | |
show create table 表名; 语句:获取数据表的结构、索引等详细信息
- mysql> show create table member;
- +--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | Table | Create Table |
- +--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | member | CREATE TABLE "member" (
- "id" int(10) DEFAULT NULL,
- "name" varchar(10) DEFAULT NULL,
- "cardid" varchar(18) DEFAULT NULL,
- "phone" varchar(11) DEFAULT NULL,
- "address" varchar(50) DEFAULT NULL,
- "remark" text
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
- +--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- 1 row in set (0.00 sec)
DDL:数据定义语言,用于创建数据库对象,如库、表、索引等。如:create、alter、drop
DML:数据操纵语言,用于对表中的数据进行管理。如:insert、update、delete
DQL:数据查询语言,用于从数据表中查找符合条件的数据记录。如:select
DCL:数据控制语言,用于设置或者更改数据库用户或角色权限。如:commit、rollback、grant
create database 库名;语句:用于创建一个新的库
- mysql> create database AAA; ### 创建数据库AAA
-
-
- mysql> use AAA ### 使用AAA数据库
- Database changed
-
-
- mysql> SHOW tables; ### 查看数据库内的表
- Empty set (0.00 sec) ### 新创建的库为空的。无表
1、int : 整型(定义整数类型的数据 )
2、float :单精度浮点4字节32位 准确表示到小数点后六位
3、double:双精度浮点8字节64位 双精度支持存储的数字小数位更多
4、char:固定长度的字符类型(定义字符类型数据 )
例如:char(4) 0001,char(10)如果存入数据的实际长度比指定长度要小,会补空格至指定长度,如果存入的数据的实际长度大于指定长度,低版本会被截取,高版本会报错
截取2和截断2区别:
原数据:.688 .691
截取: .69 .69 进行四舍五入
截断: .68 .69 不进行四舍五入
5、varchar:可变长度的字符类型 varchar(设置上限)
6、text:文本
7、image:图片
8、decimal(5,2):表示5个有效长度数字,小数点后面有2位
create table 表名;语句:用于在当前的数据库创建新的表。
语法格式:create table 表名(字段1 名称 类型,字段2 名称 类型,······,primary key(主键名))
需求:创建一张表,名为tongxunlu。包含:序号、姓名、手机号及设置主键
- mysql> use AAA; ## 使用数据库AAA
- Database changed
-
-
- mysql> create table tongxunlu(序号 int(3) primary key,姓名 varchar(20),手机号 int(15));
-
- 注释:
- create table tongxunlu ### 创建表
- 序号 int(3) primary key ### 字段1为“序号”,类型为“int”,长度为“3”,设置为主键
- (primary key)
-
- 姓名 varchar(20) ### 字段2为姓名 类型为“varchar”,长度为“20”
- .....................
-
-
- mysql> describe tongxunlu;
- +-----------+-------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-----------+-------------+------+-----+---------+-------+
- | 序号 | int(3) | NO | PRI | NULL | |
- | 姓名 | varchar(20) | YES | | NULL | |
- | 手机号 | int(15) | YES | | NULL | |
- +-----------+-------------+------+-----+---------+-------+
- 3 rows in set (0.00 sec)
-
-
-
drop 表名;语句:用于删除数据库中的表,需要指定“数据库名.表名”作为参数;若只指定表名最为参数,则先用通过use切换到目标数据库。
- mysql> show tables; ## 查看表
- +---------------+
- | Tables_in_AAA |
- +---------------+
- | lili |
- | tongxunlu |
- +---------------+
- 2 rows in set (0.00 sec)
-
-
- mysql> drop table AAA.lili; ## 删除表,指定数据库.表名作为参数
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> show tables; ## 检验结果
- +---------------+
- | Tables_in_AAA |
- +---------------+
- | tongxunlu |
- +---------------+
- 1 row in set (0.00 sec)
drop database 数据库名;语句:用于删除指定的数据库。
- mysql> show databases; ### 查看数据库
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | AAA |
- | bbb |
- | mysql |
- | performance_schema |
- | sys |
- +--------------------+
- 6 rows in set (0.00 sec)
-
- mysql> drop database bbb; ### 删除数据库bbb
- Query OK, 0 rows affected (0.00 sec)
-
- mysql> show databases; ### 查看数据库
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | AAA |
- | mysql |
- | performance_schema |
- | sys |
- +--------------------+
- 5 rows in set (0.00 sec)
-
-
语法格式:
INSERT INTO 表名(字段1,字段2[,...]) VALUES (字段1的值,字段2的值,...);
- mysql> use AAA; ## 切换到数据库AAA
- Database changed
- mysql> SHOW tables; ## 查看库中的表
- +---------------+
- | Tables_in_AAA |
- +---------------+
- | tongxunlu |
- +---------------+
- 1 row in set (0.00 sec)
-
- mysql> desc tongxunlu; ## 查看表的结构
- +-----------+-------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +-----------+-------------+------+-----+---------+-------+
- | 序号 | int(3) | NO | PRI | NULL | |
- | 姓名 | varchar(20) | YES | | NULL | |
- | 手机号 | int(15) | YES | | NULL | |
- +-----------+-------------+------+-----+---------+-------+
- 3 rows in set (0.00 sec)
-
- mysql> insert into tongxunlu values(1,'张一','188888888'); ## 向表中插入内容
- Query OK, 1 row affected (0.00 sec)
-
- mysql> insert into tongxunlu values(2,'张二','1888888888');
- Query OK, 1 row affected (0.01 sec)
-
- mysql> select * from tongxunlu; ## 查看表中的内容
- +--------+--------+------------+
- | 序号 | 姓名 | 手机号 |
- +--------+--------+------------+
- | 1 | 张一 | 188888888 |
- | 2 | 张二 | 1888888888 |
- +--------+--------+------------+
- 2 rows in set (0.00 sec)
语法格式:
SELECT 字段名1,字段名2[,...] FROM 表名[WHERE 条件表达式];
- mysql> select * from tongxunlu;
- +--------+--------+------------+
- | 序号 | 姓名 | 手机号 |
- +--------+--------+------------+
- | 1 | 张一 | 188888888 |
- | 2 | 张二 | 1888888888 |
- +--------+--------+------------+
- 2 rows in set (0.00 sec)
update语句:用于修改、更新表中的数据记录。
语法格式:update 表名 set 字段名1=字段值1[,字段名2=字段值2] where 条件表达式
- mysql> show databases; ## 查看MySQL服务器上所以数据库
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | AAA |
- | class |
- | mysql |
- | performance_schema |
- | sys |
- +--------------------+
- 6 rows in set (0.00 sec)
-
- mysql> use AAA; ## 切换到数据库AAA
- 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 TABLES; ## 查看数据库AAA内的所有表
- +---------------+
- | Tables_in_AAA |
- +---------------+
- | tongxunlu |
- +---------------+
- 1 row in set (0.00 sec)
-
- mysql> select * from tongxunlu; ## 查询tongxunlu这张表的内容
- +--------+--------+------------+
- | 序号 | 姓名 | 手机号 |
- +--------+--------+------------+
- | 1 | 张一 | 188888888 |
- | 2 | 张二 | 1888888888 |
- +--------+--------+------------+
- 2 rows in set (0.00 sec)
-
- mysql> update tongxunlu set 姓名='张三' where 序号=1; ##把序号为1的那行姓名修改为张三(字
- 段名不需要加单引号,字段值需要加)
- Query OK, 1 row affected (0.00 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
-
- mysql> select * from tongxunlu; ## 查看,验证操作
- +--------+--------+------------+
- | 序号 | 姓名 | 手机号 |
- +--------+--------+------------+
- | 1 | 张三 | 188888888 |
- | 2 | 张二 | 1888888888 |
- +--------+--------+------------+
- 2 rows in set (0.00 sec)
delete 语句:用于删除表中指定的数据记录(也就是行),
语法格式:
DELETE FROM 表名 [WHERE 条件表达式];
- mysql> select * from member;
- +------+---------+---------+--------+----------+----------------+
- | id | name | cardid | phone | address | remark |
- +------+---------+---------+--------+----------+----------------+
- | 1 | zhangsi | 111 | 1111 | hf | this is vip |
- | 4 | lisi | 1234 | 444444 | nanjing | this is normal |
- | 2 | wangwu | 12345 | 222222 | benjing | this is normal |
- | 5 | zhaoliu | 123456 | 555555 | nanjing | this is vip |
- | 3 | qianqi | 1234567 | 333333 | shanghai | this is vip |
- | 6 | liuyi | 123456 | 666666 | nanjing | this is vip |
- | 7 | laoba | 1234567 | 888888 | shanghai | this is vip |
- +------+---------+---------+--------+----------+----------------+
- 7 rows in set (0.00 sec)
-
- mysql> delete from class.member where id=1;
- Query OK, 1 row affected (0.00 sec)
-
- mysql> select * from member;
- +------+---------+---------+--------+----------+----------------+
- | id | name | cardid | phone | address | remark |
- +------+---------+---------+--------+----------+----------------+
- | 4 | lisi | 1234 | 444444 | nanjing | this is normal |
- | 2 | wangwu | 12345 | 222222 | benjing | this is normal |
- | 5 | zhaoliu | 123456 | 555555 | nanjing | this is vip |
- | 3 | qianqi | 1234567 | 333333 | shanghai | this is vip |
- | 6 | liuyi | 123456 | 666666 | nanjing | this is vip |
- | 7 | laoba | 1234567 | 888888 | shanghai | this is vip |
- +------+---------+---------+--------+----------+----------------+
- 6 rows in set (0.00 sec)
①克隆,只复制格式,
create table AAA1 like AAA; #复制格式,通过LIKE方法,复制yyy表结构生成yyy2表
②克隆表,将数据表的数据记录生成到新的表中
CREATE TABLE test02 (SELECT * from test); #复制test 表数据到test02中
修改/etc/my.cnf 配置文件,免密登陆mysql
在[mysqld]内
添加:skip-grant-tables #添加,使登录mysql不使用授权表
- [root@zwb_mysql ~]# cat /etc/my.cnf
- [client]
- port = 3306
- default-character-set=utf8
- socket = /usr/local/mysql/mysql.sock
-
- [mysql]
- port = 3306
- default-character-set=utf8
- socket = /usr/local/mysql/mysql.sock
-
- [mysqld]
- user = mysql
- basedir = /usr/local/mysql
- datadir = /usr/local/mysql/data
- port = 3306
- character_set_server=utf8
- pid-file = /usr/local/mysql/mysqld.pid
- socket = /usr/local/mysql/mysql.sock
- server-id = 1
- skip-grant-tables # 添加此字段,免密码登录
- ....................................
-
-
- [root@zwb_mysql ~]# systemctl restart mysqld ## 重启mysql
- [root@zwb_mysql ~]# mysql ## 免密登录
- Welcome to the MySQL monitor. Commands end with ; or \g.
- ....................................
- mysql>
-
- #然后使用SQL语句修改密码
-
- mysql> show databases; ## 查询有几个库
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | sys |
- +--------------------+
- 4 rows in set (0.00 sec)
- mysql> use mysql ## 使用mysql库
- Database changed
- mysql> show tables; ## 查询mysql中有几张表
- +---------------------------+
- | Tables_in_mysql |
- +---------------------------+
- | columns_priv |
- | db |
- .............................................
- | user | ## 用户信息(账户、密码)存储的表
- +---------------------------+
- 31 rows in set (0.00 sec)
-
- ## 修改root密码
- mysql> update mysql.user set authentication_string = PASSWORD('123123') where user='root'; ## 修改 mysql库user表中的authentication_string字段为 123123《使用PASSWORD()
- 表示加密 ,where定位用户名》
-
-
- mysql> flush privileges ##刷新user 表
-
- mysql> quit ## 退出 也可使用exit
- Bye
-
- 验证:
- [root@zwb_mysql ~]# mysql -uroot -p123123
- .................
- mysql> 登录成功,删除/etc/my.cnf添加的字段
-