参考:http://www.runoob.com/mysql/mysql-install.html
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum update
yum install mysql-server
赋权限
chown mysql:mysql -R /var/lib/mysql
初始化:
mysqld --initialize
启动
systemctl start mysqld
查看运行状态:
systemctl status mysqld
查看版本:
mysqladmin --version
给mysql设置密码
mysqladmin -u root password "new_password";
登录:
mysql -u root -p
Enter password:*******
mysql默认不允许远程登录,设置允许任何远程都可以连接:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'new_password' WITH GRANT OPTION;
重载授权表
FLUSH PRIVILEGES
如果为云服务器需要设置安全组,允许任何主机访问云服务器3306端口
问题:mysql默认时区为CST,会导致mysql中展示的时间没问题,取出来的时间相差13个小时
解决:
show variables like
"%time_zone%"
; #查看时区
如果time_zone的值是system,那么就会导致上面的问题,需要改为东八区
1.修改mysql配置文件:vim /etc/my.cnf
2.在[mysqld]区域中加上:
default
-time_zone =
'+8:00'
3.重启mysql
--------------------------------------------------------------------主从配置---------------------------------------------------------------------
查看mysql版本:mysql> status;
主节点:172.16.0.6
从节点:172.16.0.4
mysql版本:5.6.42 MySQL Community Server (GPL)
linux:centos7
1.编辑主节点配置文件:
vim /etc/my.cnf
2.添加如下内容:
log-bin=mysql-bin
server-id=1
skip_name_resolve=ON
innodb-file-per-table=ON
binlog_format=mixed
3.重启mysql,查看是否开启二进制日志文件
##重启
systemctl restart mysqld
##登录mysql后查看日志
mysql> show GLOBAL VARIABLES like '%log%';
##运行之后的结果中有如下这一行:
| log_bin | ON |
##查看二进制日志列表
show master LOGS;
##运行之后的结果:
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 259 |
+------------------+-----------+
1 row in set (0.00 sec)
##查看主节点id:
show GLOBAL VARIABLES like '%server%';
##结果:
+---------------------------------+--------------------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------------------+
| character_set_server | latin1 |
| collation_server | latin1_swedish_ci |
| innodb_ft_server_stopword_table | |
| server_id | 1 |
| server_id_bits | 32 |
| server_uuid | 29a7c6b0-1a51-11e9-8e77-fa163ecae677 |
+---------------------------------+--------------------------------------+
6 rows in set (0.00 sec)
##在主节点创建有复制权限的用户repluser
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'172.16.0.4' IDENTIFIED BY 'repluserpassword'
##刷新权限
FLUSH PRIVILEGES
##主节点配置完成
4.配置从节点文件:
vim /etc/my.cnf
server-id=2
relay-log=relay-log
relay-log-index=relay-log.index
innodb_file_per_table=ON
skip_name_resolve=ON
5.保存后,重启
systemctl restart mysqld
6.查看中继日志是否开启
##查看:
mysql> show GLOBAL VARIABLES like '%log%';
##结果中包含:
| relay_log | relay-log |
| relay_log_basename | /var/lib/mysql/relay-log |
| relay_log_index | /var/lib/mysql/relay-log.index |
| relay_log_info_file | relay-log.info |
| relay_log_info_repository | FILE |
##查看从节点id
mysql> show GLOBAL VARIABLES like '%server%'
##在从节点配置访问主节点的参数信息
##添加 主节点主机,访问主节点的用户名及密码,主节点二进制文件信息。
##注意:主节点的二进制文件一定要是二进制列表中的最后一个二进制文件(需要设置主节点的二进制文件和pos,通过show master LOGS查看)
mysql> CHANGE MASTER TO MASTER_HOST='172.16.0.6',MASTER_USER='repluser',MASTER_PASSWORD='repluserpassword', MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=259;
7.查看从节点状态信息:
mysql> show slave status\G;
##结果中包含(no两个复制线程没有启动):
Slave_IO_Running: No
Slave_SQL_Running: No
##启动复制线程
##START SLAVE 可以指定线程类型:IO_THREAD ,SQL_THREAD, 如果不指定,则两个都启动
mysql> START SLAVE
##再次查看从节点状态信息:
mysql> show slave status\G;
##结果中包含:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
8.至此,mysql的主----从复制配置完成。
参考:https://blog.csdn.net/daicooper/article/details/79905660
-------------------------------------------------------------------主从配置错误排查---------------------------------------------------------------------
1.现象:
mysql> start slave;
ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository
解决:
mysql> reset slave; //清空中继日志
mysql> start slave;
参考:https://www.cnblogs.com/mysql-dba/p/7201513.html
2.现象:
如果主节点有些数据没写入二进制日志,同步从节点时会报错
解决:清空主节点的二进制日志
mysql> reset master
参考:https://blog.csdn.net/lxpbs8851/article/details/7328916