Mariadb/Mysqlコマンドライン共通コマンド

32525 ワード

一、初期化等
1、データベースへの登録方法
mysql-uユーザー名-pユーザーパスワード
2、root及びユーザーパスワードの修正
    use mysql;
    update user set password=password('11111111') where user='root' and host='localhost';
    flush privileges;
MariaDB [mysql]> update user set password=password('11111111') where user='root' and host='localhost';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.01 sec)

MariaDB [mysql]> exit

3、ユーザーの作成
    insert into mysql.user(host,user,password)values("localhost","test",password("password"));
    flush privileges;
4、ユーザーの削除
    DELETE FROM user WHERE User="test" and Host="localhost";    flush privileges;  
5、ユーザーのデータベースを削除する
    drop database test1;
6、インタラクティブモード初期化
    mysql_secure_installation
 
二、常用操作
1、データベースリストを表示する
show databases:すべてのデータベースを表示
2、データベースの作成
create database zxg:名前の末尾zxgのデータベースを作成する
3、データベースに入る
use zxg:zxgのデータベースに入る
4、ライブラリ内のデータテーブルを表示する
show tables:データベースに何枚のテーブルがあるかを表示します.
5、データテーブルの作成
create table t 1(id varchar(20)、name varchar(20):t 1テーブルという名前のフィールドを作成し、id、name、varcharはデータ長の設定を表し、文字で長さ単位を定義する2つのフィールドを作成します.
6、データの挿入
insert into t 1 values("1","zxg"):テーブルにデータを挿入する
7、データシートの表示
select*from t 1:t 1テーブルデータの内容を表示する
8、多条件クエリー
select*from t 1 where id=1 and age='zxg':id、age複数条件クエリー
9、フィールド内容の表示
desc t 1:t 1テーブルフィールドの内容を表示する
10、フィールド長の変更
alter table t 1 modify column name varchar(20):nameフィールドの長さを変更する
11、このフィールドの内容を修正する
    update t1 set name='zxg.Net'where id=1:nameフィールドの内容を変更する
12、権限の更新
flush privileges:権限のリフレッシュ
13、フォームを空にする
delete from t 1:テーブルの内容をクリア
14、データテーブルの削除
drop table t 1:テーブルの削除
15、データベースの削除
drop database zxg:zxgデータベースの削除
16.データベース文字セットの表示
show variables like'%char%':データベース文字セットの表示
17、ストレージエンジンの表示
show engines:MySQLストレージエンジンを表示します.
18.デフォルトのストレージエンジンの表示
    show variables like '%storage_Engine%':MySQLのデフォルトのストレージエンジンの表示
19、ストレージエンジンの修正
alter table t 1 engine=innodb:MySQL t 1テーブルストレージエンジンの変更
 
 
 1 [root@web2 ~]# mysql
 2 Welcome to the MariaDB monitor.  Commands end with ; or \g.
 3 Your MariaDB connection id is 2
 4 Server version: 5.5.60-MariaDB MariaDB Server
 5 
 6 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
 7 
 8 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 9 
10 MariaDB [(none)]> show databases;
11 +--------------------+
12 | Database           |
13 +--------------------+
14 | information_schema |
15 | mysql              |
16 | performance_schema |
17 | test               |
18 +--------------------+
19 4 rows in set (0.00 sec)
20 
21 MariaDB [(none)]>  create database zxg;                   
22 Query OK, 1 row affected (0.00 sec)
23 
24 MariaDB [(none)]> use zxg;
25 Database changed
26 MariaDB [zxg]> show tables;
27 Empty set (0.00 sec)
28 
29 MariaDB [zxg]> create table t1(id varchar(20),name varchar(20));
30 Query OK, 0 rows affected (0.00 sec)
31 
32 MariaDB [zxg]> show tables;
33 +---------------+
34 | Tables_in_zxg |
35 +---------------+
36 | t1            |
37 +---------------+
38 1 row in set (0.00 sec)
39 
40 MariaDB [zxg]> 

 
 
 1 MariaDB [zxg]> insert into t1 values ("1","zxg");
 2 Query OK, 1 row affected (0.01 sec)
 3 
 4 MariaDB [zxg]> select *from t1;
 5 +------+------+
 6 | id | name |
 7 +------+------+
 8 | 1 | zxg |
 9 +------+------+
10 1 row in set (0.00 sec)
11 
12 MariaDB [zxg]> select *from t1 where id=1; 
13 +------+------+
14 | id | name |
15 +------+------+
16 | 1 | zxg |
17 +------+------+
18 1 row in set (0.00 sec)
19 
20 MariaDB [zxg]> select *from t1 where id=1 and name='zxg';
21 +------+------+
22 | id | name |
23 +------+------+
24 | 1 | zxg |
25 +------+------+
26 1 row in set (0.01 sec)
27 
28 MariaDB [zxg]> desc t1;
29 +-------+-------------+------+-----+---------+-------+
30 | Field | Type | Null | Key | Default | Extra |
31 +-------+-------------+------+-----+---------+-------+
32 | id | varchar(20) | YES | | NULL | |
33 | name | varchar(20) | YES | | NULL | |
34 +-------+-------------+------+-----+---------+-------+
35 2 rows in set (0.00 sec)
36 
37 MariaDB [zxg]> alter table t1 modify column name varchar(20);
38 Query OK, 0 rows affected (0.00 sec)
39 Records: 0 Duplicates: 0 Warnings: 0
40 
41 MariaDB [zxg]> update t1 set name='zxg.net' where id=1;
42 Query OK, 1 row affected (0.00 sec)
43 Rows matched: 1 Changed: 1 Warnings: 0
44 
45 MariaDB [zxg]>

 
 
 
三、mysqlデータベース文字セット設定
mysqlデータベースにデータを格納する場合、デフォルトはlatinlで符号化され、中国語文字を格納する場合、呼び出し時に文字化けして表示されます.この文字化けしの問題を解決するために、mysqlのデフォルト文字セットをUTE-8に変更する必要があります.
mariadbをインストールする時はute-8にデフォルト設定されています
MariaDB [(none)]> show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

いいえ、設定できます.
    SET character_set_client = utf8;
    SET character_set_results = utf8;
    SET character_set_connection = utf8;
    
四、mysqlデータベースパスワード管理
パスワードアクセスを設定し、パスワード解読、パスワード権限、パスワードの変更;
1、ユーザー及び授権の作成
    grant all on zxg.* to test@localhost identified by 'pas';
    
    grant select,insert,update,delete on *.*to test@"%"identified by 'pas';
    grant all on zxg.* to test@`192.168.216.53` identified by 'pas'
2、パスワードの解読方法
サービスを停止します---権限の方式をスキップしてスタートします---ただ1つのウィンドウを开いて登录します---登录してパスワードを改正します
    1)systemctl stop mariadb
    2)mysqld_safe --skip-grant-tables &
