mysql基础命令
安装与启动(rpm包) mysql(工具包)mysql-server(服务器包)
数据库目录:/var/lib/mysql
主配置文件:/etc/my.cnf
端口号:3306
创建管理员用户密码:
mysqladmin –uroot password ‘123456’
mysql –uroot –p -h localhost –s/var/lib/mysql/mysql.soc
-u 登入用户名
-h 什么方式登入
-s 套接字文件
修改当前登入密码:
setpassword = password(“123”);
修改其他用户登入密码:
setpassword for ‘用户’@’地址’ = password(”123”)
管理员密码丢失忘记解决方法:
打开:vim/etc/my.cnf
在[mysqld]区域下面
加入skip-grant-tables
重启数据库
updata mysql.user set password=password(“789”)where user = ‘root’
(修改 mysql .user里的 密码 将账户root密码改为789)
创建用户:
create user a1@’localhost’ identified by ‘123’;
(创建 用户 luwei 只允许本地登入 用什么密码123)
create user a2@’%’ identified by ‘123’;
(%表示允许用户任意远端地址登入)
grant all on nginx.* tonginx@'%' identified by '123456';完整
查询用户登入方式
select host,userfrom mysql.user;
权限:
grant all on (privilege可以不加)数据库名.* to 用户名@’%’;
是数据库用户给予某个数据库最大权限,登入方式为任何
revoke create,deleteon 数据库名.* from 用户名@‘%’;
删除数据库用户创建和删除权限
show grants for 用户名@’%’;
数据库用户地址登入具备的权限列表
更改登入方式将本地改变只远程登入
首先设置
updateuser set host='%' where user='aa';
然后重置用户登入数据库权限密码
grant all onmysql.* to 'aa'@'%' identified by '123456';
sql语句使用
增加:
create database 名字;
(创建数据库)
create tables 名字;必须有框架
(创建数据表)
create table 名字(id int,name char(30);(名字和字符类型不能有空格)
(创建数据表名字(设置第一个字段id int表示整数类型,name姓名char(30)表示设置30个字符);
insert into 数据表名字(id,name)values (1,luwei);)
插入数据到数据表名字,插入到那些字段id,name,插入什么样的数据1,luwei;
alter table 数据表名add 字符名这个随便写类型名(不能随便)(就是增加一个如age这个项)
在数据表中添加一个字符类型
alter table 数据表名add 字符名类型名aa first;
表示添加字符类型到数据表中,并且是第一列
alter table 数据表名add 字符名bb 类型名 after aa
表示将创建的bb添加到aa的后一条
例:
create table a2
创建一个表叫a2
(id int unsigned not null auto_increment ,
id 为整数字段unsigned正数(不允许为负数) not null 不允许为空 auto_increment 表示参数自增长
name char(20)not null default ‘’,
name char(20)名字字段最大为20个字符 not null 不允许为空default‘’如果不写默认为空格
age int not nulldefault ‘0’,
age int 正数字段not null 不允许为空 default如果是空默认为0
info char(50)null,
info char(50)长度50个字节 null 可以为空
primary key(id));
主键为id字段
插入一条数据测试:
insert into a2(id,name,age,info) values (1,’luwei’,27,19191919);
插入多条数据测试:
insert into a2 (name)values (‘luwei2’),(‘luwei3’);
把a2中的id,name区域数据转移到a1中
insert into a1(id,name) select id,name from a2;
删除:
drop database 名字
(删除数据库)
drop table 数据库名.数据表名;
(删除数据库下的数据表)
delete from 数据表名 where id=5;
(删除数据表中的id5的行)
dalete from 数据表名where 字段类型名(age) between 20 and 25;
删除数据表中的字段类型范围比如(age年龄20到25的)
delete from 数据表名;
清空数据表内容
alter table 数据表名drop 字符名bb
删除字符名bb的行
改变:
update 数据表名set 字段类型名=‘要修改的名字’ where 字符类型名=6;
修改数据表中匹配为6的的字段类型结果为要修改的名
update 数据表名set字段类型名=’要修改的名’ where 字段类型名between15 and 30;
修改数据表中匹配为15到30范围内的字段类型修改内容为要修改的名
alter table 原名 rename 修改后的名
(修改数据表名字)
alter table 数据表名 modify 字段类型名 char(100) NULL;
修改数据表中的字段类型属性 char 为(100);
alter table 数据表名 change 字段类型原名 information char(100) null
修改数据表字段类型名为information char(100) null(这些为后面的原参数,不然会引起改动)
修改排序
select * from personorder by id asc; 升序排列
select * from personorder by id desc;降序排列
查询:
show database ;
(查看有那些数据库)
use mysql;
进入mysql数据库里来(相当于use =cd)
show tables
查看有哪些数据表
select * from user;(user/G改变显示方式);
表示把数据表里的数据全部加载出来
describe 名字
表示显示数据表的框架
Field: 字段(默认分11个字符)
Type: 类型
Null: 是否可以为空
Key: 主键,定位快速查询
Deflule: 默认值
Extra: 额外参数
本文详细介绍MySQL的基础命令,包括安装启动、用户管理、权限配置、数据库操作等关键内容,适合初学者快速掌握MySQL的基本使用。
539

被折叠的 条评论
为什么被折叠?



