在Mac电脑上使用VS Code进行PHP开发并关联操作MySQL数据库,然后将数据库部署到ECS。
1.安装PHP和MySQL
确保你的Mac上已经安装了PHP和MySQL。你可以使用Homebrew来安装它们:
$ brew install php
$ brew install mysql
安装mysql完成后记住这一句:
MySQL is configured to only allow connections from localhost by defaultTo connect run:mysql -u root
使用以下命令查询安装情况:
$ php -v
$ mysql --version
启动MySQL服务:
$ brew services start mysql
重启MySQL服务:
$ brew services restart mysql
停止MySQL服务:
$ brew services stop mysql
2.创建MySQL用户、数据库和权限
登录到MySQL:
// 无密码直接用这一句登录
$ mysql -u root
// 也可以用这指令,输密码是直接回车跳过就是
$ mysql -u root -p
创建新数据库:
$ CREATE DATABASE mydatabase;
创建新用户并设置密码(替换myuser和mypassword为你自己的设置):
$ CREATE USER'myuser'@'localhost' IDENTIFIED BY'mypassword';
授权新用户访问数据库:
$ GRANT ALL PRIVILEGES ON mydatabase.*TO'myuser'@'localhost';
刷新权限:
$ FLUSH PRIVILEGES;
退出MySQL:
$ EXIT;
3.配置PHP连接MySQL
你需要在PHP代码中使用PDO或mysqli扩展来连接MySQL数据库。
确保你的MySQL服务正在运行,并且你已经创建了相应的用户、数据库和权限。
1).PDO
<?php
$host = '127.0.0.1';
$db = 'your_database';
$user = 'your_username';
$pass = 'your_password';
$charset = 'utf8mb4';$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,PDO::ATTR_EMULATE_PREPARES => false,
];try {$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
?>
2).mysqli扩展
<?php// 数据库连接配置
$host = 'localhost';
$dbname = 'mydatabase';
$username = 'myusername';
$password = 'mypassword';// 创建 mysqli 连接对象
$conn = new mysqli($host, $username, $password, $dbname);// 检查连接是否成功
if ($conn->connect_error) {die("连接失败: " . $conn->connect_error);
}// 执行查询语句
$query = "SELECT * FROM users";
$result = $conn->query($query);// 处理结果集
if ($result->num_rows > 0) {while ($row = $result->fetch_assoc()) {echo $row['username'] . "<br>";}
} else {echo "没有结果";
}// 关闭数据库连接
$conn->close();?>
4.在VS Code中编写和运行PHP代码
index.html
<!DOCTYPE html>
<html>
<head><title>城市信息表单</title>
</head>
<body><h1>城市信息表单</h1><form action="http://localhost:8080/city.php" method="post"><label for="cityName">城市名称:</label><input type="text" id="name" name="name" required><br><br><label for="cityCode">城市编码:</label><input type="text" id="code" name="code" required><br><br><label for="cityCode">增删改查:</label><input type="text" id="mark" name="mark" required><br><br><input type="submit" value="提交"></form><p id="jsonResponse"></p><script>document.querySelector('form').addEventListener('submit', function (event) {event.preventDefault();// 方式一:PHP 中使用这种方式获取 $name = $_POST['name'] ?? '';var form = event.target;var formData = new FormData(form);fetch(form.action, {method: 'POST',body: formData}).then(response => response.json()).then(data => {document.getElementById('jsonResponse').textContent = JSON.stringify(data);}).catch(error => console.error('请求接口失败', error));/*// 方式二:PHP中用这种方式获取 $data = json_decode(file_get_contents('php://input'), true); $name = $data['name'] ?? '';var form = event.target;var name = form.elements.name.value;var code = form.elements.code.value;var mark = form.elements.mark.value;var requestData = {name: name,code: code,mark: parseInt(mark) // mark 1:增 2:删 3:改 4:查};var xhr = new XMLHttpRequest();xhr.open('POST', form.action);xhr.setRequestHeader('Content-Type', 'application/json');// xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');xhr.onload = function () {if (xhr.status === 200) {var data = JSON.parse(xhr.responseText);document.getElementById('jsonResponse').textContent = JSON.stringify(data);} else {console.error('请求接口失败');}};xhr.send(JSON.stringify(requestData));*/});</script>
</body>
</html>
pdoConnect.php
<?php
$host = '127.0.0.1';
$db = 'city_database';
$user = 'gamin';
$pass = '123456';
$charset = 'utf8mb4';$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,PDO::ATTR_EMULATE_PREPARES => false,
];// 连接数据库
try {$pdo = new PDO($dsn, $user, $pass, $options);createCitiesTable();
} catch (\PDOException $e) {throw new \PDOException($e->getMessage(), (int)$e->getCode());
}// 创建城市表
function createCitiesTable() {$tb_name = "cities";// 检查表是否已存在$isTableExists = checkTableExists($tb_name);if (!$isTableExists) { // 创建表$sql = "CREATE TABLE $tb_name (id INT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(255) NOT NULL,code INT NOT NULL)";global $pdo;$pdo->exec($sql);} else {}
}// 检查表是否存在
function checkTableExists($tableName) {global $pdo;$stmt = $pdo->query("SHOW TABLES LIKE '$tableName'");return $stmt->rowCount() > 0;
}?>
sqlOperation.php
<?php
require "pdoConnect.php";function pdo() {global $pdo;if (isset($pdo)) {return $pdo;} else {// 处理 $pdo 未初始化的情况return null;}
}// 改变数据 插入|更新|删除
function changeData(string $sql, array $arr) {$stmt = pdo()?->prepare($sql);$result = $stmt?->execute($arr);return $result;
}// 查询数据
function findData(string $sql, array $arr) {$stmt = pdo()?->prepare($sql);$stmt?->execute($arr);$result = $stmt?->fetch();return $result;
}?>
output.php
<?phpclass ErrorCode {const FAILURE = -1;const SUCCESS = 0;const INVALID_INPUT = 100;const MISSING_PARAMETER = 101;const DATABASE_ERROR = 200;const FILE_NOT_FOUND = 201;const METHOD_NOT_ALLOW = 405;const INTERNAL_SERVER_ERROR = 500;const DATA_NOT_FOUND = 1000;public static function getErrorMessage($errorCode) {switch ($errorCode) {case self::FAILURE:return "Operation failed.";case self::SUCCESS:return "Operation succeeded.";case self::INVALID_INPUT:return "Invalid input provided.";case self::MISSING_PARAMETER:return "Required parameter is missing.";case self::DATABASE_ERROR:return "Database error occurred.";case self::FILE_NOT_FOUND:return "File not found.";case self::METHOD_NOT_ALLOW:return "Method not allowed."; case self::INTERNAL_SERVER_ERROR:return "Internal Server Error."; case self::DATA_NOT_FOUND:return "No matching data found."; default:return "Unknown error occurred.";}}
}function outputJSON(int $errorCode, $data = []) {// 构建要返回的数据$response = ['code' => $errorCode,'message' => ErrorCode::getErrorMessage($errorCode),'data' => $data];// 设置响应内容为 JSON 格式header('Content-Type: application/json');// 将数据转换为 JSON 字符串$json = json_encode($response);// 输出 JSON 字符串echo $json;
}?>
city.php
<?php
require "sqlOperation.php";
require "output.php";$tb_name = "cities";// 检查POST数据
if ($_SERVER['REQUEST_METHOD'] === 'POST') {/* 方式一: */// 获取数据并验证$name = $_POST['name'] ?? '';$code = $_POST['code'] ?? '';$mark = $_POST['mark'] ?? 0;/* 方式二:表单中Content-Type用'application/json'时,用这种方式获取传参 // 获取 JSON 数据并解析$data = json_decode(file_get_contents('php://input'), true);// 获取数据并验证$name = $data['name'] ?? '';$code = $data['code'] ?? '';$mark = $data['mark'] ?? 0;*/if (empty($name) || empty($code) || $mark == 0) {outputJSON(ErrorCode::MISSING_PARAMETER);return;}// mark 1:增 2:删 3:改 4:查if ($mark == 1) {// 插入数据$result = changeData("INSERT INTO $tb_name (name, code) VALUES (:name, :code)", ['name' => $name, 'code' => $code]);if ($result) {outputJSON(errorCode::SUCCESS);} else {outputJSON(errorCode::FAILURE);}} else if ($mark == 2) {// 删除数据$result = changeData("DELETE FROM $tb_name WHERE code = :code", ['code' => $code]);if ($result) {outputJSON(errorCode::SUCCESS);} else {outputJSON(errorCode::FAILURE);}} else if ($mark == 3) {// 更新数据$result = changeData("UPDATE $tb_name SET name = :name WHERE code = :code", ['name' => $name, 'code' => $code]);if ($result) {outputJSON(errorCode::SUCCESS);} else {outputJSON(errorCode::FAILURE);}} else if ($mark == 4) {// 查询数据$result = findData("SELECT * FROM $tb_name WHERE code = :code", ['code' => $code]);if ($result) {// 查询成功,至少有一条匹配的数据outputJSON(errorCode::SUCCESS,$result);} else {// 查询失败,没有匹配的数据outputJSON(ErrorCode::DATA_NOT_FOUND);}}exit;
}?>
cityList.php
<?php
require("pdoConnect.php");
require("output.php");// 检查POST数据
if ($_SERVER['REQUEST_METHOD'] === 'POST') { try {// 获取数据并验证$page = isset($_POST['page']) ? intval($_POST['page']) : 1; // 当前页码,默认为第一页$pageSize = isset($_POST['pageSize']) ? intval($_POST['pageSize']) : 10; // 每页数据条数,默认为 10// 对页码进行有效性检查$page = max(1, intval($page)); ;// 确保每页记录数为正整数$pageSize = max(1, intval($pageSize)); ;// 表名$tb_name = "cities";// 查询总记录数$countSql = "SELECT COUNT(*) AS total FROM $tb_name";$countStmt = $pdo->prepare($countSql);$countStmt->execute();$totalItems = $countStmt->fetchColumn();// 计算总页数$totalPages = ceil($totalItems / $pageSize);// 对页码进行有效性检查$page = min($page, $totalPages);// 计算偏移量$offset = ($page - 1) * $pageSize;// 查询当前页的数据$sql = "SELECT * FROM $tb_name LIMIT :offset, :pageSize";$stmt = $pdo->prepare($sql);$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);$stmt->bindValue(':pageSize', $pageSize, PDO::PARAM_INT);$stmt->execute();$pagedData = $stmt->fetchAll(PDO::FETCH_ASSOC);// 构建要返回的数据$response = ['page' => $page,'pageSize' => $pageSize,'totalPages' => $totalPages,'totalItems' => $totalItems,'data' => array_map(function ($item) {return $item;}, $pagedData)];outputJSON(ErrorCode::SUCCESS, $response);} catch (Exception $e) {// 发生错误时返回错误响应outputJSON(ErrorCode::INTERNAL_SERVER_ERROR);}
} else {// 非 POST 请求返回错误响应outputJSON(ErrorCode::METHOD_NOT_ALLOW);
}?>
5.测试本地开发环境
在本地开发环境中测试你的PHP脚本,确保它可以正确地与MySQL数据库进行交互。
6.将MySQL数据库部署到ECS
1).创建ECS实例:登录到阿里云管理控制台,创建一个ECS实例。
2).安装MySQL:通过SSH连接到你的ECS实例,使用相应的包管理器安装MySQL。
3).配置MySQL:设置用户权限、创建数据库,并导入你的本地数据库数据到ECS上的MySQL实例。
4).更新你的PHP代码中的数据库连接信息,使其指向ECS实例上的MySQL。
7.部署PHP代码到ECS
1).将你的PHP代码上传到ECS实例。你可以使用FTP、SCP或者Git来传输文件。
2).配置Web服务器:在ECS上安装和配置Nginx或Apache作为Web服务器来处理PHP请求。
3).确保ECS的安全组规则允许访问MySQL和Web服务器的端口。
8.测试远程环境
在浏览器中访问你的ECS实例的公网IP地址,确保你的PHP应用能够正确地运行并连接到MySQL数据库。