下载地址:MySQL :: Download MySQL Community Server (Archived Versions)
文件名称:mysql-8.0.17-linux-glibc2.12-x86_64.tar.xz
先解压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的bin目录,执行mysql安装的初始化操作
./mysqld --user=mysql --basedir=/data/mysql8 --datadir=/data/mysql8/data/ --initialize
初始化完成后,会得一个临时密码,注意保存(sE&Pu?k+g8?d)(tNC81xKhTw)m)
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
切换到mysql8根目录,添加mysqld服务到系统
cp -a ./support-files/mysql.server /etc/init.d/mysql
授权并添加服务
chmod +x /etc/init.d/mysql
chkconfig --add mysql
启动mysql服务
service mysql start
把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/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
切换至/data/mysqlslave/bin目录,执行mysql安装的初始化操作
./mysqld --user=mysql --basedir=/data/mysqlslave --datadir=/data/mysqlslave/data/ --initialize
初始化完成后,会得一个临时密码,注意保存(S_fXXbUMK8b.)
启动mysql服务
service mysqlslave start
临时密码登陆
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
/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,则说明主从模式搭建成功了。
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';