如何用linux登入数据库,Linux中MySQL数据库的使用①-----登录、数据库操作、表结构操作...
啟動mysql服務(wù)
systemctl start mysqld
登錄mysql
mysql -u root -p # 登錄本機(jī)
mysql -u root -hlocalhost -P3306 -p # 登錄指定地址
CentOS登錄mysql時需要輸入系統(tǒng)給里的l臨時密碼:
cat /var/log/mysqld.log |grep password
登錄進(jìn)來后因為是臨時密碼,所以不能進(jìn)行操作。需要改密碼后才能使用數(shù)據(jù)庫。
alter user root@localhost identified with mysql_native_password by 'Yyjhbb020814@';
忘記密碼怎么辦?
打開/etc/my.cnf配置文件
vim /etc/my.cnf
#在里面加一句
[mysqld]
skip-grant-tables
如果文件中已經(jīng)存在[mysqld],則直接將skip-grant-tables寫到下方即可。
然后重啟mysqld服務(wù)
systemctl restart mysqld
此時已經(jīng)可以無密碼登錄mysql
沒有密碼很不安全,因此需要更新mysql密碼
update mysql.user set authentication_string=password('密碼') where user='root';
一、常用操作
1.創(chuàng)建用戶,授予權(quán)限
GRANT ALL PRIVILEGES on *.* to 'maxin'@'localhost' IDENTIFIED BY "Mxin19981024@" WITH GRANT OPTION;
grant:授予
all privileges:所有權(quán)限
*.*:所有數(shù)據(jù)庫里的所有表
maxin@localhost:用戶maxin允許本機(jī)登錄
identified by “Mxin19981024@”:密碼
with grant option:他的權(quán)限可以向下傳遞
2.查看權(quán)限
show grants;
show grants for 'root'@'localhost'; # 查看指定用戶的權(quán)限
3.回收權(quán)限
#回收用戶的所有權(quán)限
revoke all privileges on *.* from '用戶'@'localhost';
#回收權(quán)限的傳遞
revoke grant option on *.* from '用戶'@'localhost';
4.刪除用戶
drop user 用戶@localhost;
二、數(shù)據(jù)庫的操作
1.查看數(shù)據(jù)庫
show databases;
2.創(chuàng)建數(shù)據(jù)庫
create database [if not exists] 數(shù)據(jù)庫名 charset=utf8;
3.使用數(shù)據(jù)庫
use 數(shù)據(jù)庫名;
4.修改數(shù)據(jù)庫
alter database 數(shù)據(jù)庫名 charset=字符集; #只能修改字符集
5.刪除數(shù)據(jù)庫
drop database [if exists] 數(shù)據(jù)庫名;
三、表的操作
1.創(chuàng)建表
create table [if not exists] 表名(
id int not null auto_increment primary key comment '主鍵',
name varchar(128) not null,
age int
)charset=utf8;
2.查看表結(jié)構(gòu)
desc 表名;
show create table 表名; # 查看建表語句
3.刪除表
drop table 表名;
4.修改表結(jié)構(gòu)
alter table 表名 rename 新表名; # 改表名
alter table 表名 rename to 數(shù)據(jù)庫.新表名; # 將表移動到另一個數(shù)據(jù)庫中
alter table 表名 add 字段名 類型; # 新增字段
alter table 表名 modify 字段名 屬性; # 修改屬性
alter table 表名 change 字段名 新字段名 屬性; # 用來修改字段名
alter table 表名 drop 字段名; # 刪除字段
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的如何用linux登入数据库,Linux中MySQL数据库的使用①-----登录、数据库操作、表结构操作...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 光刻机唯一上市公司2020
- 下一篇: linux cmake编译源码,linu