系統centos7
mariadb通過yum安裝
mysql配置文件位於/etc/my.cnf
常用參數:
1)max_connections設置最大連接(用戶)數,其默認值為100,設置太小會出現too many connections錯誤。例如,max_connections=1000
其修改方法有兩種,即時生效,查看當前max_connections值,select &&MAX_CONNECTIONS AS 'Max Connections';
臨時設置: set global max_connections=2000;
編輯my.conf:添加參數max_connections=200,不過需要重啟mysql才能生效
2)安全參數
max_connect_errors這個參數負責阻止客戶端嘗試暴力破解密碼,當某台主機錯誤連接次數達到該值時,該主機無法再嘗試登陸。解決方法是重啟mysql,或者把該值改大一點
3)設置數據包大小
max_allowed_packet設置server運行通信的最大數據包大小,如果該參數過小,可能導致比較大的insert或update執行失敗
常用命令
1)參看當前mysql連接情況
show status where variable_name like '%Thread%';
mysqld]
log-slow-queries=zhao #開啟慢查詢日志
long_query_time=5 #語句查詢時間查過這個值將會被記錄到慢查詢日志中
log=abp #通用查詢日志
log-bin=mysql-bin #二進制日志
character_set_server=utf8 #字符
collation-server=utf8_general_ci
init_connect='SET NAMES utf8' #設置utf8字符
#binlog_format=row
#skip-grant #忘記密碼時可以去掉這行的注釋,從而免驗證進入數據庫
datadir=/var/lib/mysql #數據目錄,類似於win系統上的data目錄
socket=/var/lib/mysql/mysql.sock #sock文件路徑
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log #錯誤日志
pid-file=/var/run/mariadb/mariadb.pid #pid文件路徑
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
另外還有幾個主要參數
(1)innodb_buffer_pool_size 緩存數據和索引的內存緩沖區大小,理論上該至越高,訪問數據需要的磁盤i/o就越少,建議設置為物理內存大小的70%~80%。該參數寫在my.cnf的[mysqld]里。修改之后重啟mysql生效,使用select @@innodb_buffer_pool_size/1024/1024查看(除以1024是因為要轉換為M)
(2)innodb_log_file_size 日志文件大小 值越大越節約磁盤I/O,但在崩潰回復時越慢。建議將日志文件大小設置為256MB或更大。該參數也是放在my.cnf的[mysqld]下。修改之后可能會無法啟動mysql,解決方法是,停止mysql,然后刪除mysql的日志文件,執行rm -f /var/lib/mysql/ib_logfile*。重啟mysql,使用select查看,修改成功
(3)innodb_flush_log_at_trx_commit這個值我還暫時搞不懂,建議設置為2。
(4)sync_binlog 建議設置為0