Scroll to top
Khắc phục lỗi khi cài đặt gem mysql2 trên macOS

Khắc phục lỗi khi cài đặt gem mysql2 trên macOS

ByAdmin 2024-01-04 21:20
4 min read

Hướng dẫn khắc phục lỗi gặp phải khi cài đặt gem mysql2, một thư viện cung cấp kết nối giữa Ruby và cơ sở dữ liệu MySQL.

Trong quá trình phát triển ứng dụng bằng Ruby, việc cài đặt các gem (thư viện) là một bước không thể thiếu. Tuy nhiên, không phải lúc nào quá trình này cũng diễn ra suôn sẻ. Một ví dụ điển hình là lỗi gặp phải khi cài đặt gem mysql2, một thư viện cung cấp kết nối giữa Ruby và cơ sở dữ liệu MySQL.

Lỗi ld: library 'zstd' not found

Lỗi sẽ xuất hiện như sau:

Copy zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
sudo gem i mysql2
Building native extensions. This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    current directory: /Users/yuto/.anyenv/envs/rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/mysql2-0.5.5/ext/mysql2
/Users/yuto/.anyenv/envs/rbenv/versions/3.3.0/bin/ruby extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_gc_mark_movable()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_enc_interned_str() in ruby.h... yes
-----
Using --with-openssl-dir=/opt/homebrew/opt/openssl@3
-----
-----
Using mysql_config at /opt/homebrew/opt/mysql-client/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_DEFAULT_AUTH in mysql.h... yes
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.h... no
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /opt/homebrew/opt/mysql-client/lib
-----
creating Makefile

current directory: /Users/yuto/.anyenv/envs/rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/mysql2-0.5.5/ext/mysql2
make DESTDIR\= sitearchdir\=./.gem.20240103-5440-o6u9l5 sitelibdir\=./.gem.20240103-5440-o6u9l5 clean

current directory: /Users/yuto/.anyenv/envs/rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/mysql2-0.5.5/ext/mysql2
make DESTDIR\= sitearchdir\=./.gem.20240103-5440-o6u9l5 sitelibdir\=./.gem.20240103-5440-o6u9l5
compiling client.c
In file included from client.c:15:
./mysql_enc_name_to_ruby.h:43:1: warning: a function definition without a prototype is deprecated in all versions of C and is not supported in C2x [-Wdeprecated-non-prototype]
mysql2_mysql_enc_name_to_rb_hash (str, len)
^
./mysql_enc_name_to_ruby.h:86:1: warning: a function definition without a prototype is deprecated in all versions of C and is not supported in C2x [-Wdeprecated-non-prototype]
mysql2_mysql_enc_name_to_rb (str, len)
^
2 warnings generated.
compiling infile.c
compiling mysql2_ext.c
compiling result.c
result.c:304:35: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
        precision = field->length - (field->decimals > 0 ? 2 : 1);
                  ~ ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library 'zstd' not found
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Nguyên nhân

Lỗi này xuất phát từ việc thiếu thư viện cần thiết. Trong trường hợp này, zstdmysql-client là hai thư viện cần được cài đặt và cấu hình đúng cách.

Giải pháp

Sử dụng Homebrew, một trình quản lý gói phổ biến trên macOS, để cài đặt các thư viện cần thiết:

Copy zsh
1
brew install zstd mysql-client

Sau khi đã cài đặt các thư viện cần thiết, bạn cần chỉ định đường dẫn của chúng khi cài đặt gem mysql2:

Copy zsh
1
sudo gem install mysql2 -- --with-opt-dir=$(brew --prefix zstd):$(brew --prefix mysql-client)
Đánh giá bài viết: 5/5 (11 đánh giá)
Bạn chưa đánh giá

Bình luận

Author
hoclaptrinh.io author
Tác giả:Yuto Yasunaga

Mình là một full stack developer, tốt nghiệp và làm việc tại Nhật Bản. Trang web này là nơi mình tổng hợp, đúc kết và lưu trữ lại những kiến thức trong quá trình học và làm việc liên quan đến IT.
Hy vọng những bài viết ở website này sẽ có ích cho bạn.