Mac下go安装,环境搭建(包含MySQL),项目运行
一、环境搭建
https://go.dev/dl/
查看是arm64,还是x86-64命令:
-uname -a
我的是
Darwin okerdeMacBook-Pro.local 23.5.0 Darwin Kernel Version 23.5.0: Wed May 1 20:13:18 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6030 arm64
安装后,重新打开终端
输入命令:go env
GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/oker/Library/Caches/go-build'
GOENV='/Users/oker/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/oker/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/oker/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.0'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/oker/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/yb/z1bgqqd50dd2b_y9_bfybyb40000gn/T/go-build1945541292=/tmp/go-build -gno-record-gcc-switches -fno-common'
二、完整运行一个项目
安装MySQL
地址:https://dev.mysql.com/downloads/mysql/
go mod tidy
make build
make run
brew不支持mysql 5.7的安装了
brew install mysql@8.0
在外面启动:
brew services restart mysql@8.0
brew services stop mysql@8.0
brew services start mysql@8.0
/etc/my.cnf /etc/mysql/my.cnf /opt/homebrew/etc/my.cnf ~/.my.cnf
卸载
brew remove mysql@8.0
brew uninstall mysql@8.0
删除之前的文件
cd /opt/homebrew/var/mysql
rm -rf * 全部删除
安装
brew install mysql@8.0
To restart mysql@8.0 after an upgrade:
brew services restart mysql@8.0
Or, if you don't want/need a background service you can just run:
/opt/homebrew/opt/mysql@8.0/bin/mysqld_safe --datadir\=/opt/homebrew/var/mysql
启动
brew services start mysql@8.0
设置密码
mysql_secure_installation
ps -ax | grep mysql 停止并杀死任何 MySQL 进程 brew remove mysql brew cleanup sudo rm /usr/local/mysql sudo rm -rf /usr/local/var/mysql sudo rm -rf /usr/local/mysql* sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist sudo rm -rf /Library/StartupItems/MySQLCOM sudo rm -rf /Library/PreferencePanes/My* launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist 编辑 /etc/hostconfig 删除行 MYSQLCOM=-YES- rm -rf ~/Library/PreferencePanes/My* sudo rm -rf /Library/Receipts/mysql* sudo rm -rf /Library/Receipts/MySQL* sudo rm -rf /private/var/db/receipts/*mysql* 重新启动计算机只是为了确保所有 MySQL 进程都被杀死 尝试运行MySQL,无法工作,卸载完成
brew install mysql@8.0
brew services start mysql@8.0
mysql_secure_installation
报错:
mysql_secure_installation Securing the MySQL server deployment. # Default Homebrew MySQL server config Enter password for user root: # Default Homebrew MySQL server config Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (61) oker@okerdeMacBook-Pro etc % brew services start mysql@8.0 Bootstrap failed: 5: Input/output error Try re-running the command as root for richer errors. Error: Failure while executing; `/bin/launchctl bootstrap gui/501 /Users/{username}/Library/LaunchAgents/homebrew.mxcl.mysql@8.0.plist` exited with 5. oker@okerdeMacBook-Pro etc % brew services restart mysql@8.0
最关键是这一步:
sudo rm /tmp/mysql*
删除后,再运行mysql_secure_installation就好了。
回到工程:
make run
[error] failed to initialize database, got error Error 1049 (42000): Unknown database 'w3d'
panic: failed to connect database
mysql -u root -p
CREATE DATABASE w3d;
make run
MySQL客户端无法访问MySQL
https://sequelpro.com/test-builds
下载,安装。
报错。
解决办法:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
上面error的解决办法:
show variables like 'validate_pass%';
set global validate_password.length = 1;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
再次连接,就好了。