centos中mysql的安装使用教程
时间: 2024-10-28 15:09:55 浏览: 8
在CentOS系统中安装MySQL数据库通常包括几个步骤:
1. **更新包列表**:
```
sudo yum update -y
```
2. **添加MySQL官方仓库**:
```
sudo tee /etc/yum.repos.d/mysql-community.repo <<EOF
[mysql80-community]
name=MySQL Community Server 8.0 Repository
baseurl=https://mirrors.aliyun.com/mysql/community-release-el7/
gpgkey=https://mirrors.aliyun.com/mysql/community-release/el7/GPG-KEY-mysql-community-server
gpgcheck=1
enabled=1
repotimezone=UTC
EOF
```
3. **安装MySQL**:
```
sudo yum install mysql-community-server -y
```
4. **启动并设置服务**:
```
sudo systemctl start mysqld
sudo systemctl enable mysqld
```
5. **安全初始化**:
首次运行MySQL时,会提示创建root用户的密码,你可以按照提示操作。也可以通过命令行手动初始化:
```
sudo mysql_secure_installation
```
6. **登录MySQL**:
使用`mysql`命令进入MySQL shell:
```
sudo mysql
```
7. **创建用户和数据库**:
创建新的用户和数据库,例如:
```
CREATE DATABASE your_database;
GRANT ALL PRIVILEGES ON your_database.* TO 'your_username'@'localhost' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;
```
8. **退出MySQL**:
`exit;`
阅读全文