一、安装mysql
1.1 mysql下载链接:
https://downloads.mysql.com/archives/community/
1.2 解压包并创建相应的数据目录
tar -xvf mysql-8.2.0-linux-glibc2.28-x86_64.tar.xz -C /usr/local
cd /usr/local/
mv mysql-8.2.0-linux-glibc2.28-x86_64/ mysql
mkdir /usr/local/mysql/data/
groupadd mysql
useradd mysql -g mysql
chown -R mysql:mysql /usr/local/mysql
1.3 添加配置文件
vim my.cnf
[client]
port = 3306
socket = /usr/local/mysql/mysql.sock
[mysql]
prompt = "\u@mysqldb \R:\m:\s [\d]> "
no_auto_rehash
loose-skip-binary-as-hex
[mysqld]
user = mysql
port = 3306
#主从复制或MGR集群中,server_id记得要不同
#另外,实例启动时会生成 auto.cnf,里面的 server_uuid 值也要不同
#server_uuid的值还可以自己手动指定,只要符合uuid的格式标准就可以
server_id = 3306
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket = /usr/local/mysql/mysql.sock
pid-file = /var/run/mysqld/mysqld.pid
character_set_server = UTF8MB4
skip_name_resolve = 1
# 不区分大小写
lower_case_table_names=1
#log settings
log_timestamps = SYSTEM
log-error = /var/log/mysqld.log
log_error_verbosity = 3
slow_query_log = 1
log_slow_extra = 1
log_slave_updates=1 # 事务记录到binlog,否则无法开启并发复制
long_query_time = 0.1
log_queries_not_using_indexes = 1
log_throttle_queries_not_using_indexes = 60
min_examined_row_limit = 100
log_slow_admin_statements = 1
log_slow_slave_statements = 1
log_bin = binlog
binlog_format = ROW
sync_binlog = 1 #MGR环境中由其他节点提供容错性,可不设置双1以提高本地节点性能
binlog_cache_size = 4M
max_binlog_cache_size = 2G
max_binlog_size = 1G
binlog_rows_query_log_events = 1
binlog_expire_logs_seconds = 604800
#MySQL 8.0.22前,想启用MGR的话,需要设置binlog_checksum=NONE才行
# binlog_checksum = CRC32
gtid_mode = ON
enforce_gtid_consistency = TRUE
#myisam settings
key_buffer_size = 32M
myisam_sort_buffer_size = 128M
[mysqldump]
quick
1.4 初始化mysql
cd /usr/local/mysql/bin/
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --user=mysql --initialize
1.5 启动mysql
#将mysql启动脚本拷贝到系统目录
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
#将mysql加到启动服务列表
chkconfig --add mysql
#开机启动
chkconfig mysql on
#启动mysql
service mysql start
如果遇到报错:
Starting MySQL...... ERROR! The server quit without updating PID file (/var/run/mysqld/mysqld.pid).
解决:
mkdir /var/run/mysqld
chown -R mysql:mysql /var/run/mysqld
service mysql start #启动之后看到SUCCESS,可以查看端口和进程确保正常
1.6 查看初始化的密码,修改root密码
查看密码
cat /var/log/mysqld.log | grep password | grep root@localhost
修改密码
/usr/local/mysql/bin/mysqladmin -u root -p'lu7&uMfsK(Rs' password 'root'
ln -s /usr/local/mysql/bin/mysql /usr/bin
mysql -uroot proot
Enter password:
二、mysql主从复制搭建
2.1 两台机器配置解析
vim /etc/hosts
172.26.21.26 jtkl-ext-mysql1
172.26.21.27 jtkl-ext-mysql2
2.2 主库上操作
vim /etc/my.cnf
[mysqld]
log-bin=/var/log/mysql/mysql-bin #添加此配置
server-id=26 #修改server-id 3306--->26
创建完日志目录、授权之后重启服务
mkdir /var/log/mysql
chown mysql.mysql /var/log/mysql
service mysql stop
service mysql start
mysql -uroot -proot
执行sql
mysql> CREATE USER 'mysql'@'%' identified by 'mysql';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'mysql'@'%';
mysql> flush privileges;
mysql> show master status\G #记录File和Positoin后面的内容
2.3 从库上操作
vim /etc/my.cnf
[mysqld]
server-id=27 #修改server-id 3306--->27
重启服务
service mysql stop
service mysql start
mysql -uroot proot
mysql> \e
粘贴以下内容:根据实际修改并删除注释
CHANGE MASTER TO
MASTER_HOST='jtkl-ext-mysql1', #修改为master的主机名
MASTER_USER='mysql', #主库上创建的用户
MASTER_PASSWORD='mysql', #用户密码
MASTER_LOG_FILE='binlog.000005', #主库上show master status\G显示的file的内容
MASTER_LOG_POS=702; #主库上show master status\G显示到Positoin的内容
以上内容wq保存,退出后
-> ;
mysql> start slave;
mysql> show slave status\G #查看双yes,则成功。否则失败
2.4 测试:
主库执行
mysql> create database test;
从库查看:
mysql> show databases;