1. 程式人生 > >Mac安裝MariaDB資料庫

Mac安裝MariaDB資料庫

Mac安裝MariaDB資料庫

參考資料:

Mac中MariaDB資料庫的安裝步驟
使用Homebrew在Mac OS X上安裝MariaDB 10.1.16

如果你是Mac上的開發者,你可以在OS X上通過Homebrew來簡單的獲取安裝最新穩定版本的MariaDB,接下來我們將一步步的來指導安裝MariaDB資料庫,如果你的Mac中已經安裝好了Xcode和Homebrew的話,則直接跳到第四步。

1. 安裝Xcode

使用如下命令來安裝Xcode:

xcode-select --install

2. 安裝Homebrew

安裝Homebrew的命令:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

3. 檢查Homebrew

brew doctor

4. 更新Homebrew

如果通過上面的命令檢查到Homebrew不是最新的版本,可以通過如下命令來把Homebrew更新到最新:

brew update

5. 確認MariaDB的版本

在Homebrew倉庫中確認MariaDB的版本:

brew info mariadb

6. 安裝MariaDB

通過如下命令來下載安裝MariaDB:

brew install mariadb

7. 執行資料庫安裝程式

mysql_install_db

8. 執行MariaDB

經過了上面的若干命令,已經安裝好了MariaDB資料庫,但是MariaDB資料庫服務並沒有啟動,你可以通過這個命令來啟動MariaDB資料庫服務:

mysql.server start

9. 安全的完成安裝

通過上面的啟動MariaDB資料庫服務,你已經可以連線MariaDB的資料庫了,但是還不夠安全,通過如下步驟可以完成更全面的設定,如:重設root使用者的密碼、移除匿名使用者、移除預設的test資料庫等等

mysql_secure_installation

具體的執行和設定如下:

Bens-MacBook-Pro:10.1.16 ben$ mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n]
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

10. 連線MariaDB資料

連線MariaDB資料庫的命令:

mysql -u root -p

11. 驗證MariaDB版本

MariaDB [(none)]> select @@version; 
+-----------------+
| @@version  |
+-----------------+
| 10.1.14-MariaDB |
+-----------------+
1 row in set (0.00 sec)

MariaDB基礎命令

下面是MariaDB的一些基礎使用命令:

-- 顯示資料庫列表
show databases;
 
-- 切換到名為mysql的資料庫,顯示該庫中的資料表
use mysql; 
show tables;
 
-- 顯示資料表table的結構
desc table;
 
-- 建資料庫A與刪資料庫A
create database `database_A`; 
drop database `database_A`;
 
-- 建表:
use database_A; 
create table table_A(欄位列表); 
drop table table_A;
 
-- 顯示錶中的記錄:
select * from table_A;
 
-- 清空表中記錄:
delete from table_A;