CentOS7安装mysql环境

下载

/home/newland/program中执行

1
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz

解压

解压后重命名

1
2
tar -zxvf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.36-linux-glibc2.12-x86_64 mysql-5.7.36

配置

创建数据存储文件夹

1
mkdir /home/newland/program/mysql-5.7.36/data

添加系统mysql组和mysql用户,修改安装目录拥有者为mysql用户

1
2
3
groupadd mysql
useradd mysql -g mysql -d /home/newland/program/mysql-5.7.36
chown -R mysql:mysql /home/newland/program/mysql-5.7.36

初始化,生成临时密码,记录下来备用

1
/home/newland/program/mysql-5.7.36/bin/mysqld --user=mysql --basedir=/home/newland/program/mysql-5.7.36 --datadir=/home/newland/program/mysql-5.7.36/data --initialize

将调优后的my.cnf移动至/etc覆盖原文件

1
mv my.cnf /etc

将mysql.server拷贝至/etc/init.d/下,重命名为mysqld,并且修改内容

1
2
3
4
5
cp /home/newland/program/mysql-5.7.36/support-files/mysql.server /etc/init.d/mysqld
vim /etc/init.d/mysqld
# 修改以下内容后保存
basedir=/home/newland/program/mysql-5.7.36
datadir=/home/newland/program/mysql-5.7.36/data

启动服务并加入开机自启

1
2
systemctl start mysqld
systemctl enable mysqld

使用前面记录的临时密码进入命令行修改root密码,创建新用户

1
2
3
4
mysql -u root -p 临时密码
> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root密码';
> GRANT ALL PRIVILEGES ON *.* TO 'newland'@'%' IDENTIFIED BY 'newland密码' WITH GRANT OPTION;
> FLUSH PRIVILEGES;

创建数据库,导入数据

1
2
3
> CREATE DATABASE newland_ebm_v2 default character set utf8mb4 collate utf8mb4_general_ci;
> USE newland_ebm_v2;
> SOURCE /home/newland/program/newland_ebm_v2.sql;

主从同步

主库 192.168.100.242:3306

修改my.cnf

1
2
3
4
5
6
vim /etc/my.cnf
# 修改以下内容后保存
server-id=1
binlog_format=ROW
log-bin=mysql-bin
expire_logs_days=30

重启服务使之生效

1
systemctl restart mysqld

创建用于主从同步的用户rep

1
2
> GRANT REPLICATION SLAVE ON *.* TO 'rep'@'192.168.100.243' IDENTIFIED BY '同步密码';
> FLUSH PRIVILEGES;

查看master进程状态表,File和Position记录下来备用

1
> SHOW MASTER STATUS;

从库 192.168.100.243:3306

修改my.cnf

1
2
3
vim /etc/my.cnf
# 修改以下内容后保存
server-id=2

重启服务使之生效

1
systemctl restart mysqld

指定主库,使用前面记录的File和Position

1
> CHANGE MASTER TO MASTER_HOST='192.168.100.242', MASTER_USER='rep', MASTER_PASSWORD='同步密码', MASTER_LOG_FILE='binary-log.000005', MASTER_LOG_POS=602;

启动从库slave进程

1
> start slave;

查看从库slave进程同步情况

1
2
3
4
> SHOW SLAVE STATUS \G
# 当以下两项参数为yes时同步成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes