// 使用CLI连接MySQL数据库 $ mysql -u username -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.7.20 MySQL Community Server (GPL) mysql>show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
命令行是一种常见的MySQL使用方式,可以通过CLI与MySQL服务器进行交互。CLI包含一组命令行操作,包括创建数据库,添加表和记录等。
// PHP代码连接MySQL数据库 $servername = "localhost"; $username = "root"; $password = ""; try { $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); // 设置 PDO 错误模式为异常 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); }
编程语言API是另一种使用MySQL的方式,它通过编写代码与MySQL进行交互。这种方式提供了更灵活的方式来操作数据库,并且可以实现自动操作数据库。
// 使用phpMyAdmin管理MySQL数据库 // 登录界面 <html> <head><title>phpMyAdmin</title></head> <body> <form method="post" name="index"> 用户名: <input type="text" name="Username" /> 密码: <input type="password" name="Password" /> <input type="submit" value="登录"/> </form> </body> </html>
网页管理工具是最常见的MySQL使用方式之一。phpMyAdmin是一种流行的开源工具,可用于管理MySQL数据库,包括创建、删除数据库和表格,以及增加、修改和删除数据记录等等。