centos7下载安装mysql5.7及主从配置
时间: 2024-10-09 20:06:53 浏览: 111
CentOS 7中安装MySQL 5.7并设置主从复制的步骤如下:
**安装MySQL 5.7**:
1. 更新系统包信息:
```
sudo yum update -y
```
2. 添加MySQL社区仓库:
```
sudo tee /etc/yum.repos.d/mysql-community.repo << EOF
[mysql57-community]
name=MySQL Community Server 5.7 Repository
baseurl=https://dev.mysql.com/rpm/redhat/7-x86_64/
gpgkey=https://dev.mysql.com/doc/mysql-server/5.7/en/mysql-rpm-gpg-key.html
gpgcheck=1
enabled=1
EOF
```
3. 安装MySQL 5.7:
```
sudo yum install mysql-community-server -y
```
安装过程中会提示你设置root用户的密码。
**配置主从复制**:
1. 启动MySQL服务并登录到命令行客户端:
```
sudo systemctl start mysqld
mysql -p
```
2. 创建一个新的用户用于从服务器,并分配复制权限:
```sql
CREATE USER 'replication_user'@'%' IDENTIFIED BY 'your_password';
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';
FLUSH PRIVILEGES;
```
3. 登录主数据库并设置复制参数,如更改目录、同步位置等:
```
CHANGE MASTER TO MASTER_HOST='master_host', MASTER_USER='replication_user', MASTER_PASSWORD='your_password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=<start_position>;
```
`master_host`是主服务器地址,`<start_position>`是从哪里开始复制日志的位置。
4. 主服务器上创建新的复制线程:
```
START SLAVE;
```
5. 验证主从复制是否成功运行:
```sql
SHOW SLAVE STATUS \G
```
阅读全文