MySQL 多層的目錄階層 一次查詢完成

資料庫設計分類、目錄等等的結構,要去考量有幾個階層,當階層不明確,或者階層很多,通常會用下述設計方式(想要幾個階層都可以):

  • cate_id / cate_name / cate_parent_id (預設 cate_parent_id = 0)

依照這種設計法,每次查詢就一直找到自己的父節點id,當找到 cate_parent_id = 0 就找到最上層了

這種作法就會需要寫個 while 一直往上查詢上去,有沒有辦法一行 SQL 語法直接達成呢?

閱讀全文〈MySQL 多層的目錄階層 一次查詢完成〉

MySQL my.cnf 如何做語法檢查(syntax check)

Apache2 設定檔設好後,重新啟動前,都會做語法檢查:apache2ctl configtest,若有錯誤可以重新啟動前即時修正。

但是 MySQL 要怎麼做語法檢查呢?

閱讀全文〈MySQL my.cnf 如何做語法檢查(syntax check)〉

MySQL InnoDB 遇到 ./ib_logfile0 is of different size 5242880 bytes 修復

MySQL 使用 Innobackupex 來做備份,詳見此篇:使用 Percona innobackupex 備份 與 還原 MySQL

執行下述命令:

  • $ sudo innobackupex --user=root --password=password /home/user/2017-01-01/

出現下述錯誤訊息:

InnoDB: Error: log file ./ib_logfile0 is of different size 5242880 bytes

註:原始 DB 是 Debian 預設的 MySQL

修復方式如下步驟:

  1. vi /etc/mysql/my.cnf # 增加 innodb_log_file_size 的參數即可
    [mysql]
    innodb_log_file_size=5M
  2. /etc/init.d/mysql reload
  3. 再來 innobackupex 就可以正常執行了

使用 Percona innobackupex 備份 與 還原 MySQL

MySQL 的 InnoDB 只能用 mysqldump,而沒辦法使用 cp 來備份,這點可以使用 Percona 出的 innobackupex 來解決~

Percona 出很多 MySQL 的工具,可以做同步、快速備份 等等 ..

閱讀全文〈使用 Percona innobackupex 備份 與 還原 MySQL〉

MySQL 出現 InnoDB STORAGE ENGINE failed 修復

MySQL 要從備份還原啟動,但是發生啟動失敗,在 mysql 的 error log 裡面看到下述訊息。

Unable to start MySQL service: Plugin 'InnoDB' registration as a STORAGE ENGINE failed.

InnoDB: Using Linux native AIO
InnoDB: Initializing buffer pool, size = 128.0M
InnoDB: Completed initialization of buffer pool
InnoDB: Error: log file ./ib_logfile0 is of different size 0 50331648 bytes
InnoDB: than specified in the .cnf file 0 5242880 bytes!
[ERROR] Plugin 'InnoDB' init function returned error.
[ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
[ERROR] Unknown/unsupported storage engine: InnoDB

170122 12:01:00  InnoDB: Warning: table 'mysql/innodb_index_stats' InnoDB: in InnoDB data dictionary has unknown flags 50.
170122 12:01:00  InnoDB: Warning: table 'test/collect' InnoDB: in InnoDB data dictionary has unknown flags 50.
170122 12:01:00 [ERROR] /usr/sbin/mysqld: Table './mysql/user' is marked as crashed and should be repaired
170122 12:01:00 [Warning] Checking table:   './mysql/user'
170122 12:01:00 [ERROR] 1 client is using or hasn't closed the table properly
170122 12:01:00  InnoDB: Warning: table 'test/users'
InnoDB: in InnoDB data dictionary has unknown flags 50.

要怎麼修復呢?

閱讀全文〈MySQL 出現 InnoDB STORAGE ENGINE failed 修復〉

MySQL 新增、移除 Primay Key 語法

MySQL 使用 ALTER Table 的語法蠻直覺的,如下:

  • ALTER TABLE table_name ADD COLUMN column_name char(1) not null default 'y' comment 'xxx'; # 預設加在最後面,若要指定加在哪裡,需要用 AFTER 的命令
  • ALTER TABLE table_name ADD COLUMN column_name char(1) not null default 'y' comment 'xxx' AFTER column_name;
  • ALTER TABLE table_name DROP COLUMN column_name;
  • ALTER TABLE table_name MODIFY COLUMN column_name char(1) not null default 'y' comment 'xxx';

在 Primay Key 的沒有「修改」的語法,想要修改得用刪除再新增的方法來達成,如下範例:

  • ALTER TABLE table_name DROP PRIMARY KEY, ADD PRIMARY KEY (`pk_1`, `pk_2`);

Percona DB 遇到 InnoDB MEMCACHED: Memcached uses atomic increment 修正

Debian Linux 升級 Percona DB 5.7 (MySQL),遇到下述的警告訊息:

  • InnoDB MEMCACHED: Memcached uses atomic increment
  • InnoDBMemcached: Unable to open table 'innodbmemcache/containers'
  • InnoDBMemcached: Please create config table'containers' in database
    'innodb
    memcache' by running 'innodbmemcachedconfig.sql. error Table not found'
  • Failed to initialize instance. Error code: 13

解法:

  1. mysql -u root -p
  2. source /usr/share/mysql/innodb_memcached_config.sql;

於 Ubuntu、Debian 安裝 Percona Server 簡單步驟

MySQL Percona 相信大家已經不陌生,安裝步驟於 2012、2013年也寫過下述幾篇:

不過現在有更簡單的安裝方式了,所以來更新更簡單的安裝方式

閱讀全文〈於 Ubuntu、Debian 安裝 Percona Server 簡單步驟〉