SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW()) - TO_DAYS('1788-11-26')), '%Y') + 0;
原理:日期差轉天數 → FROM_DAYS 得基準年 → 取年份 → 隱式轉數值。
DELETE t1, t2
FROM class t1
INNER JOIN stuscore t2 ON t1.classid = t2.classid
WHERE t1.classid IN (1, 2);
ALTER TABLE stuscore ADD COLUMN classid INT NOT NULL AFTER course;
FLUSH STATUS; -- 將 Threads_created 等計數器歸零
SHOW STATUS LIKE '%thread%';
關鍵指標:
Threads_connected:當前連接數;Threads_created:歷史創建線程數,過大說明 thread_cache_size 過小;Threads_running:活躍線程數。/usr/mysql/bin/mysqladmin -h 192.168.0.3 -u yangsq -p password
注意:-h 不支持通配符 %,需具體 IP。
/usr/mysql/bin/mysqladmin --port 3306 shutdown
CREATE USER 'u'@'%' IDENTIFIED BY 'pwd'; 等價於 GRANT USAGE ON *.* TO 'u'@'%';;information_schema 和 test 庫;REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'root'@'127.0.0.1'; 後仍保留 GRANT USAGE ON *.* TO 'root'@'127.0.0.1';。# -n 非空為真,但必須雙引號包裹變量防止語法錯誤
if [ -n "$var" ]; then echo "non-empty"; fi
| 檢查項 | 命令/腳本 | 告警閾值建議 |
|---|---|---|
| 連接數使用率 | SHOW GLOBAL STATUS LIKE 'Threads_connected'; / max_connections | > 80% |
| 慢查詢增長 | SHOW GLOBAL STATUS LIKE 'Slow_queries'; | 分鐘級增長 > 10 |
| InnoDB 行鎖等待 | SHOW ENGINE INNODB STATUS \G 關注 Row locks | 平均等待 > 50ms |
| 磁碟空間 | df -h / information_schema.tables.data_length | > 85% |
| Replication 延遲 | SHOW SLAVE STATUS \G Seconds_Behind_Master | > 30s |
[mysqld]
innodb_buffer_pool_size = 1G # 物理內存 60-70%
innodb_log_file_size = 128M # 大事務支撐
innodb_log_buffer_size = 4M
innodb_flush_log_at_trx_commit = 1 # 數據安全
max_connections = 1500
thread_cache_size = 100
join_buffer_size = 128M
sort_buffer_size = 10M
read_rnd_buffer_size = 2M
event_scheduler = ON
explicit_defaults_for_timestamp = ON
lower_case_table_names = 1