### 創建 MySQL 數據庫及用戶
MySQL 是一個使用廣泛的數據庫服務器,你肯定會在某些節點上安裝配置 MySQL 服務器。 本節將向你展示如何安裝配置 MySQL 服務器,以及如何為應用程序自動創建數據庫和用戶。
#### 準備工作
1. 如果你還沒有 MySQL 模塊,先創建一個:
```
# mkdir /etc/puppet/modules/mysql
# mkdir /etc/puppet/modules/manifests
# mkdir /etc/puppet/modules/files
```
2. 使用如下內容創建 /etc/puppet/modules/mysql/manifests/server.pp 文件:
```
class mysql::server {
package { "mysql-server": ensure => installed }
service { "mysql":
enable => true,
ensure => running,
require => Package["mysql-server"],
}
file { "/etc/mysql/my.cnf":
owner => "mysql", group => "mysql",
source => "puppet:///mysql/my.cnf",
notify => Service["mysql"],
require => Package["mysql-server"],
}
exec { "set-mysql-password":
unless => "/usr/bin/mysqladmin -uroot -p${mysql_password}
status",
command => "/usr/bin/mysqladmin -uroot password ${mysql_
password}",
require => Service["mysql"],
}
}
```
3. 使用如下內容創建 /etc/puppet/modules/mysql/files/my.cnf 文件:
```
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
user = mysql
socket = /var/run/mysqld/mysqld.sock
port = 3306
datadir = /var/lib/mysql
!includedir /etc/mysql/conf.d/
```
4. 添加如下代碼到 /etc/puppet/manifests/site.pp 文件:
```
$mysql_password = "secret"
```
5. 運行 Puppet:
```
# puppet agent --test
info: Retrieving plugin
info: Caching catalog for cookbook.bitfieldconsulting.com
info: Applying configuration version '1309448283'
notice: /Stage[main]/Mysql::Server/Package[mysql-server]/ensure:
ensure changed 'purged' to 'present'
notice: /Stage[main]/Mysql::Server/File[/etc/mysql/my.cnf]/owner:
owner changed 'root' to 'mysql'
notice: /Stage[main]/Mysql::Server/File[/etc/mysql/my.cnf]/group:
group changed 'root' to 'mysql'
info: /Stage[main]/Mysql::Server/File[/etc/mysql/my.cnf]:
Scheduling refresh of Service[mysql]
info: /Stage[main]/Mysql::Server/File[/etc/mysql/my.cnf]:
Scheduling refresh of Service[mysql]
notice: /Stage[main]/Mysql::Server/Service[mysql]/enable: enable
changed 'false' to 'true'
notice: /Stage[main]/Mysql::Server/Service[mysql]: Triggered
'refresh' from 2 events
notice: Finished catalog run in 61.78 seconds
```
#### 操作步驟
1. 添加如下代碼到 /etc/puppet/modules/mysql/manifests/server.pp 文件:
```
define db( $user, $password ) {
include mysql::server
exec { "create-${name}-db":
unless => "/usr/bin/mysql -u${user} -p${password}${name}",
command => "/usr/bin/mysql -uroot -p${mysql_password} -e
\"create database ${name}; grant all on ${name}.* to
${user}@localhost identified by '$password'; flush
privileges;\"",
require => Service["mysql"],
}
}
```
2. 添加如下代碼到一個節點:
```
mysql::server::db { "johnstest":
user => "john",
password => "johnstest",
}
```
3. 運行 Puppet:
```
# puppet agent --test
info: Retrieving plugin
info: Caching catalog for cookbook.bitfieldconsulting.com
info: Applying configuration version '1309449259'
notice: /Stage[main]//Node[cookbook]/Mysql::Server::Db[johnstest]/
Exec[create-johnstest-db]/returns: executed successfully
notice: Finished catalog run in 1.61 seconds
```
4. 檢查數據庫是否已經創建,以及用戶和權限的正確性:
```
# mysql -ujohn -pjohnstest johnstest
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 36
Server version: 5.1.41-3ubuntu12.10 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the current
input statement.
mysql>
```
#### 工作原理
mysql::server 類安裝 MySQL,并使用你在 site.pp 文件中設置的 root 用戶口令配置 MySQL。 名為 mysql::server::db 的 define 允許我們使用一個指定的名字創建數據庫, 以及一個能訪問此數據庫的相關的 MySQL 用戶。 例如,一個典型的 web 應用程序可能需要一個以應用程序命名的數據庫, 以及一個可以登錄數據庫的特定用戶名。
#### 更多用法
要創建多個數據庫,只需添加多個 mysql::server::db 實例:
```
mysql::server::db { [ "test1", "test2", "test3" ]:
user => "john",
password => "johnstest",
}
```
- Puppet 2.7 Cookbook 中文版
- 中文翻譯版
- 譯者序
- 項目緣起
- 翻譯方法
- 社區鏈接
- 社區建議
- 貢獻者
- 原書版權頁
- 關于作者
- 前言
- 本書內容
- 閱讀前提
- 適用讀者
- 格式約定
- 讀者反饋
- 客戶支持
- 下載案例代碼
- 勘誤表
- Puppet 基礎設施
- 使用版本控制
- 使用提交鉤子
- 使用 Rake 部署變更
- 配置 Puppet 的文件服務器
- 從 cron 運行 Puppet
- 使用自動簽名
- 預簽名證書
- 從 Puppet 的 filebucket 檢索文件
- 使用 Passenger 擴展 Puppet 的部署規模
- 創建去中心化的分布式 Puppet 架構
- 監控、報告和排錯
- 生成報告
- 通過 Email 發送包含特定標簽的日志信息
- 創建圖形化報告
- 自動生成 HTML 文檔
- 繪制依賴關系圖
- 測試你的 Puppet 配置清單
- 執行模擬運行
- 檢測編譯錯誤
- 理解 Puppet 的錯誤信息
- 顯示命令的輸出結果
- 輸出調試信息
- 檢查配置設置
- 使用標簽
- 使用運行階段
- 使用不同的環境
- Puppet 語言及其寫作風格
- 使用 Puppet 社區規范
- 使用模塊
- 使用標準的命名規范
- 使用嵌入式 Ruby 代碼
- 使用純 Ruby 代碼書寫配置清單
- 遍歷多個項目
- 書寫強大的條件語句
- 在 if 語句中使用正則表達式
- 使用選擇器和 case 語句
- 檢測字符串中是否包含指定的值
- 使用正則表達式替換
- 書寫更優質的配置清單
- 使用資源的數組
- 使用 define 資源
- 指定資源的依賴關系
- 使用節點繼承
- 使用類的繼承和重載
- 給類傳遞參數
- 書寫可重用的跨平臺配置清單
- 獲得系統的環境信息
- 導入動態信息
- 從 CSV 文件導入數據
- 給 Shell 命令傳遞參數
- 使用文件和軟件包
- 為配置文件添加配置行
- 使用 Augeas 自動修改配置文件
- 使用配置片段構建配置文件
- 使用 ERB 模板
- 在模板中遍歷數組
- 從第三方倉庫安裝軟件包
- 配置 APT 軟件倉庫
- 配置 GEM 倉庫
- 從源碼包自動構建軟件
- 比較軟件包的版本
- 用戶和虛擬資源
- 使用虛擬資源
- 使用虛擬資源管理用戶
- 管理用戶基于密鑰的 SSH 訪問
- 管理用戶的自定義文件
- 有效地分發 cron 任務
- 當文件更新時運行命令
- 使用主機資源
- 為文件資源指定多個源
- 使用文件資源遞歸地分發整個目錄樹
- 清理過期的舊文件
- 使用日程表資源
- 資源的審計
- 臨時禁用資源
- 管理時區
- 應用程序
- 管理 Apache 服務
- 創建 Apache 虛擬主機
- 創建 Nginx 虛擬主機
- 創建 MySQL 數據庫及用戶
- 管理 Drupal 站點
- 管理 Rails 應用程序
- 服務器和云基礎設施
- 部署 Nagios 監控服務器
- 使用 Heartbeat 構建高可用服務
- 管理 NFS 服務和文件共享
- 使用 HAProxy 為多個 web 服務器實現負載均衡
- 使用 iptables 管理防火墻
- 管理 Amazon 的 EC2 實例
- 使用 Vagrant 管理虛擬機
- 外部工具和 Puppet 生態環境
- 創建 Facter 的自定義 fact
- 在運行 Puppet 之前和之后執行命令
- 從 Shell 會話生成 Puppet 配置清單
- 從運行的系統上生成 Puppet 配置清單
- 使用 Puppet Dashboard
- 使用 Foreman
- 使用 MCollective
- 使用公共模塊
- 使用外部節點分類器
- 創建自定義的資源類型
- 創建自定義的提供者