Macos下安裝MySQL簡明教程

環境介紹

Macos Jave 10.14. MySQL 5.7

基於Brew安裝

brew install [email protected]

運行結果如下:

Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/[email protected]
######################################################################## 100.0%
==> Pouring [email protected]
==> /usr/local/Cellar/[email protected]/5.7.24/bin/mysqld --initialize-insecure --user=chenjunfeng1 --basedir=/usr/local/Cellar/[email protected]/5.7.24 --datadir=/usr/local/var/mysql --tmpdir=/tmp
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

[email protected] is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have [email protected] first in your PATH run:
  echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.bash_profile

For compilers to find [email protected] you may need to set:
  export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
  export CPPFLAGS="-I/usr/local/opt/[email protected]/include"

For pkg-config to find [email protected] you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig"

To have launchd start [email protected] now and restart at login:
  brew services start [email protected]
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/[email protected]/bin/mysql.server start
==> Summary
🍺  /usr/local/Cellar/[email protected]/5.7.24: 317 files, 234.3MB

查看當前MySQL的安裝情況

brew info [email protected]

命令的輸出結果如下:

[email protected]: stable 5.7.24 (bottled) [keg-only]
Open source relational database management system
https://dev.mysql.com/doc/refman/5.7/en/
/usr/local/Cellar/[email protected]/5.7.24 (317 files, 234.3MB)
  Poured from bottle on 2018-10-29 at 09:50:32
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/[email protected]
==> Dependencies
Build: cmake ✘
Required: openssl ✔
==> Requirements
Required: macOS >= 10.7 ✔
==> Options
--with-embedded
	Build the embedded server
--with-local-infile
	Build with local infile loading support
--with-memcached
	Build with InnoDB Memcached plugin
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

[email protected] is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have [email protected] first in your PATH run:
  echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.bash_profile

For compilers to find [email protected] you may need to set:
  export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
  export CPPFLAGS="-I/usr/local/opt/[email protected]/include"

For pkg-config to find [email protected] you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig"


To have launchd start [email protected] now and restart at login:
  brew services start [email protected]
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/[email protected]/bin/mysql.server start
==> Analytics
install: 21,701 (30 days), 61,998 (90 days), 93,106 (365 days)
install_on_request: 21,647 (30 days), 61,858 (90 days), 92,927 (365 days)
build_error: 0 (30 days)

MySQL安裝Brew Services

安裝brew services

brew tap homebrew/services
命令輸出結果爲:

Updating Homebrew...
==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 14 (delta 0), reused 9 (delta 0), pack-reused 0
Unpacking objects: 100% (14/14), done.
Tapped 1 command (44 files, 58.6KB).
hostmachine:~ forkusername$ brew services start [email protected]
==> Successfully started `[email protected]` (label: [email protected])

啓動MySQL

brew services start [email protected]
啓動過程信息如下:

==> Successfully started `[email protected]` (label: [email protected])

查看啓動的service list

brew services list
查看服務的列表結果輸出:

Name      Status  User         Plist
emacs     stopped              
[email protected] started chenjunfeng1 /Users/username/Library/LaunchAgents/[email protected]

將當前的MySQL強制指向5.7

brew link [email protected] --force

信息輸出如下:

Linking /usr/local/Cellar/[email protected]/5.7.24... 87 symlinks created

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.bash_profile

檢查當前MySQL的版本

mysql -V

版本信息輸出如下:

mysql  Ver 14.14 Distrib 5.7.24, for osx10.14 (x86_64) using  EditLine wrapper

配置MySQL

設置root密碼

mysqladmin -u root password ‘password’

輸出結果信息:

mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

登陸MySQL,創建表

mysql -u root -p ‘password’
show databases;
create database ‘db_name’;
use db_name;

總結

在Macos下,主要是使用brew來進行系統的配置和管理的。

Reference

  1. https://gist.github.com/operatino/392614486ce4421063b9dece4dfe6c21
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章