mac python 怎么安装mysql
时间: 2024-08-19 22:02:07 浏览: 116
在Mac上使用Python连接MySQL数据库,首先需要安装Python的MySQL驱动程序pymysql。以下是安装步骤:
1. **确保已安装Homebrew**(如果你还没有安装,可以访问https://brew.sh 官网按照指示安装):
```
brew install python3 # 如果你想要安装Python 3版本
```
2. **安装pip**(Python包管理工具):
```
pip3 install --upgrade pip
```
3. **安装pymysql**驱动程序:
```
pip3 install pymysql
```
4. **验证安装**:
打开Python交互模式(命令行输入 `python3` 或者 `python3 -m ipython`),然后运行以下代码测试连接:
```python
import pymysql
connection = pymysql.connect(host='localhost', user='your_username', password='your_password', db='your_database')
try:
cursor = connection.cursor()
print("Connected to MySQL!")
except Exception as e:
print(f"Error: {e}")
finally:
if connection.open:
connection.close()
```
请将上述代码中的 `your_username`, `your_password`, 和 `your_database` 替换为你实际的MySQL用户名、密码和数据库名。
阅读全文
相关推荐

















