如何用linux登入数据库,Linux中MySQL数据库的使用①-----登录、数据库操作、表结构操作...
啟動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 字段名; # 刪除字段
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的如何用linux登入数据库,Linux中MySQL数据库的使用①-----登录、数据库操作、表结构操作...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 光刻机唯一上市公司2020
- 下一篇: 数据库怎么看是什么编码_离婚了怎么发朋友