[root@web2 ~]# mysqld_safe --skip-grant-tables &
[1] 47542
[root@web2 ~]# 190520 15:45:22 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
190520 15:45:22 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

 
[root@web2 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> update user set password=password('11111111') where user='root' ;
Query OK, 3 rows affected (0.01 sec)
Rows matched: 4  Changed: 3  Warnings: 0

MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> exit
Bye
[root@web2 ~]# 

そして「権限スキップ」方式「ctrl+c」を終了し、mysqlを正常に起動すればよい
 
 
 
五、mysqlプロファイルの詳細
   
1、パラメータの説明
 
[mysqld]                                                              #     
port        = 3306                                                    #    
socket      = /tmp/mysql.sock                                         #    
user    = mariadb                                                     #  mariadb    
basedir = /usr/local/mariadb                                          #    
datadir = /data/mysql                                                 #    
log_error = /data/mysql/mariadb.err                                 #    
pid-file = /data/mysql/mariadb.pid                                 #pid    
skip-external-locking                                                 #  mysql     ,           
key_buffer_size = 64M                                                 #        ,         64M
max_allowed_packet = 1M                                               #            ,             ,     16MB    ,           
table_open_cache = 256                                                #mysql      ,         table_open_cache   , MYSQL               ,       ,   64,     200     ,         200*N(N               );       ,                ,           ,    
sort_buffer_size = 1M                                                 #    order by group by      ,           ,   Using filesort,      ,                   ,   256k,          ,  128~256k,      using filesort   ,          
net_buffer_length = 8K                                                #         net_buffer_length  ,         max_allowed_packet  
read_buffer_size = 1M                                                 #           ,              ,          ,mysql                 ,              read_buffer_size , buffer                ,  buffer            ,       128k,       
read_rnd_buffer_size = 512K                                           #           ,              ,  ,          order by     ,                 ,   256k,       
myisam_sort_buffer_size = 16M                                         # myisam   repair table      ,        ,         "myisam_sort_buffer_size is to small"
thread_cache_size = 32                                                #   ,    。         ,       ,            ,         ,             ,          ,       ,           。
query_cache_size = 32M                                                #  select           。         select                  。              ,              ,                 。            ,     ,    query_cache_type=1,         。       ,      (query_cache_type=0)。
tmp_table_size = 64M                                                  #  HEAP        (     32M);                  MyISAM            。
                                                                      #
explicit_defaults_for_timestamp = true                            #         
#skip-networking                                                      #
max_connections = 500                                                 #            ,                  。   100,     512-1000  。   ,                        MySQL    。        100-200500-800     。              MySQL/MariaDB      。
max_connect_errors = 100                                              #        ,        ,                      ,            ,       ,          。          ,         (              )。
open_files_limit = 65535                                              #mysql       
                                                                      #
log-bin=mysql-bin                                                     #       datadir
binlog_format=mixed                                                   #    
server-id   = 1                                                       #             ID  ; n      1~2 32           。             ,Helloweba        。
expire_logs_days = 10                                                 #        ,       。                  ,                。     7~14 。
                                                                       #
default_storage_engine = InnoDB                                      #           (     MyISAM)。         –default-table-type     。
innodb_file_per_table = 1                                             #         ,                .ibd      。   .idb            。                 “TRUNCATE”       ,             ,            。                                。            I/O   。
innodb_data_home_dir = /data/mysql                                 #InnoDB   ,   InnoDB                     。       ,       MySQL     。
innodb_data_file_path = ibdata1:10M:autoextend                #    InnoDB        :            ;                    (B)、   (MB)     (GB)     ;                ;                autoextend         (max:n)。
innodb_log_group_home_dir = /data/mysql                           #    InnoDB         ( ib_logfile0、ib_logfile1 )。       ,InnoDB        MySQL                 。
innodb_buffer_pool_size = 256M                                       #     InnoDB         ,   128KB,             60%~70%。
innodb_log_file_size = 64M                                            #                 (     1MB)。
innodb_log_buffer_size = 8M                                           #           。InnoDB         ,      ,      Innodb Log Buffer ,   innodb_flush_log_trx_commit          (         ) ,        (       ) 。    innodb_log_buffer_size                。   8MB,   16~64MB  。
innodb_flush_log_at_trx_commit = 1                                 #                                     (    ”  ”)    。   0                    ,            ,         ;    1(    )           COMMIT             ,         ,            ;    2        ,       COMMIT       ,          。
innodb_lock_wait_timeout = 50                                         #         n (s)            ,   ROLLBACK        。              InnoDB                        。          50s。
                                                                      #
[mysqldump]                                                           #
quick                                                                 #
max_allowed_packet = 16M                                              #            
                                                                      #
[mysql]                                                               #
no-auto-rehash                                                        #
                                                                      #
[myisamchk]                                                           #
key_buffer_size = 64M                                                 #    
sort_buffer_size = 1M                                                 #    
read_buffer = 2M                                                      #
write_buffer = 2M                                                     #
                                                                      #
[mysqlhotcopy]                                                        #
interactive-timeout                                                   #
                                                

 
 
2、mysqlデータベースインデックスケース(百万級)
 [client]                                         
port    =3306                                    
socket    =/tmp/mysql.sock                       
                                                 
                                                 
                                                 
[mysqld]                                                                               
port        = 3306                                                           
socket      = /tmp/mysql.sock                                     
user    = mysql    
server_id  = 10
datadir  = /data/mysql/
old_passwords  = 1
lower_case_table_names  = 1
character-set-server  = utf8
default-storage-engine  = MYISAM
log-bin  = bin.log
log-error  = error.log
pid-file  = mysql.pid
long_query_time  = 2
slow_query_time  = 2
slow_query_log 
slow_query_log_file  = slow.log
binlog_cache_size  = 4MB
binlog_format  = mixed
max_binlog_cache_size  = 16MB
max_binlog_size  = 1GB
expire_logs_days  = 30
ft_min_word_len  = 4
back_log  = 512
max_allowed_packet  = 64MB
max_connections  = 4096
max_connect_errors  = 100
join_buffer_size  = 2MB
read_buffer_size  = 2MB
read_rnd_buffer_size  = 2MB
sort_buffer_size    = 2MB
query_cache_size  = 2MB
table_open_cache  = 10000
thread_cache_size  = 256
max_heap_table_size  = 64MB
tmp_table_size  = 64MB
thread_stack  = 192KB
thread_concurrency  = 24
local-infile  = 0
skip-show-database
skip-name-resolve
skip-external-locking
connect_timeout  = 600
interactive_timeout  = 600
wait_timeout  = 600
#MyISAM
key_buffer_size  = 512MB
bulk_insert_buffer_size  = 64MB
mysiam_sort_buffer_size  = 64MB
mysiam_max_sort_file_size  = 1GB
mysiam_repair_threads  = 1
concurrent_insert  = 2
myisam_recover
#INNODB
innodb_buffer_pool_size  = 64G
innodb_additional_mem_pool_size  = 32MB
innodb_data_file_path  = ibdata1:1G;ibdata2:1G:autoextend
innodb_read_io_threads  = 8
innodb_write_io_threads   = 8
innodb_file_per_table  = 1
innodb_flush_log_at_thx_commit  = 2
innodb_lock_wait_timeout  = 120
innodb_log_buffer_size  = 8MB
innodb_log_file_size  = 256MB
innodb_log_files_in_group  = 3
innodb_max_dirty_pages_pct  = 90
innodb_thread_concurrency  = 16
innodb_open_files  = 10000
#innodb_force_recovery  = 4
#replication slave
read-only
#skip-salve-start
relay-log  = relay.log
log-slave-updates



 
 
参考資料:
https://www.centos.bz/2018/02/mariadb-mysql%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6my-cnf%E8%A7%A3%E8%AF%BB/
『露出:Linux企業運維実戦』
 
転載は出典を明記してください.https://www.cnblogs.com/zhangxingeng/p/10882845.html