1.安装MySQL
1.1 更换源指定安装版本
wgethttp://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
yumlocalinstall mysql57-community-release-el7-8.noarch.rpm
yum repolist enabled | grep"mysql.*-community.*"
1.2安装mysql
yum install mysql-community-server
1.3 开启数据库,并找到指定密码登录,创建账号
systemctl start mysqld
grep 'temporary password' /var/log/mysqld.log #初始密码
mysql -uroot -p
mysql>create user root;
mysql>grant all privileges on *.* to root@'%' identified by 'Juzi@2018';
mysql>flush peivileges;
mysql>exit
mysql -uroot -pJuzi@123 #重新登录
1.4 mysql5.7账号密码登录优化
在安装完成mysql5.7之后.登录会默认修改密码,修改密码过程中可能会报错,myslq5.7对密码有一定要求
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
#报错 这个其实与<span style="color:#ff0000;">validate_password_policy</span>的值有关
validate_password_policy有以下取值:
Policy | Tests Performed |
---|---|
0 or LOW |
Length |
1 or MEDIUM |
Length; numeric, lowercase/uppercase, and special characters |
2 or STRONG |
Length; numeric, lowercase/uppercase, and special characters; dictionary file |
默认是1,即MEDIUM,所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。
有时候,只是为了自己测试,不想密码设置得那么复杂,譬如说,我只想设置root的密码为123456。
必须修改两个全局参数:
首先,修改validate_password_policy参数的值
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
这样,判断密码的标准就基于密码的长度了。这个由validate_password_length参数来决定
![](/qrcode.jpg)
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| 8 |
+----------------------------+
1 row in set (0.00 sec)
validate_password_length参数默认为8,它有最小值的限制,最小值为:
- validate_password_number_count
- + validate_password_special_char_count
- + (2 * validate_password_mixed_case_count)
其中,validate_password_number_count指定了密码中数据的长度,validate_password_special_char_count指定了密码中特殊字符的长度,validate_password_mixed_case_count指定了密码中大小字母的长度。
这些参数,默认值均为1,所以validate_password_length最小值为4,如果你显性指定validate_password_length的值小于4,尽管不会报错,但validate_password_length的值将设为4。如下所示:
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| 8 |
+----------------------------+
1 row in set (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| 4 |
+----------------------------+
1 row in set (0.00 sec)
如果修改了validate_password_number_count,validate_password_special_char_count,validate_password_mixed_case_count中任何一个值,则validate_password_length将进行动态修改。
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| 4 |
+----------------------------+
row in set (0.00 sec)
mysql> select @@validate_password_mixed_case_count;
+--------------------------------------+
| @@validate_password_mixed_case_count |
+--------------------------------------+
| 1 |
+--------------------------------------+
row in set (0.00 sec)
mysql> set global validate_password_mixed_case_count=2;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@validate_password_mixed_case_count;
+--------------------------------------+
| @@validate_password_mixed_case_count |
+--------------------------------------+
| 2 |
+--------------------------------------+
row in set (0.00 sec)
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| 6 |
+----------------------------+
row in set (0.00 sec)
当然,前提是validate_password插件必须已经安装,MySQL5.7是默认安装的。
那么如何验证validate_password插件是否安装呢?可通过查看以下参数,如果没有安装,则输出将为空。
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password_dictionary_file | |
| validate_password_length | 6 |
| validate_password_mixed_case_count | 2 |
| validate_password_number_count | 1 |
| validate_password_policy | LOW |
| validate_password_special_char_count | 1 |
+--------------------------------------+-------+
6 rows in set (0.00 sec
2.搭建主从
2.1修改主库配置文件
[root@5haocentos ~]# cat /etc/my.cnf
[mysqld]
server-id=1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
port=33080
log-bin=mysql-bin
log-bin-index=master-bin.index
binlog_format=mixed
binlog-do-db=bgy
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
#密码永久不过期配置
default_password_lifetime=0
#skip-grant-tables
symbolic-links=0
log-error=/var/log/mysqld.log
#pid-file=/var/run/mysqld/mysqld.pid
pid-file=/var/lib/mysql/mysqld.pid
wait_timeout=31536000
interactive_timeout=31536000
2.2登录创建备份账号 锁表
mysql>create user backup;
mysql>grant all privileges on *.* to 'backup'@'192.168.0.%' identified by 'Yuan@123';
mysql>flush privileges;
mysql>flush tables with read lock; #锁表
2.3修改从库配置文件
[root@6haocentos ~]# cat /etc/my.cnf
[mysqld]
server-id=2
log-bin=mysql-bin
binlog_format=mixed
relay-log=slave-relay-bin
relay-log-index=slave-relay-bin.index
binlog-do-db=bgy
replicate_wild_ignore_table=mysql.%
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
port=33080
innodb_checksum_algorithm=innodb
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
2.4在从库上配置主库信息(根据个人设置 )
mysql> reset maste;
mysql> change master to master_host='192.168.0.5',master_user='backup',master_password='****',master_log_file='mysql-bin.000006',master_log_pos='154',master_port=3306;
2.5主库解锁
unlock tables
2.6开启主从 查看状态
mysql> start slave;
mysql>show slave status\G;
若出现
则主从搭建完成