MySQL的安裝
我因為新的項目用的 gem 'mysql2', '0.4.5'
先馆,與我的 MySQL 5.7
不兼容奕枝,就重裝了MySQL。
但執(zhí)行 mysql -uroot
快鱼,報錯 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
執(zhí)行 ln -s /usr/local/var/mysql/mysql.sock /tmp/mysql.sock
后读第,
再執(zhí)行 mysql -uroot
曙博,報錯ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/var/mysql/mysql.sock' (2)
去/usr/local/var/mysql
目錄查看,發(fā)現(xiàn)根本沒有 mysql.sock
文件怜瞒。所以父泳,不只是沒有軟鏈接的問題。
又Google到了各種答案盼砍,各種嘗試后都還是不行尘吗。
最終搜到了以下答案,直接徹底刪除之前的MySQL浇坐,然后重裝:brew install mysql on mac os
brew remove mysql
brew cleanup
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
sudo rm -rf /usr/local/var/mysql
brew install mysql #安裝最新版本
brew install mysql@5.7 #安裝指定版本
unset TMPDIR
mysql.server start
mysql -uroot
我自己一開始的重裝出錯的原因睬捶,應(yīng)該就是沒有徹底刪除之前的MySQL。
執(zhí)行 rails console
時近刘,若報錯: Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.20.dylib (LoadError)
擒贸, 說明是重裝了MySQL臀晃,rails 找不到 mysql的相關(guān)文件了。這時需要依次執(zhí)行:
gem uninstall mysql2
gem install mysql
安裝 gem mysql2 的指定版本
比如Rails項目的Gemfile中使用的是 gem mysql2 0.3.18 版本介劫,執(zhí)行 gem install mysql2 -v '0.3.18',報錯:
Building native extensions. This could take a while...
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
/Users/huazai/.rvm/rubies/ruby-2.1.1/bin/ruby extconf.rb
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Setting rpath to /usr/local/Cellar/mysql/8.0.15/lib
-----
creating Makefile
make "DESTDIR=" clean
make "DESTDIR="
compiling client.c
client.c:431:3: error: use of undeclared identifier 'my_bool'
my_bool res = mysql_read_query_result(client);
^
client.c:433:19: error: use of undeclared identifier 'res'
return (void *)(res == 0 ? Qtrue : Qfalse);
^
client.c:762:3: error: use of undeclared identifier 'my_bool'
my_bool boolval;
^
client.c:793:7: error: use of undeclared identifier 'boolval'
boolval = (value == Qfalse ? 0 : 1);
^
client.c:794:17: error: use of undeclared identifier 'boolval'
retval = &boolval;
^
client.c:797:10: error: use of undeclared identifier 'MYSQL_SECURE_AUTH'; did you mean 'MYSQL_DEFAULT_AUTH'?
case MYSQL_SECURE_AUTH:
^~~~~~~~~~~~~~~~~
MYSQL_DEFAULT_AUTH
/usr/local/Cellar/mysql/8.0.15/include/mysql/mysql.h:188:3: note: 'MYSQL_DEFAULT_AUTH' declared here
MYSQL_DEFAULT_AUTH,
^
client.c:798:7: error: use of undeclared identifier 'boolval'
boolval = (value == Qfalse ? 0 : 1);
^
client.c:799:17: error: use of undeclared identifier 'boolval'
retval = &boolval;
^
client.c:830:38: error: use of undeclared identifier 'boolval'
wrapper->reconnect_enabled = boolval;
^
client.c:1196:38: error: use of undeclared identifier 'MYSQL_SECURE_AUTH'; did you mean 'MYSQL_DEFAULT_AUTH'?
return _mysql_client_options(self, MYSQL_SECURE_AUTH, value);
^~~~~~~~~~~~~~~~~
MYSQL_DEFAULT_AUTH
/usr/local/Cellar/mysql/8.0.15/include/mysql/mysql.h:188:3: note: 'MYSQL_DEFAULT_AUTH' declared here
MYSQL_DEFAULT_AUTH,
^
10 errors generated.
make: *** [client.o] Error 1
make failed, exit code 2
是因為 和 mysql 的高版本不兼容徽惋。需要卸載 MySQL重裝兼容的版本,比如 MySQL 5.7版座韵。
但安裝5.7版后再執(zhí)行 gem install mysql2 -v '0.3.18' --source 'https://gems.ruby-china.com/
又報錯:
Building native extensions. This could take a while...
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
/Users/huazai/.rvm/rubies/ruby-2.1.1/bin/ruby extconf.rb
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/huazai/.rvm/rubies/ruby-2.1.1/bin/ruby
--with-mysql-dir
--without-mysql-dir
--with-mysql-include
--without-mysql-include=${mysql-dir}/include
--with-mysql-lib
--without-mysql-lib=${mysql-dir}/lib
--with-mysql-config
--without-mysql-config
--with-mysql-dir
--without-mysql-dir
--with-mysql-include
--without-mysql-include=${mysql-dir}/include
--with-mysql-lib
--without-mysql-lib=${mysql-dir}/lib
--with-mysqlclientlib
--without-mysqlclientlib
--with-mlib
--without-mlib
--with-mysqlclientlib
--without-mysqlclientlib
--with-zlib
--without-zlib
--with-mysqlclientlib
--without-mysqlclientlib
--with-socketlib
--without-socketlib
--with-mysqlclientlib
--without-mysqlclientlib
--with-nsllib
--without-nsllib
--with-mysqlclientlib
--without-mysqlclientlib
--with-mygcclib
--without-mygcclib
--with-mysqlclientlib
--without-mysqlclientlib
extconf failed, exit code 1
也搜到了各種方法险绘,都沒解決問題∮辏可能是我重裝之前刪掉了一些不該刪除的文件宦棺。不知道以后還會不會帶來災難,哈哈哈哈哈哈哈哈哈哈哈
我是最后通過執(zhí)行gem install mysql2 -v '0.3.18' --source 'https://gems.ruby-china.com/' -- --with-mysql-config=/usr/local/Cellar/mysql@5.7/5.7.25/bin/mysql_config
才安裝成功了黔帕。
執(zhí)行 rails console
時代咸,若報錯:Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) (Mysql2::Error)
衩侥,執(zhí)行ln -s /usr/local/var/mysql/mysql.sock /tmp/mysql.sock
柳琢,如果報錯 ln: /tmp/mysql.sock: File exists
,可先將文件/tmp/mysql.sock
刪除壮虫,再執(zhí)行 ln -s /usr/local/var/mysql/mysql.sock /tmp/mysql.sock