記一些MySQL比較常用的SQL語句

2021-09-04 12:18:17 字數 2594 閱讀 2799

下面的表名填需要重置的表

使用truncate:

truncate 表名;

使用修改表的方法:

alter

table 表名 auto_increment

=1

暫時失效

set foreign_key_checks =

0;

外來鍵有效

set foreign_key_checks =

1;

資料庫當前只有乙個root使用者,需要建立乙個唯讀帳戶給其他使用,因使用者是使用資料庫管理工具在其他主機訪問,所以還要開戶遠端訪問許可權,操作步驟如下。

使用現有的root使用者登入到mysql。

mysql -uroot -p

建立使用者,並授權select查詢許可權,授權遠端訪問許可權,注意,命令中username/password指使用者名稱密碼,請自己指定。若要限制僅指定ip可以使用此使用者訪問mysql,將%改為具ip即可。

grant

selecton*

.*to'username'

@'%' identified by

"password"

;

重新整理mysql許可權,使使用者建立、授權生效。

flush privileges

;

檢視mysql資料庫中所有使用者

select

distinct concat(

'user: '''

,user

,'''@'''

,host,

''';'

)as query from mysql.

user

;

檢視資料庫中具體某個使用者的許可權

how grants for

'cactiuser'

@'%'

;

修改資料庫編碼及字符集

這一步比較簡單,直接執行即可:

alter

database db_name character

set= utf8mb4 collate

= utf8mb4_general_ci;

修改資料表與表中字段的編碼及字符集

這裡需要兩步。

首先,需要利用語句,生成所有實際執行的語句:

select

concat(

"alter table `"

, table_name,

"` convert to character set utf8mb4 collate utf8mb4_general_ci;"

)as target_tables

from

information_schema.

tables

where

table_schema =

''and table_type =

"base table"

此語句會基於 mysql 的元資料表,得到一組可直接執行的 sql 列表,如下:

alter

table

`table1`

convert

tocharacter

set utf8mb4 collate utf8mb4_bin;

alter

table

`table2`

convert

tocharacter

set utf8mb4 collate utf8mb4_bin;

alter

table

`table3`

convert

tocharacter

set utf8mb4 collate utf8mb4_bin;

alter

table

`table4`

convert

tocharacter

set utf8mb4 collate utf8mb4_bin;

alter

table

`table5`

convert

tocharacter

set utf8mb4 collate utf8mb4_bin;

alter

table

`table6`

convert

tocharacter

set utf8mb4 collate utf8mb4_bin;

其中,table1 到 table6 即為資料庫中的所有資料表。

然後,直接將語句貼上並執行即可。

注意,這裡使用 convert to 而非 default,是因為後者不會修改表中字段的編碼和字符集。

此外,對於資料表比較多的資料庫,可以先將第一步的執行結果匯出到 .sql 檔案,再通過該 sql 檔案執行即可。

記一些MySQL比較常見的設定

注意 修改配置檔案以後,需要重啟mysql服務才能生效。使用命令 修改my.ini檔案 這個檔案一般在mysql的安裝目錄,沒有的話自己建立即可,然後在裡面新增 允許最大連線數 max connections 10000 mysql 設定mysql客戶端預設字符集 default character...

mysql一些命令 mysql常用的一些命令

一 授權登入 參考grant all privileges on cacti.to hnf localhost identified by hnf 2014 只給cacti這個資料庫授權 grant all on to root localhost identified by huningfei 只...

記一些spring的aop中一些常用獲取引數的方法

如果aspect切點切入的是controller在spring mvc 的配置中加入 獲取request 與response 獲取方法與方法上的注釋 這個我主要是用joinpoint來獲取,獲取方式如下 signature signature joinpoint.getsignature metho...