「MySQL」MySQL構成および基本コマンド使用
21624 ワード
一、MySQL構成
まず、データベースにログインし、データベース情報を表示します.データベースにログインするコマンドは次のとおりです.
データベースのパスワードを入力するよう求められます.パスワードを入力すればいいです.ログインに成功すると、次のような出力が表示されます.
データベースエンコーディング文を表示するには、次の手順に従います.
実行結果は次のとおりです.
次に、データベースの永続的な文字符号化方法を設定します.MySQLのプロファイルパスは
次のような出力があります.
キーボードの
データベースに再ログインしてエンコーディングのステータスを表示するには、次の手順に従います.
その他cnfプロファイルの設定項目は以下の通りで、自分で設定を選択できます.
二、MySQL基本コマンド使用
1.データベースの選択(データベース切り替え)
結果:
2.すべてのデータベースの表示
結果:
3.すべてのユーザーの表示
結果:
4.ユーザー権限の表示
結果:
5.データベースのバージョンの表示
結果:
6.データベースでサポートされているエンジンの表示
結果:
7.データベース・データベースのデフォルト・ストレージ・エンジンの表示
結果:
8.現在選択されているデータベースの表示
結果:
9.現在選択されているデータベースのステータスの表示
結果:
よく使うまとめは、どんどん更新されていきます.
まず、データベースにログインし、データベース情報を表示します.データベースにログインするコマンドは次のとおりです.
mysql -u root -p
データベースのパスワードを入力するよう求められます.パスワードを入力すればいいです.ログインに成功すると、次のような出力が表示されます.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.23 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Commands end with ; or \g.
SQL文命令は;
または\g
で終わる.Your MySQL connection id is 10
現在時刻までのデータベース接続回数を記録している.Server version: 5.7.23 MySQL Community Server (GPL)
データベースバージョンType 'help;' or '\h' for help. Type '\c' to clear the current input statement
入力help
ヘルプ情報を取得し、入力\c
現在のコマンドをクリアします.データベースエンコーディング文を表示するには、次の手順に従います.
show variables like 'character%';
実行結果は次のとおりです.
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| 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)
次に、データベースの永続的な文字符号化方法を設定します.MySQLのプロファイルパスは
/etc/my.cnf
次のコマンドで編集できます(先に使用quit
またはexit
コマンドでデータベースを終了します):vim /etc/my.cnf
次のような出力があります.
[root@bogon ~]# vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
"/etc/my.cnf" 27L, 960C
キーボードの
i
を押して編集状態に入り、[mysqld]
ラベルの下に次の2行を追加します.# :
character_set_server=utf8
# : ,SET NAMES utf8
init_connect='SET NAMES utf8'
esc
キーを押して編集モードを終了し、:wq
を入力し、保存して終了します.次のコマンドを使用して、データベースを再起動します.service mysqld restart
データベースに再ログインしてエンコーディングのステータスを表示するには、次の手順に従います.
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
その他cnfプロファイルの設定項目は以下の通りで、自分で設定を選択できます.
# ,
[client]
#
port = 3306
# socket
socket = /data/mysqldata/3306/mysql.sock
#
default-character-set = utf8mb4
#
[mysqld]
#MySQL
port = 3307
# MySQL
socket = /data/mysqldata/3307/mysql.sock
#pid
pid-file = /data/mysqldata/3307/mysql.pid
# ( )
basedir = /usr/local/mysql-5.7.11
#
datadir = /data/mysqldata/3307/data
#MySQL
tmpdir = /data/mysqldata/3307/tmp
# ( )
character_set_server = utf8mb4
# ,
collation_server = utf8mb4_bin
#MySQL
user = mysql
#This variable applies when binary logging is enabled.
# It controls whether stored function creators can be trusted not to create stored functions that will cause
#unsafe events to be written to the binary log.
#If set to 0 (the default), users are not permitted to create or alter stored functions unless they have the SUPER
#privilege in addition to the CREATE ROUTINE or ALTER ROUTINE privilege. binlog , 1. binlog
log_bin_trust_function_creators = 1
# ,
performance_schema = 0
#secure_auth MySQL (<4.1) 。MySQL 5.6.7 secure_auth 1
secure_auth = 1
#
#ft_min_word_len = 1
# MySQL myisam
#myisam_recover
# null
explicit_defaults_for_timestamp
# ( )
event_scheduler
# ;External-locking MyISAM
skip-external-locking
# ; mysqld ,mysqld 。 。 , 。
# MySQL DNS 。 , , IP , MySQL !
skip-name-resolve
#MySQL IP
#bind-address = 127.0.0.1
# , --skip-slave-start , mysql
skip-slave-start
#The number of seconds to wait for more data from a master/slave connection before aborting the read. MySQL ,
slave_net_timeout = 30
# Master Slave , Master Slave ( )。
#Slave slave_net_timeout , , 。
#1. Slave_IO_Running,Slave_SQL_Running,Seconds_Behind_Master. pt-heartbeat 。
#2.slave_net_timeout , 。
# load data local infile。 local ,
local-infile = 0
# MySQL 。 MySQL , , ( ) 。
#back_log MySQL 。
back_log = 1024
#sql_mode, mysql sql , ! NO_AUTO_CREATE_USER: GRANT 。
#sql_mode = 'PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,NO_KEY_OPTIONS,NO_TABLE_OPTIONS,NO_FIELD_OPTIONS,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
#NO_ENGINE_SUBSTITUTION ,
sql_mode = NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER
# , MyISAM . , 。 16M, Key_read_requests
# Key_reads, key_buffer_size
key_buffer_size = 32M
# 。 net_buffer_length , max_allowed_packet 。
# 。 BLOB , 。
# server 。 max_allowed_packet , 。
max_allowed_packet = 512M
# ; , id, , thread_stack 。
thread_stack = 256K
# MySQL 。 ORDER BY , MySQL 。
# , sort_buffer_size 。
sort_buffer_size = 16M
# MySQL 。 ,MySQL 。read_buffer_size 。
# , , 。
read_buffer_size = 16M
# ( )Join ,MySQL Join (all/index join), Join “ ”
# , Join Buffer Join 。 Join Buffer ,MySQL Buffer , # Join Buffer Join Join , # Join Buffer , Buffer , 。 , IO , 。
join_buffer_size = 16M
# MySQL 。 ( , ), 。 ,MySQL , ,
# , , 。 MySQL , , 。
read_rnd_buffer_size = 32M
# 。 , , 。
#( , SQL 。 , , max_allowed_packet 。)
net_buffer_length = 16K
# MyISAM repair table , ; ” myisam_sort_buffer_size is too small”
myisam_sort_buffer_size = 128M
# 8M, MyISAM insert … select/ insert … values(…),(…) load data infile , cache , thread ;
# : MyISAM load , bulk_insert_buffer_size/myisam_sort_buffer_size/key_buffer_size
bulk_insert_buffer_size = 32M
#thread_cahe_size , 。 , , , ,
# , 。 , 。
thread_cache_size = 384
# : SELECT DB ,DB , SQL DB ,DB Client。
# , 。 , , 。
query_cache_size = 0
# 。 :0,1,2, off、on、demand。
query_cache_type = 0
# , 。( tmp_table_size max_heap_table_size 。)
# ,MySQL MyISAM , tmpdir
tmp_table_size = 1024M
# .# .
max_heap_table_size = 512M
#mysql
open_files_limit = 10240
#MySQL (SUPER) , , max_connections。
# MySQL max_connections+1;
# ( ) 16384, 16384, 16384 ;
# max_connections , 。 (CPU、 ) 、 ;
# ”Too many connections” ;
max_connections = 2000
# ,0 ;
max-user-connections = 0
#max_connect_errors MySQL , 。max_connect_errors 。
# 10 , MySQL , ( )10 , MySQL 。
max_connect_errors = 100000
# , /
table_open_cache = 5120
#interactive_time -- mysql ( mysql gui tool )
interactive_timeout = 86400
#wait_timeout -- MySQL
wait_timeout = 86400
#
# InnoDB , , , 。 , ? , ?
# , Nginx,, Nginx access log 。 , , Cache ?
# , Cache , —— ,Cache 。 ,Cache , :
# I/O, ; Cache , , 。
binlog_cache_size = 16M
#
slow_query_log = 1
# 1s;MySQL long_query_time SQL , 。
long_query_time = 1
# index
log-slow-admin-statements
log-queries-not-using-indexes
# *** Replication related settings ***
# : 。 , , binlog 。
# MySQL 5.1.12 , : SQL (statement-based replication, SBR), (row-based replication, RBR), (mixed-based replication, MBR)。 ,binlog :STATEMENT,ROW,MIXED。MBR ,SBR 。
binlog_format = ROW
# session , 。
#max_binlog_cache_size = 102400
# ,binlog
log-bin = /data/mysqldata/3307/binlog/mysql-bin
log-bin-index = /data/mysqldata/3307/binlog/mysql-bin.index
#relay-log I/O ,
# SQL relay-log
#binlog relaylog , slave sql relaylog 。
relay-log = /data/mysqldata/3307/relay/mysql-relay-bin
relay-log-index = /data/mysqldata/3307/relay/mysql-relay-bin.index
# ID,
server_id = 100
#log_slave_updates 。
log_slave_updates = 1
# 。 0, “ ”。 。
expire-logs-days = 15
# , 。 1GB 4096 。 1GB。
max_binlog_size = 512M
#replicate-wild-ignore-table , replicate-do-db replicate-ignore-db
replicate-wild-ignore-table = mysql.%
# Table
#replicate-wild-do-table = db_name.%
# ; , 。 ,MYSQL ,
# 。
#slave-skip-errors = 1062,1053,1146
# , ,
auto_increment_offset = 1
auto_increment_increment = 2
# :mysql.slave_realy_log_info
relay_log_info_repository = TABLE
# master :mysql.salve_master_info
master_info_repository = TABLE
# ; slave , relay-log , , relay-log,
# master , relay-log
relay_log_recovery = on
# *** innodb setting ***
#InnoDB 。 I/O。
innodb_buffer_pool_size = 4G
#
innodb_data_file_path = ibdata1:1G:autoextend
# commit 。 1, , 。
# , , 0 , I/O。 0。
innodb_flush_log_at_trx_commit = 0
#sync_binlog=n, n ,MySQL fsync binlog_cache 。
#sync_binlog = 1000
# CPU , innodb_read_io_threads innodb_write_io_threads IO ,
innodb_read_io_threads = 8
innodb_write_io_threads = 8
#Innodb Plugin , :Antelope、Barracuda 。 Barracuda Antelope 。
innodb_file_format = Barracuda
# Innodb
innodb_open_files = 65536
# 。
innodb_purge_threads = 1
#
innodb_support_xa = FALSE
#InnoDB 。 1M 8M。 (commit)。
# , , I/O。
innodb_log_buffer_size = 256M
# ( MB)。 n , 1M (buffer pool) 1/n。 ,
# , I/O。 。
innodb_log_file_size = 1G
#
innodb_log_files_in_group = 3
# (rooled back) ,InnoDB ( )
#innodb_lock_wait_timeout = 120
#innodb_max_dirty_pages_pct : Innodb , 1-100, 90. :
# Innodb , swap , , swap 。 : 90%, 15%。
# , , , , 。
innodb_max_dirty_pages_pct = 75
#innodb_buffer_pool_size , hash , 。
innodb_buffer_pool_instances = 4
# Innodb checkpoint IO
innodb_io_capacity = 500
# : Innodb , 。 。
# : 。 DB 。
innodb_file_per_table = 1
# / ( IO), insert buffer ,
# , merge 。 , merge
innodb_change_buffering = inserts
# , , ;
innodb_adaptive_flushing = 1
# ,
transaction-isolation = READ-COMMITTED
#innodb_flush_method innodb redo log 、
#InnoDB O_DIRECT , fsync() 。
innodb_flush_method = O_DIRECT
# 1. 0: Innodb ; 1: InnoDB 。
#innodb_use_sys_malloc = 1
[mysqldump]
# mysqldump
quick
# server ; mysql
max_allowed_packet = 512M
#TCP/IP , net_buffer_length
net_buffer_length = 16384
[mysql]
#auto-rehash
auto-rehash
#isamchk
[isamchk]
key_buffer = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
# myisamchk 、
[myisamchk]
key_buffer = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
#mysqlhotcopy lock tables、flush tables cp scp . , , MyISAM .
interactive-timeout
PS: , !
二、MySQL基本コマンド使用
1.データベースの選択(データベース切り替え)
# : use
use mysql;
結果:
mysql> 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
2.すべてのデータベースの表示
show databases;
結果:
mysql> show Databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
3.すべてのユーザーの表示
select host, user from user;
結果:
mysql> select host, user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | admin |
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
5 rows in set (0.01 sec)
4.ユーザー権限の表示
show grants for admin;
結果:
mysql> show grants for admin;
+--------------------------------------------------------------+
| Grants for admin@% |
+--------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION |
+--------------------------------------------------------------+
1 row in set (0.00 sec)
5.データベースのバージョンの表示
select version();
結果:
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.23 |
+-----------+
1 row in set (0.00 sec)
6.データベースでサポートされているエンジンの表示
show engines;
結果:
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
PS:Support YES, ,NO, ,DEFAULT
7.データベース・データベースのデフォルト・ストレージ・エンジンの表示
show variables like 'default_storage_engine';
結果:
mysql> show variables like 'default_storage_engine';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| default_storage_engine | InnoDB |
+------------------------+--------+
1 row in set (0.00 sec)
8.現在選択されているデータベースの表示
select database();
結果:
mysql> select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)
9.現在選択されているデータベースのステータスの表示
status;
結果:
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using EditLine wrapper
Connection id: 2
Current database: mysql
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.23 MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 2 hours 20 min 24 sec
Threads: 1 Questions: 180 Slow queries: 0 Opens: 138 Flush tables: 1 Open tables: 131 Queries per second avg: 0.021
--------------
よく使うまとめは、どんどん更新されていきます.