ผมพบปัญหาการใช้งาน MySQL ร่วมกับ ERPNext ผมจึงเปลี่ยนมา ติดตั้ง MariaDB บน Mac แทน และถอนการติดตั้ง MySQL ออกจาก Mac เพื่อให้ Debugging ง่ายขึ้น
MySQL บน Mac
ถ้าติดตั้ง MySQL บน Mac มันจะโชว์อยู่ที่ System Preferences

คลิ๊กตรงรูปปลาโลมา จะเข้ามาที่หน้าจอนี้ครับ เลือกแท็บ Instances จะเห็นปุ่ม Start/Stop MySQL Server

แท็บ Configuration จะแสดงรายละเอียดเกี่ยวกับไฟล์ต่าง ๆ โดยค่า Default จะเป็นตามรูปสีเข้ม คือจะไม่มี Configuration File

รูปนี้ผมเพิ่ม Configuration File เข้าไป

ถอนการติดตั้ง MySQL
ไปที่แท็บ Instances คลิ๊กปุ่ม Stop MySQL Server แล้วคลิ๊กปุ่ม Uninstall

ล้างไฟล์ที่เกี่ยวข้อง
การมีทั้ง MySQL และ MariaDB อยู่บนเครื่อง Mac ผมพบปัญหาจุกจิกหลายประการ ดังนั้นเมื่อถอน MySQL ออกจากเครื่องแล้ว ก็ควรลบไฟล์ที่เกี่ยวข้องทิ้งไปด้วยครับ ลองใช้คำสั่งต่อไปนี้ลบไฟล์ หรือโฟลเดอร์ ที่เกี่ยวข้องครับ
sudo rm -rf /usr/local/mysql
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /private/var/db/receipts/*mysql*
ติดตั้ง MariaDB
ผมคิดว่าถ้าจะพัฒนาโปรแกรม ERPNext บนเครื่อง Mac ผมควรเลือกแค่ตัวเดียวระหว่าง MariaDB หรือ MySQL คนที่สร้าง ERPNext เขาแนะนำ MariaDB ผมเคยลองติดตั้งทั้ง MariaDB และ MySQL บนเครื่อง Mac ผมรู้สึกว่าเครื่องมันทำงานรวนๆ ทำ Debugging ยาก การติดตั้ง ERPNext App ค่อนข้างมีปัญหา มันฟ้อง Error ตอนสร้างฐานข้อมูล เช่น


Homebrew MariaDB
จะติดตั้งด้วยคำสั่ง
brew install mariadb
❯ brew install mariadb
Updating Homebrew...
==> Auto-updated Homebrew!
Updated Homebrew from c5de106c2 to 0e63fb820.
Updated 4 taps (homebrew/core, homebrew/cask, homebrew/services and snyk/tap).
==> New Formulae
opensearch-dashboards pyoxidizer
==> Updated Formulae
Updated 66 formulae.
==> Updated Casks
Updated 30 casks.
==> Downloading https://ghcr.io/v2/homebrew/core/mariadb/manifests/10.6.4
Already downloaded: /Users/manotlj/Library/Caches/Homebrew/downloads/bd7a1ecf2f1d64a655dc2f802451f80b56f655802849b44546931d89e6b7475f--mariadb-10.6.4.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/mariadb/blobs/sha256:618039a83fbea4ad919c9062482793ae70103402644fe6466e868
Already downloaded: /Users/manotlj/Library/Caches/Homebrew/downloads/5fc297b88528bedb08253eb9201d9c36bc3d913720ace6eaa764618aad3ba495--mariadb--10.6.4.big_sur.bottle.tar.gz
==> Pouring mariadb--10.6.4.big_sur.bottle.tar.gz
==> /usr/local/Cellar/mariadb/10.6.4/bin/mysql_install_db --verbose --user=manotlj --basedir=/usr/local/Cellar/mariadb/10.6
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.
MySQL is configured to only allow connections from localhost by default
To have launchd start mariadb now and restart at login:
brew services start mariadb
Or, if you don't want/need a background service you can just run:
mysql.server start
==> Summary
🍺 /usr/local/Cellar/mariadb/10.6.4: 902 files, 183MB
คำสั่ง MariaDB ที่ใช้บ่อย
ขั้นตอนนี้มีจุดสนใจอยู่ 2 จุด
- Start MariaDB ทุกครั้งที่เปิดเครื่อง Mac ให้ใช้คำสั่ง
brew services start mariadb
คำสั่งอื่นๆ ที่ใช้บ่อย
brew services list
brew services restart mariadb
brew services stop mariadb
- แต่ถ้าต้องการ Start MariaDB เมื่อต้องการใช้งาน ให้ใช้คำสั่ง
mysql.server start
คำสั่งอื่นๆ ที่ต้องใช้ร่วมกัน
mysql.server stop
mysql.server restart
mysql.server status
ทำความรู้จัก MariaDB
เมื่อติดตั้งเสร็จ และ MariaDB Start เรียบร้อย ลองถามเครื่องดูว่าตอนนี้ใช้ MySQL เวอร์ชั่นไหน
mysql --version
❯ mysql --version
mysql Ver 15.1 Distrib 10.6.4-MariaDB, for osx10.16 (x86_64) using readline 5.1
ทำงานที่ไหน
which MySQL
❯ which MySQL
/usr/local/bin/MySQL
ข้อมูลส่วนตัวมีอะไรบ้าง
brew info mariadb
❯ brew info mariadb
mariadb: stable 10.6.4 (bottled)
Drop-in replacement for MySQL
MariaDB Foundation
Conflicts with:
mariadb-connector-c (because both install `mariadb_config`)
mysql (because mariadb, mysql, and percona install the same binaries)
mytop (because both install `mytop` binaries)
percona-server (because mariadb, mysql, and percona install the same binaries)
/usr/local/Cellar/mariadb/10.6.4 (902 files, 183MB) *
Poured from bottle on 2021-08-14 at 11:08:30
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/mariadb.rb
License: GPL-2.0-only
==> Dependencies
Build: bison ✘, cmake ✘, pkg-config ✔
Required: groonga ✔, [email protected] ✔, pcre2 ✔
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.
MySQL is configured to only allow connections from localhost by default
To have launchd start mariadb now and restart at login:
brew services start mariadb
Or, if you don't want/need a background service you can just run:
mysql.server start
==> Analytics
install: 13,939 (30 days), 37,514 (90 days), 166,591 (365 days)
install-on-request: 13,917 (30 days), 37,465 (90 days), 165,577 (365 days)
build-error: 0 (30 days)
ก็คล้ายๆสัมภาษณ์งานแหละครับ เมื่อทำความรู้จักกันเรียบร้อยแล้ว ก็เริ่มทำขั้นตอนต่อไปครับ
ติดตั้งฐานข้อมูล
ต่อไปให้ติดตั้งฐานข้อมูล
mysql_install_db
❯ mysql_install_db
WARNING: The host 'Manots-iMac.local' could not be looked up with /usr/local/Cellar/mariadb/10.6.4/bin/resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MariaDB version. The MariaDB daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MariaDB privileges !
mysql.user table already exists!
Run mysql_upgrade, not mysql_install_db
Upgrade MariaDB
ข้อมูลที่ได้หลังจากใช้คำสั่ง mysql_install_db คือ ตาราง mysql.user มีอยู่แล้ว ต่อไปให้สั่ง
mysql_upgrade
❯ mysql_upgrade
Version check failed. Got the following error when calling the 'mysql' command line client
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
FATAL ERROR: Upgrade failed
Error และวิธีแก้
ขั้นตอนการใช้คำสั่ง mysql_upgrade เราอาจเจอ ERROR: Access denied for user ‘root’@’localhost’
ให้แก้ Error ดังนี้ครับ
- ในกรณีที่เรากำหนดรหัสผ่านให้กับ User root ใช้คำสั่ง
mysql -u root
- หรือเพิ่งติดตั้งใหม่ ยังไม่ได้กำหนดรหัสผ่านใดๆ ใช้คำสั่ง
mysql
ถ้าจำไม่ได้ก็ไม่เป็นไรครับ ลองใช้ทั้งสองคำสั่งดู
❯ mysql -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
❯ mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 10.6.4-MariaDB Homebrew
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)]> select Host, User, Password from mysql.user;
+-------------------+-------------+-------------------------------------------+
| Host | User | Password |
+-------------------+-------------+-------------------------------------------+
| localhost | mariadb.sys | |
| localhost | root | invalid |
| localhost | manotlj | invalid |
| localhost | | |
| manots-imac.local | | |
| % | root | *BA54F16D5528DD88024D6C02E7B82F67665072EF |
+-------------------+-------------+-------------------------------------------+
6 rows in set (0.148 sec)
MariaDB [(none)]>
จากโค้ดด้านบนผมลองใช้ mysql -u root แล้วไม่ผ่าน ผมจึงเปลี่ยนเป็น mysql ก็สามารถเข้ามาที่ MariaDB monitor ได้ เพื่อความแน่ใจ ผมจะเช็คดูว่า User root มีรหัสผ่านหรือยัง โดยใช้คำสั่ง
select Host, User, Password from mysql.user;
MariaDB ก็จะตอบมา ตามที่แสดงในโค้ดด้านบนครับ ตรงคอลัมน์ Password แสดงผลเป็น invalid แสดงว่าผมยังไม่ได้สร้างรหัสผ่านให้ User root
สร้างรหัสผ่านให้ User root
เมื่อยังไม่ได้สร้างรหัสผ่าน ไหนๆก็เข้ามาที่ MariaDB แล้ว ก็สร้างเสียเลยครับ ตอนนี้เคอร์เซอร์จะอยู่ที่ MariaDB [(none)]>
พิมพ์คำสั่ง
UPDATE mysql.user SET password = PASSWORD('your-password') WHERE user = 'root';
MariaDB [(none)]> UPDATE mysql.user SET password = PASSWORD('your-password') WHERE user = 'root';
ERROR 1356 (HY000): View 'mysql.user' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
MariaDB [(none)]> select Host, User, Password from mysql.user;
+-------------------+-------------+-------------------------------------------+
| Host | User | Password |
+-------------------+-------------+-------------------------------------------+
| localhost | mariadb.sys | |
| localhost | root | invalid |
| localhost | manotlj | invalid |
| localhost | | |
| manots-imac.local | | |
| % | root | *BA54F16D5528DD88024D6C02E7B82F67665072EF |
+-------------------+-------------+-------------------------------------------+
6 rows in set (0.001 sec)
ปรากฏว่ามันฟ้อง ERROR: View ‘mysql.user’ reference invalid table(s) or column(s) …
ไม่เป็นไรครับลองใช้คำสั่งใหม่
ALTER USER 'root'@'localhost' IDENTIFIED BY 'your-password';
FLUSH PRIVILEGES;
MariaDB [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY 'your-password';
Query OK, 0 rows affected (0.055 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> select Host, User, Password from mysql.user;
+-------------------+-------------+-------------------------------------------+
| Host | User | Password |
+-------------------+-------------+-------------------------------------------+
| localhost | mariadb.sys | |
| localhost | root | *BA54F16D5528DD88024D6C02E7B82F67665072EF |
| localhost | manotlj | invalid |
| localhost | | |
| manots-imac.local | | |
| % | root | *BA54F16D5528DD88024D6C02E7B82F67665072EF |
+-------------------+-------------+-------------------------------------------+
6 rows in set (0.002 sec)
MariaDB [(none)]> exit
ครั้งนี้สำเร็จครับ โอเคครับตอนนี้ User root ของเรามีรหัสผ่านแล้ว พิมพ์คำสั่ง exit ออกมาจาก MariaDB แล้วรีสตาร์ท MariaDB ครับ
brew services restart mariadb
❯ brew services restart mariadb
Stopping `mariadb`... (might take a while)
==> Successfully stopped `mariadb` (label: homebrew.mxcl.mariadb)
==> Successfully started `mariadb` (label: homebrew.mxcl.mariadb)
หลังรีสตาร์ท ตอนนี้ลองใช้คำสั่งแบบไม่ต้องใช้รหัสผ่าน
❯ mysql
ERROR 2002 (HY000): Can't connect to local server through socket '/tmp/mysql.sock' (2)
จะพบ ERROR: Can’t connect to local server through socket ‘/tmp/mysql.sock’ (2)
แสดงว่า MariaDB มันถูกปกป้องด้วยรหัสผ่านเรียบร้อยแล้วครับ เปลี่ยนเป็นคำสั่ง
mysql -u root -p
❯ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.6.4-MariaDB Homebrew
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)]>
เมื่อใส่รหัสผ่านเราก็สามารถเข้ามาที่ MariaDB monitor ได้ตามที่คาดครับ ถ้าใช้คำสั่งขอดูรหัสผ่าน
select Host, User, Password from mysql.user;
MariaDB [(none)]> select Host, User, Password from mysql.user;
+-------------------+-------------+-------------------------------------------+
| Host | User | Password |
+-------------------+-------------+-------------------------------------------+
| localhost | mariadb.sys | |
| localhost | root | *BA54F16D5528DD88024D6C02E7B82F67665072EF |
| localhost | manotlj | invalid |
| localhost | | |
| manots-imac.local | | |
| % | root | *BA54F16D5528DD88024D6C02E7B82F67665072EF |
+-------------------+-------------+-------------------------------------------+
6 rows in set (0.010 sec)
MariaDB [(none)]>
ให้สังเกตตรงคอลัมน์ Password ของ User root จะมีตัวอักษรยาวๆ แทนคำว่า invalid แสดงว่า root มีรหัสผ่านแล้วครับ
Password ที่เก็บไว้บนฐานข้อมูลจะถูก Encrypted ไว้
เราจึงมองเห็นเป็นตัวอักษรยาวๆ
mysql_upgrade อีกครั้ง
หลังจากเราสร้างรหัสผ่านให้ root และรีสตาร์ท MariaDB เรียบร้อยแล้ว ให้สั่ง mysql_upgrade อีกครั้ง
mysql_upgrade -u root -p
❯ mysql_upgrade -u root -p
Enter password:
Phase 1/7: Checking and upgrading mysql database
Processing databases
mysql
mysql.column_stats OK
mysql.columns_priv OK
mysql.db OK
mysql.event OK
mysql.func OK
...
...
...
sys.x$waits_global_by_latency OK
Phase 4/7: Running 'mysql_fix_privilege_tables'
Phase 5/7: Fixing table and database names
Phase 6/7: Checking and upgrading tables
Processing databases
information_schema
performance_schema
sys
sys.sys_config OK
test
Phase 7/7: Running 'FLUSH PRIVILEGES'
OK
~ 1m 6s
❯
หลังจากใส่รหัสผ่าน เครื่องก็จะอัพเกรด MariaDB ให้ครับ
my.cnf สำหรับ ERPNext
เปิดไฟล์ my.cnf จากโฟลเดอร์ /usr/local/etc/my.cnf ด้วยคำสั่ง
sudo nano /usr/local/etc/my.cnf
*** เรารู้ได้อย่างไรว่าไฟล์ my.cnf อยู่ที่นี่ ลองใช้คำสั่งนี้ดูครับ
mariadb --help
❯ mariadb --help
mariadb Ver 15.1 Distrib 10.6.4-MariaDB, for osx10.16 (x86_64) using readline 5.1
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Usage: mariadb [OPTIONS] [database]
Default options are read from the following files in the given order:
/usr/local/etc/my.cnf ~/.my.cnf
The following groups are read: mysql mariadb-client client client-server client-mariadb
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit.
--no-defaults Don't read default options from any option file.
The following specify which files/extra groups are read (specified before remaining options):
--defaults-file=# Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.
--defaults-group-suffix=# Additionally read default groups with # appended as a suffix.
-?, --help Display this help and exit.
...
...
...
select-limit 1000
max-join-size 1000000
secure-auth FALSE
show-warnings FALSE
plugin-dir (No default value)
default-auth (No default value)
binary-mode FALSE
connect-expired-password FALSE
~
❯
ตรงใต้บรรทัด Default options are read from …
MariaDB จะใช้ Configuration file จาก “/usr/local/etc/my.cnf” ก่อน ถ้าไม่มีก็จะมองหาที่ “~/.my.cnf”
เมื่อเปิดเข้าไปที่ my.cnf แล้วให้เพิ่มโค้ดนี้เข้าไปครับ
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
[mysql]
default-character-set = utf8mb4
วิธีแก้ Error
ในกรณีที่พบปัญหา Table ‘tabDefaultValue’ missing…
ERROR 4047 (HY000) at line 10: InnoDB refuses to write tables with ROW_FORMAT=COMPRESSED or KEY_BLOCK_SIZE. Table 'tabDefaultValue' missing...
❯ bench new-site bunchee.local
MySQL root password:
ERROR 4047 (HY000) at line 10: InnoDB refuses to write tables with ROW_FORMAT=COMPRESSED or KEY_BLOCK_SIZE.
Table 'tabDefaultValue' missing in the restored site. Database not installed correctly, this can due to lack of permission, or that the database name exists. Check your mysql root password, validity of the backup file or use --force to reinstall
เพิ่มโค้ดนี้เข้าไปใน my.cnf ครับ
innodb_read_only_compressed = FALSE
หลังแก้ไข จะได้ไฟล์ my.cnf หน้าตาแบบนี้ครับ
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
# Fixed Table 'tabDefaultValue' missing
innodb_read_only_compressed = FALSE
[mysql]
default-character-set = utf8mb4
สรุป
เรา ติดตั้ง MariaDB บน Mac ด้วยคำสั่ง
brew install mariadb
จากนั้นเราสามารถใช้ MariaDB ได้เลย ด้วยคำสั่ง mysql
แต่ถ้าจะใช้งานร่วมกับ ERPNext ให้กำหนดรหัสผ่านให้กับ root ด้วยคำสั่ง
❯ mysql
MariaDB [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY 'your-password';
MariaDB [(none)]> FLUSH PRIVILEGES;
❯ brew services restart mariadb
หลังจากรีสตาร์ท MariaDB ให้ใช้คำสั่ง
❯ mysql -u root -p
Enter password:
ตรวจดูว่า User root มีรหัสผ่านหรือยัง ด้วยคำสั่ง
MariaDB [(none)]> select Host, User, Password from mysql.user;