在Linux系統(tǒng)上,使用XAMPP連接MySQL數(shù)據(jù)庫(kù)非常方便。以下是步驟:
1. 安裝XAMPP
sudo tar xvfz xampp-linux-x64-7.4.12-0-installer.run.tar.gz -C /opt sudo /opt/lampp/manager-linux-x64.run
2. 啟動(dòng)XAMPP
sudo /opt/lampp/lampp start
3. 進(jìn)入MySQL
/opt/lampp/bin/mysql -u root -p
4. 創(chuàng)建數(shù)據(jù)庫(kù)
CREATE DATABASE testdb;
5. 創(chuàng)建用戶并為其賦予權(quán)限
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON testdb.* TO 'testuser'@'localhost'; FLUSH PRIVILEGES;
6. 連接MySQL
$servername = "localhost"; $username = "testuser"; $password = "password"; $dbname = "testdb"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully";
注意事項(xiàng):
- 在連接MySQL之前,確保XAMPP已啟動(dòng)。
- 在創(chuàng)建用戶并為其賦予權(quán)限時(shí),將'password'替換為實(shí)際的密碼。
- 在連接MySQL時(shí),將$servername,$username,$password和$dbname替換為實(shí)際的值。