解压安装MySQL5.7
- MySQL安装包下载网址:https://downloads.mysql.com/archives/community/、https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-5.7/
- 下载到目录/data1/soft/,并解压到目录/data1/soft/mysql
cd /data1/soft/ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz tar -zxvf mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz mv mysql-5.7.39-linux-glibc2.12-x86_64 mysql
- 创建MySQL用户组,创建文件和路径,并且授权(在my.cnf中用到)
mkdir /data1/soft/mysql/run /data1/soft/mysql/logs touch /data1/soft/mysql/mysql.log groupadd mysql useradd -g mysql mysql -d /data1/soft/mysql chown -R mysql:mysql /data1/soft/mysql chown mysql:mysql /data1/soft/mysql/mysql.log
- 配置MySQL本地服务
cp -a /data1/soft/mysql/support-files/mysql.server /etc/init.d/mysqld
- 编辑`/etc/my.cnf`文件,设置端口50005,设置默认编码utf8mb4,内容如下
[mysql] port=50005 default-character-set=utf8 socket=/data1/soft/mysql/mysql.sock max_allowed_packet = 500M [mysqld] log-bin=/data1/soft/mysql/mysql-bin binlog_format=ROW basedir=/data1/soft/mysql datadir=/data1/soft/mysql/data socket=/data1/soft/mysql/mysql.sock symbolic-links=0 port=50001 server-id=1 log-bin=mysql-bin max_allowed_packet = 500M max_connections=500 log_bin_trust_function_creators = 1 log_bin=ON character-set-server=utf8mb4 collation-server=utf8mb4_general_ci [client] default-character-set=utf8mb4 [mysqld_safe] log-error=/data1/soft/mysql/mysql.log pid-file=/data1/soft/mysql/mysql.pid !includedir /etc/my.cnf.d
- 创建软连接
ln -s /data1/soft/mysql/mysql.sock /tmp/mysql.sock
- 初始化数据库并启动服务
-
su mysql /data1/soft/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/data1/soft/mysql/ --datadir=/data1/soft/mysql/data/ --user=mysql --initialize
- 上一步的命令运行结束后,在终端可以看到初始密码,记录改初始密码,在后续步骤修改
- 启动服务
service mysqld start
- 切换到`root`用户,在文件/etc/profile末尾添加
PATH=$PATH:/data1/soft/mysql/bin
- 执行以下命令,使环境变量生效
source /etc/profile
- 设置开机自动启动
-
chmod +x /etc/init.d/mysqld chkconfig --add mysqld chkconfig --level 345 mysqld on
- 执行chkconfig --list命令,如果mysqld的3、4、5状态为开或者为 on则表示设置开机自动启动设置成功
- 修改密码,按回车Enter后,输入一次初始密码,再输入两次新密码
mysqladmin -u root -p password
- 登录mysql
mysql -u root -p
- 检查数据库的日志是否开启,日志格式是否为ROW
-
SHOW VARIABLES LIKE "log_bin"; # 开启为ON show variables like 'binlog_format';
主从复制
- 安装好主库和从库后开配置主从备份
- 修改从库的配置文件/etc/my.cnf,在[mysqld]下增加以下配置,其中server-id用于标识主从数据库,二者设置不能相同
# Master-slave Replication server-id=2 #read_only = 1 master_info_repository=TABLE relay_log_info_repository=TABLE slave-skip-errors=all # 不进行同步的库,可按需要修改 replicate-ignore-db=mysql replicate-ignore-db=information_schema # 需要进行同步的库,以db1和db2为例,可按需要修改 replicate-do-db=db1 replicate-do-db=db2
- 在主库创建一个repl账号,仅用于主从复制连接
grant replication slave on *.* to 'repl'@'从库ip' identified by '密码';
- 修改主库的my.cnf文件,确保日志功能打开,并设置server-id
server-id=1 log-bin=mysql-bin binlog_format=ROW
- 登录MySQL检查上一步`my.cnf`的修改是否配置成功
SHOW VARIABLES LIKE 'log_bin'; show variables like 'binlog_format'; show variables like 'server_id';
- 在从节点远程连接主库,生成master的数据备份mysql.bak.sql,假设要同步的数据库是db1,db2
mysqldump -u root -p -h 192.168.0.18 -P 3306 --skip-lock-tables --single-transaction --flush-logs --hex-blob --master-data=2 --databases db1 db2 > mysql.bak.sql
- -在从节点导入备份文件
mysql -u root -p < mysql.bak.sql
- 在备份文件中查找日志文件备份前的Position值,输入命令less mysql.bak.sql,查找字符CHANGE,例如找到以下信息,则说明日志偏移量是154
-
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=154;
- 基于上一步信息,登录从库的MySQL,执行以下SQL语句
change master to master_host='192.168.0.18', master_user='repl', master_password='主库repl的密码', master_log_file='mysql-bin.000004', master_port=3306, master_log_pos=154;
- 在从库启动主从,并查看状态
start slave; show slave status\G;