启动mysql服务
systemctl start mysqld
登录mysql
mysql -u root -p # 登录本机
mysql -u root -hlocalhost -P3306 -p # 登录指定地址
CentOS登录mysql时需要输入系统给里的l临时密码:
cat /var/log/mysqld.log |grep password
登录进来后因为是临时密码,所以不能进行操作。需要改密码后才能使用数据库。
alter user root@localhost identified with mysql_native_password by 'Yyjhbb020814@';
忘记密码怎么办?
打开/etc/my.cnf配置文件
vim /etc/my.cnf
#在里面加一句
[mysqld]
skip-grant-tables
如果文件中已经存在[mysqld],则直接将skip-grant-tables写到下方即可。
然后重启mysqld服务
systemctl restart mysqld
此时已经可以无密码登录mysql
没有密码很不安全,因此需要更新mysql密码
update mysql.user set authentication_string=password('密码') where user='root';
一、常用操作
1.创建用户,授予权限
GRANT ALL PRIVILEGES on *.* to 'maxin'@'localhost' IDENTIFIED BY "Mxin19981024@" WITH GRANT OPTION;
grant:授予
all privileges:所有权限
*.*:所有数据库里的所有表
maxin@localhost:用户maxin允许本机登录
identified by “Mxin19981024@”:密码
with grant option:他的权限可以向下传递
2.查看权限
show grants;
show grants for 'root'@'localhost'; # 查看指定用户的权限
3.回收权限
#回收用户的所有权限
revoke all privileges on *.* from '用户'@'localhost';
#回收权限的传递
revoke grant option on *.* from '用户'@'localhost';
4.删除用户
drop user 用户@localhost;
二、数据库的操作
1.查看数据库
show databases;
2.创建数据库
create database [if not exists] 数据库名 charset=utf8;
3.使用数据库
use 数据库名;
4.修改数据库
alter database 数据库名 charset=字符集; #只能修改字符集
5.删除数据库
drop database [if exists] 数据库名;
三、表的操作
1.创建表
create table [if not exists] 表名(
id int not null auto_increment primary key comment '主键',
name varchar(128) not null,
age int
)charset=utf8;
2.查看表结构
desc 表名;
show create table 表名; # 查看建表语句
3.删除表
drop table 表名;
4.修改表结构
alter table 表名 rename 新表名; # 改表名
alter table 表名 rename to 数据库.新表名; # 将表移动到另一个数据库中
alter table 表名 add 字段名 类型; # 新增字段
alter table 表名 modify 字段名 属性; # 修改属性
alter table 表名 change 字段名 新字段名 属性; # 用来修改字段名
alter table 表名 drop 字段名; # 删除字段