Linux环境下Mysql8的下载、安装、主从模式搭建

主库部署

下载

下载地址:MySQL :: Download MySQL Community Server (Archived Versions)

文件名称:mysql-8.0.17-linux-glibc2.12-x86_64.tar.xz

Linux环境下Mysql8的下载、安装、主从模式搭建_第1张图片

解压

先解压xz

xz -d mysql-8.0.17-linux-glibc2.12-x86_64.tar.xz

再解压tar

tar -xvf mysql-8.0.17-linux-glibc2.12-x86_64.tar

创建数据文件目录

mkdir data

创建用户组、用户和密码

groupadd mysql

useradd -g mysql mysql

给用户授予权

chown -R mysql.mysql /data/mysql8

/data/mysql8是解压msyql的根目录

mysql初始化

切换至mysql的bin目录,执行mysql安装的初始化操作

./mysqld --user=mysql --basedir=/data/mysql8 --datadir=/data/mysql8/data/ --initialize

初始化完成后,会得一个临时密码,注意保存(sE&Pu?k+g8?d)(tNC81xKhTw)m)

mysql配置文件编辑

mysql初始化完后,会在/etc目录下生成/etc/my.cnf文件

[mysqld]
basedir=/data/mysql8/
datadir=/data/mysql8/data/
character-set-server=UTF8MB4
#socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

mysql服务配置

切换到mysql8根目录,添加mysqld服务到系统

cp -a ./support-files/mysql.server /etc/init.d/mysql

授权并添加服务

chmod +x /etc/init.d/mysql

chkconfig --add mysql

mysql启动

启动mysql服务

service mysql start

mysql登陆

把mysql命令添加到系统里

ln -s /data/mysql8/bin/mysql /usr/bin

临时密码登陆

mysql -u root -p

密码修改与远程连接授权

修改密码

alter user 'root'@'localhost' identified with mysql_native_password by 'as123123';

flush privileges;

给用户增加远程连接权限

update user set host='%' where user='root';

flush privileges;

从库部署

安装文件解压

先解压xz

xz -d mysql-8.0.17-linux-glibc2.12-x86_64.tar.xz

再解压tar

tar -xvf mysql-8.0.17-linux-glibc2.12-x86_64.tar

解压完成后,重命名安装文件目录为mysqlslave

给安装目录授权

chown -R mysql.mysql /data/mysqlslave

/data/mysqslave是解压msyql的根目录

配置文件修改

复制第一次的配置文件/etc/my.cnf到/data/mysqlslave目录

cp /etc/my.cnf /data/mysqlslave/my.cnf

修改端口为3307,以及第二个mysql服务的安装根目录、数据文件目录

[mysqld]
port=3307
basedir=/data/mysqlslave/
datadir=/data/mysqlslave/data/
character-set-server=UTF8MB4
socket=/tmp/mysqlslave.sock
pid-file=/data/mysqlslave/mysqlslave.pid
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

mysqlslave参数配置

修改/mysqlslave/support-files/mysql.server,修改内容如下:

basedir=/data/mysqlslave/ datadir=/data/mysqlslave/data conf=/data/mysqlslave/my.cnf

切换到mysqlslave根目录,添加mysqld服务到系统

cp -a ./support-files/mysql.server /etc/init.d/mysqlslave

授权并添加服务

chmod +x /etc/init.d/mysqlslave

chkconfig --add mysqlslave

mysql初始化

切换至/data/mysqlslave/bin目录,执行mysql安装的初始化操作

./mysqld --user=mysql --basedir=/data/mysqlslave --datadir=/data/mysqlslave/data/ --initialize

初始化完成后,会得一个临时密码,注意保存(S_fXXbUMK8b.)

mysql启动

启动mysql服务

service mysqlslave start

mysql登陆

临时密码登陆

mysql -h 127.0.0.1 -P 3307 -u root -p

密码修改与远程连接授权

修改密码

alter user 'root'@'localhost' identified with mysql_native_password by 'as123123';

flush privileges;

给用户增加远程连接权限

use mysql;

update user set host='%' where user='root';

flush privileges;

主从模式搭建

环境准备

防火墙

systemctl status firewalld # 查看防火状态

systemctl stop firewalld #临时关闭

systemctl disable firewalld #永久关闭防火墙

主库

配置文件修改

在配置文件/etc/my.cnf,增加内容:

log-bin=mysql-bin server-id=1 binlog-do-db=test binlog_ignore_db=mysql

server-id=1 中的1可以任定义,只要是唯一的就行。

log-bin=mysql-bin 表示启用binlog功能,并制定二进制日志的存储目录,

binlog-do-db=test 是表示只备份test 数据库,有多个就使用空格隔开。

binlog_ignore_db=mysql 表示忽略备份mysql。

不加binlog-do-db和binlog_ignore_db,那就表示备份全部数据库。

重启数据库

service mysqld restart

从库访问账号创建

#创建账号 create user 'slave001'@'%' identified WITH mysql_native_password BY 'as123123'; #ALTER USER 'slave001'@'192.168.0.227' IDENTIFIED WITH mysql_native_password BY 'as123123'; #授权 grant all privileges on *.* to 'slave001'@'%' with grant option; #刷新 FLUSH PRIVILEGES;

服务器状态

记录服务器状态,这个后面从库要根据这里的结果(File、Position)进行配置

mysql> show master status

Linux环境下Mysql8的下载、安装、主从模式搭建_第2张图片

从库

配置文件修改

/data/mysqlslave/my.cnf增加内容:

server-id=2

重启数据库

service mysqlslave restart

连接主库

use mysql; change master to master_host='192.168.0.227',master_user='slave001',master_password='as123123',master_port=3306,master_log_file='mysql-bin.000002',master_log_pos=2720;

change master to master_host=‘主服务器ip’,

master_user=‘刚在主服务器创建的用户名’,

master_password=‘刚在主服务器创建的用户密码’,

master_port=3306,

master_log_file=‘刚查看主服务器下的File值’,

master_log_pos=刚查看主服务器下的Position值

启动从库

#stop slave; start slave; # 启动slave show slave status; # 查看状态

最重要的是看一个slave_io_running和slave_sql_running的值,如果都是yes,则说明主从模式搭建成功了。

Linux环境下Mysql8的下载、安装、主从模式搭建_第3张图片

用户权限管理

创建用户

create user 'gaoxing'@'%' identified with mysql_native_password by 'as123123';

授予权限

grant all privileges on hpiot_test.* to 'gaoxing'@'%';

privileges:用户的操作权限,如select, delete, update等,共14个。

dbname:数据库名

tablename:表名

WITH GRANT OPTION: 被授权的用户可以将他的拥有的权限授给其他用户若要授权用户对所有数据库和表的相应操作权限可以用*表示,如*.*。

查询用户拥有权限

SHOW GRANTS FOR 'username'@'hostname'; #username 表示用户名,hostname 表示主机名或主机 IP

撤销权限

REVOKE privilege ON dbname.tablename FROM 'username'@'host';

授予system_user权限

grant system_user on *.* to 'root';

查询用户

select user,host from user;

删除用户

drop user 'slave001'@'localhost';

修改用户密码

ALTER USER 'slave001'@'192.168.0.227' IDENTIFIED WITH mysql_native_password BY 'as123123';

你可能感兴趣的:(mysql,mysql,数据库,linux,主从模式,部署教程)