mysql安装配置

mysql安装配置

1.获取mysql软件安装包

1.1.从官网获取

1
2
3
4
5
6
7
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-community-common-8.0.33-1.el7.x86_64.rpm
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-community-icu-data-files-8.0.33-1.el7.x86_64.rpm
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-community-libs-8.0.33-1.el7.x86_64.rpm
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-community-client-plugins-8.0.33-1.el7.x86_64.rpm
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-community-client-plugins-8.0.33-1.el7.x86_64.rpm

rpm -ivh *.rpm

1.2 从yum源获取

2.安装软件包

1
sudo yum install mysql

2.启动mysql

1
2
sudo systemctl restart mysqld
#启动mysql后mysql会自动初始化数据库

3.修改密码

获取密码

1
tail /var/log/mysqld.log

修改密码

1
2
3
4
5
6
7
8
#登录mysql
mysql -uroot -p
#mysql
alter user'root'@'localhost' identified by '新密码';
#mariadb
UPDATE mysql.user SET password = PASSWORD('root') WHERE user = 'root';
#刷新
flush privileges

如果出现修改密码不符合策略

1
2
3
4
5
#修改mysql配置文件
vim /etc/my.cnf
#加入以下两行
validate_password.policy=LOW
validate_password.length=0

4.mysql配置主从复制

4.1 mysql主从复制的原理
  • 1.mysql主库将数据的更改记录记录到二进制日志(binary log)中
  • 2.mysql从库将主库的更改记录的二进制日志同步到本地的中继日志(relay log)
  • 3.mysql从库读取中继日志,根据日志中的操作步骤,同步数据
4.2 配置主库

修改配置文件

1
2
3
4
5
6
7
8
[mysqld]
server-id=1
log-bin=localhost-binlog #日志文件

#需要同步的表,不配置默认同步所有数据库
binlog-do-db=db_a
binlog-do-db=db_b
binlog-do-db=db_c

创建从复制的用户

1
2
#创建slave用户
create user slave@'%' identified with mysql_native_password by 'slave';
4.3 配置从库
1
2
3
[mysqld]
server-id=2
#这个server-id要和主库不一样
1
2
3
4
5
6
7
change master to 
master_host='172.17.0.4',
master_port=3306,
master_user='slave',
master_password='slave',
master_log_file='logbin.000002',
master_log_pos=1825;#这个参数可以由主库show master status得到

开始复制

1
start slave

mysql安装配置
https://dreamaccount.github.io/2023/05/11/mysql安装配置/
作者
404NotFound
发布于
2023年5月11日
许可协议