創建用戶:
格式:grant select on 數據庫.* to 用戶名@登錄主機 identified by '密碼'
舉例:
例 1:增加一個用戶 test1 密碼為 abc,讓他可以在任何主機上登錄,並對所有數據庫有
查詢、插入、修改、刪除的權限。首先用以 root 用戶連入 MySQL,然后鍵入以下命令:
grant select,insert,update,delete on *.* to root@localhost identified by 'mysql';
或者
grant all privileges on *.* to root@localhost identified by 'mysql';
然后刷新權限設置。
flush privileges;
例 2:如果你不想 root 有密碼操作數據庫“mydb”里的數據表,可以再打一個命令將密碼消掉。
grant select,insert,update,delete on mydb.* to root@localhost identified by '';
#注意:此處的"localhost",是指該用戶只能在本地登錄,不能在另外一台機器上遠程登錄。
如果想遠程登錄的話,將"localhost"改為"%",
表示在任何一台電腦上都可以登錄。也可以指定某台機器可以遠程登錄。
2、修改密碼
用UPDATE直接編輯user表 mysql -u root mysql> use mysql; mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root'; mysql> FLUSH PRIVILEGES; 在丟失root密碼的時候,可以這樣 mysqld_safe --skip-grant-tables& mysql -u root mysql mysql> UPDATE user SET password=PASSWORD("new password") WHERE user='root'; mysql> FLUSH PRIVILEGES;
3、修改登錄的ip,讓那個ip可以登錄
數據庫可以遠程連接或者說用IP地址可以訪問
可能是你的帳號不允許從遠程登陸,只能在localhost。 這個時候只要在localhost的那台電腦,登入mysql后, 更改 "mysql" 數據庫里的 "user" 表里的 "host" 項, 從"localhost"改稱"%" mysql -u root -p mysql>use mysql;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.100' IDENTIFIED BY 'password' WITH GRANT OPTION;
mysql> flush privileges; #刷新數據庫(不要然,重啟才可以看到效果)
mysql> select host, user from user; #查看
4. 刪除用戶
@>mysql -u root -p
@>密碼
mysql>Delete FROM user Where User='test' and Host='localhost';
mysql>flush privileges;
mysql>drop database testDB; //刪除用戶的數據庫
刪除賬戶及權限:>drop user 用戶名@'%';
>drop user 用戶名@ localhost;
5. 列出所有數據庫
mysql>show database;
6. 切換數據庫
mysql>use '數據庫名';
7. 列出所有表
mysql>show tables;
8. 顯示數據表結構
mysql>describe 表名;
9. 刪除數據庫和數據表
mysql>drop database 數據庫名;
mysql>drop table 數據表名;
10.表操作
備注:操作之前使用“use <數據庫名>”應連接某個數據庫。
建表
命令:create table <表名> (<字段名 1> <類型 1> [,..<字段名 n> <類型 n>]);
例子:
mysql> create table MyClass(
> id int(4) not null primary key auto_increment,
> name char(20) not null,
> sex int(4) not null default '0',
> degree double(16,2));
獲取表結構
命令: desc 表名,或者show columns from 表名
例子:
mysql> describe MyClass
mysql> desc MyClass;
mysql> show columns from MyClass;
刪除表
命令:drop table <表名>
例如:刪除表名為 MyClass 的表
mysql> drop table MyClass;
插入數據
命令:insert into <表名> [( <字段名 1>[,..<字段名 n > ])] values ( 值 1 )[, ( 值 n )]
例子:
mysql> insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);
查詢表中的數據
查詢所有行
mysql> select * from MyClass;
查詢前幾行數據
例如:查看表 MyClass 中前 2 行數據
mysql> select * from MyClass order by id limit 0,2;
或者
mysql> select * from MyClass limit 0,2;
刪除表中數據
命令:delete from 表名 where 表達式
例如:刪除表 MyClass 中編號為 1 的記錄
mysql> delete from MyClass where id=1;
修改表中數據
命令:update 表名 set 字段=新值,... where 條件
mysql> update MyClass set name='Mary' where id=1;
在表中增加字段
命令:alter table 表名 add 字段 類型 其他;
例如:在表 MyClass 中添加了一個字段 passtest,類型為 int(4),默認值為 0
mysql> alter table MyClass add passtest int(4) default '0'
更改表名
命令:rename table 原表名 to 新表名;
例如:在表 MyClass 名字更改為 YouClass
mysql> rename table MyClass to YouClass;
更新字段內容
命令:update 表名 set 字段名 = 新內容
update 表名 set 字段名 = replace(字段名, '舊內容', '新內容');
例如:文章前面加入 4 個空格
update article set content=concat(' ', content);
原文https://www.cnblogs.com/lemon-flm/p/7597879.html