mysql 8 命令 Mysql8常用命令

2021-10-18 08:49:20 字數 4867 閱讀 9038

1、安裝:

移除原來的mysql服務:mysqld -remove mysql

mysqld -install

2、初始化:

mysqld  --initialize

3、啟動:

net start mysql

4、登陸:

mysql -u root -p

5、修改密碼:

mysqld --shared-memory --skip-grant-tables ;

mysql ;

flush privileges ;

alter user "root"@"localhost" identified by "root";

6、授權:

grant all privileges on *.* to 'root'@'%'identified by '密碼' with grant option;

grant all privileges on src.* to 'src'@'%' ;

7、檢視引數:

show variables like '%lower%' ;

8、建立資料庫:

1、 create database 資料庫名;

2、 grant select,insert,update,delete,create,drop,alter on 資料庫名.* to 資料庫名@localhost identified by '密碼';

grant all privileges  on *.* to root@'%';

3、 set password for  '資料庫名'@'localhost' = old_password('密碼');

4、建立使用者:

create user foo@localhost identified by '123';

update user set user="新使用者名稱" where user="root" ;

5、更新密碼:

update user set password=password("新密碼") where user='你的使用者名稱' ;

set password for root@localhost = password('@root123');

遠端連線修改密碼:alter user 'root'@'localhost' identified with mysql_native_password by 'root1234';

9、刪除字段:

alter table id_name drop column age,drop column address;

10、刪除資料庫:

drop database xhkdb ;

11、刪除資料庫下所有表:show create table s10.tpg_gzfsqb ;

select concat('drop table if exists ', table_name, ';')

from information_schema.tables

where table_schema = 'mydb';

12、檢視版本號:select version();

檢視埠號:show global variables like 'port';

13、載入檔案:source d:/demo.sql

14、新增表字段:alter table table1 add transactor varchar(10) not null;

15、建表語句:

create table tb_emp(id int primary key auto_increment,   ###auto_increment只是mysql特有的

name varchar(18),

*** varchar(2));

16、刪除所有表資料:

select concat( 'drop table ', table_name, ';' )

from information_schema.tables

where table_schema='mydatabase'  table_name like ' sql_%';

17、刪資料:you are using safe mode解決方法:set sql_safe_updates = 0 ;

18、檢視建立表語句:

19、檢視引數:show variables like 'lower_case_table_names';

20、解除正在死鎖的狀態有兩種方法:

第一種:

1.查詢是否鎖表

show open tables where in_use > 0 ;

2.查詢程序(如果您有super許可權,您可以看到所有執行緒。否則,您只能看到您自己的執行緒)

show processlist ;

3.殺死程序id(就是上面命令的id列)

kill id

第二種:

1.檢視下在鎖的事務

select * from information_schema.innodb_trx ;

2.殺死程序id(就是上面命令的trx_mysql_thread_id列)

kill 執行緒id

21、引數含義:

key_buffer_size=8388608    檢視命令:show variables like 'key_buffer_size';    set global key_buffer_size=204800;

為了最小化磁碟的 i/o ,myisam 儲存引擎的表使用鍵快取記憶體來快取索引,這個鍵快取記憶體的大小則通過 key-buffer-size 引數來設定。

read_buffer_size=131072    檢視命令:show variables like 'read_buffer_size';    set global read_buffer_size=8192;

max_used_connections=12

max_threads=200

thread_count=13

connection_count=12

22、建立sequence:

a、建立--sequence 管理表

drop table if exists sequence;

create table sequence (

name varchar(50) not null,

current_value int not null,

increment int not null default 1,

primary key (name)

) engine=innodb;

b、建立--取當前值的函式

drop function if exists currval;

delimiter $

create function currval (seq_name varchar(50))

returns integer

language sql

deterministic

contains sql

sql security definer

comment ''

begin

declare value integer;

set value = 0;

select current_value into value

from sequence

where name = seq_name;

return value;

enddelimiter ;

c、建立--取下乙個值的函式

drop function if exists nextval;

delimiter $

create function nextval (seq_name varchar(50))

returns integer

language sql

deterministic

contains sql

sql security definer

comment ''

begin

update sequence

set current_value = current_value + increment

where name = seq_name;

return currval(seq_name);

enddelimiter ;

d、建立--更新當前值的函式

drop function if exists setval;

delimiter $

create function setval (seq_name varchar(50), value integer)

returns integer

language sql

deterministic

contains sql

sql security definer

comment ''

begin

update sequence

set current_value = value

where name = seq_name;

return currval(seq_name);

enddelimiter ;

e、測試函式功能

insert into sequence values ('testseq', 0, 1);----新增乙個sequence名稱和初始值,以及自增幅度

select setval('testseq', 10);---設定指定sequence的初始值

select currval('testseq');--查詢指定sequence的當前值

select nextval('testseq');--查詢指定sequence的下乙個值

mysql8問題 mysql8中的問題

only full group by 對於group by聚合操作,如果在 select 中的列,沒有在 group by 現,那麼這個sql是不合法的,因為列不在group by從句中。no auto value on zero 該值影響自增長列的插入。預設設定下,插入0或null代表生成下乙個自...

mysql8建立不了使用者 mysql8建立使用者

假如是mysql8版本的話,使用 grant all privileges to 使用者 localhost identified by 自定義密碼 會報錯,因為要先建立使用者再進行賦權,不能同時進行 建立使用者 create user 使用者名稱 localhost identified by 密...

MySQL教程 MySQL 8入門

mysql仍然是現代應用程式程式設計堆疊中最常見,最一致的元素之一。如果您想要為您的應用程式或服務使用資料庫,並且您的需求相當通用,那麼mysql是簡單的預設值之一。它被廣泛使用並且易於理解,因此在為特定應用程式部署mysql時,您可以汲取大量社群知識和經驗。最新的主要版本mysql 8修復了mys...