阅读前了解一下mariadb与mysql的关系

centos7使用二进制包安装mysql

sudo groupadd mysql

sudo useradd -r -g mysql -s /bin/false mysql

cd /usr/local

sudo tar zxvf /path/to/mysql-VERSION-OS.tar.gz

sudo ln -s full-path-to-mysql-VERSION-OS mysql # 如果要删除软链接使用unlink mysql

cd mysql

sudo chown mysql:mysql -R /usr/local/mysql

sudo chmod 755 /usr/local/mysql/

修改/etc/my.cnf配置

[mysqld]

bind-address=0.0.0.0

port=3306

user=mysql

basedir=/usr/local/mysql

datadir=/usr/local/mysql/data

socket=/tmp/mysql.sock

log-error=/usr/local/mysql/data/mysql.err

pid-file=/usr/local/mysql/data/mysql.pid

#character config

#

character_set_server=utf8mb4

#

symbolic-links=0

#

#explicit_defaults_for_timestamp=true

#

#

# 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](http://fedoraproject.org/wiki/Systemd)

[mysqld_safe]

#start mysql service error log

log-error=/var/log/mariadb/mariadb.log

pid-file=/var/run/mariadb/mariadb.pid
sudo mkdir -p /var/log/mariadb/

sudo chown mysql:mysql -R /var/log/mariadb/

sudo chmod 755 /var/log/mariadb/

cd /usr/local/mysql/bin/

sudo ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql --initialize

如果遇到libaio.so.1没有,去下载,这是正常现象

sudo yum install -y libaio
sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

sudo ln -s /usr/local/mysql/bin/mysql /usr/bin

sudo service mysql start

如果报错: log-error set to ‘/var/log/mariadb/mariadb.log’, however file don’t exists. Create writable for user ‘mysql’.

则:

sudo touch /var/log/mariadb/mariadb.log

sudo chown -R mysql:mysql  /var/log/mariadb/
sudo cat /usr/local/mysql/data/mysql.err查看密码

sudo mysql -uroot -p'密码' 密码如果有特殊字符必须用'包裹

SET PASSWORD = PASSWORD('123456');

ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;

FLUSH PRIVILEGES;

use mysql;

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

FLUSH PRIVILEGES;

配置主从复制(使用GTID进行复制)

主服务器配置:

[mysqld]

server-id=28 #唯一标识符

gtid_mode=on #开启gtid

enforce_gtid_consistency=on # 阻止会影响复制正确性的语句执行也就是有些mysql语句会执行不了

log-bin=mysql-bin # enable binary logging

innodb_flush_log_at_trx_commit=1 #保障每个事务都被同步地写到磁盘

sync_binlog=1 #每次事务执行时都把二进制日志同步写入磁盘,防止在服务器崩溃时丢失事务

binlog_format=row #按行复制

skip_slave_start=on #Tells the replica server not to start the replication threads when the server starts.To start the threads later, use a START SLAVE statement.

log_slave_updates=on #从sql线程写入也会写到二进制日志

从服务器配置:

[mysqld]

server_id=29

gtid_mode=on

enforce_gtid_consistency=on

log-bin=mysql-bin

innodb_flush_log_at_trx_commit=1

sync_binlog=1

binlog_format=row

skip_slave_start=on

log_slave_updates=on

以下操作都是连接mysql后执行

检查GTID开启

show variables like '%gtid%';

主库建立授权用户

grant replication slave on *.* to 'slave'@'10.10.100.10' identified by 'slave_2022!';
FLUSH PRIVILEGES;

slave连接到master

CHANGE MASTER TO

MASTER_HOST='10.79.185.28',

MASTER_USER='slave',

MASTER_PASSWORD='slave_2022!',

MASTER_PORT=3306,

MASTER_AUTO_POSITION = 1;# 1 代表采用GTID协议复制 0 代表采用老的binlog复制

start slave;# 开启主从复制

master上查看salve信息:

show slave hosts;#(可能要等一会才有)

验证:

主数据库执行

CREATE SCHEMA `test1` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ;

查看从数据库是否有对于的库

额外:

迁移数据到新磁盘:

sudo service mysql stop

sudo mkdir -p /export/mysql

cd /usr/local/mysql

sudo cp -ar data /export/mysql/data

sudo ln -s /export/mysql/data /usr/local/mysql/data

(在从服务器上重新执行连接master的语句和start slave语句)

Starting MySQL… ERROR! The server quit without updating PID file (/usr/local/m ysql/data/mysql.pid). 如果修改my.cnf文件中有语法问题,也会报这个错。