官方网站:https://www.yugabyte.com/
1、先决条件
# 确定是否安装python2,暂时不支持python3
python --version
Python 2.7.10(系统默认)
# 打开文件数量限制
launchctl limit maxfiles
maxfiles 256 unlimited(系统默认)
我们需要提高打开文件数量限制(系统要求为1048576)
打开目录 /Library/LaunchDaemons/
创建limit.maxfiles.plist
文件,文件内容如下:
Label
limit.maxfiles
ProgramArguments
launchctl
limit
maxfiles
1048576
1048576
RunAtLoad
ServiceIPC
修改文件权限
sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist
sudo chmod 644 /Library/LaunchDaemons/limit.maxfiles.plist
加载plist文件
sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist
确认修改后限制
launchctl limit maxfiles
maxfiles 1048576 1048576 (修改后)
2、下载
浏览器下载比较慢,建议用工具来下载,地址为https://downloads.yugabyte.com/yugabyte-1.3.0.0-darwin.tar.gz
3、配置
sudo ifconfig lo0 alias 127.0.0.2
sudo ifconfig lo0 alias 127.0.0.3
sudo ifconfig lo0 alias 127.0.0.4
sudo ifconfig lo0 alias 127.0.0.5
sudo ifconfig lo0 alias 127.0.0.6
sudo ifconfig lo0 alias 127.0.0.7
4、创建本地服务
./bin/yb-ctl create
5、运行本地客户端
http://127.0.0.1:7000/
6、编写测试范例
创建数据库
# 进入yugabyte的命令行
./bin/ysqlsh --echo-queries
# 创建数据库
CREATE DATABASE yb_demo;
GRANT ALL ON DATABASE yb_demo to postgres;
# 连接数据库
\c yb_demo;
在Laravel项目中创建新的路由,位置在routes/web.php
Route::get('/yugabyte', function () {
return view('yugabyte');
});
创建试图文件yugabyte.blade.php
PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_PERSISTENT => true));
/* Create the table if it doesn't exist. */
$stmt = 'CREATE TABLE IF NOT EXISTS employee (id int PRIMARY KEY,
name varchar,
salary int,
dept varchar)';
$dbh->exec($stmt);
/* Prepare the insert statement. */
$insert_stmt = $dbh->prepare('INSERT INTO employee(id, name, salary, dept) ' .
'VALUES (:id, :name, :salary, :dept)');
/* Insert a row. */
$insert_stmt->bindValue(':id', 10, PDO::PARAM_INT);
$insert_stmt->bindValue(':name', 'Jane', PDO::PARAM_STR);
$insert_stmt->bindValue(':salary', 150000, PDO::PARAM_INT);
$insert_stmt->bindValue(':dept', 'Engineering', PDO::PARAM_STR);
$insert_stmt->execute();
/* Insert a row. */
$insert_stmt->bindValue(':id', 11, PDO::PARAM_INT);
$insert_stmt->bindValue(':name', 'Joe', PDO::PARAM_STR);
$insert_stmt->bindValue(':salary', 140000, PDO::PARAM_INT);
$insert_stmt->bindValue(':dept', 'Finance', PDO::PARAM_STR);
$insert_stmt->execute();
echo "Inserted new records successfully.\n";
/* Prepare query statement to retrieve user info by id */
$query = $dbh->prepare('SELECT name, salary, dept FROM employee WHERE id = :id');
$query->bindValue(':id', 11, PDO::PARAM_INT);
$query->execute();
$user_info = $query->fetch(PDO::FETCH_ASSOC);
echo "Retrieving info for user id 11...\n";
print_r($user_info);
} catch (Exception $excp) {
print "EXCEPTION: " . $excp->getMessage() . "\n";
exit(1);
}
最后输入地址:http://localhost:8888/yugabyte
界面显示Inserted new records successfully. Retrieving info for user id 11... Array ( [name] => Joe [salary] => 140000 [dept] => Finance )
表示执行成功。
P.S.如果缺少驱动的话,需要自行编译php-pgsql
,一般情况下PHP默认自带该驱动。