CenterOS安装MySql8

2021/9/27 mysqlmysql8

官网安装介绍 (opens new window)

# 一、下载 MySQL

网址:https://dev.mysql.com/downloads/mysql/

# 二、选择版本

  • Select Operating System: 选择 Red Hat ,CentOS 是基于红帽的
  • Select OS Version: 选择 linux7 然后选择RPM bundle

image

# 三、默认数据库处理

通过 rpm -qa | grep mariadb 命令查看有没有默认的数据库安装包 mariadb

rpm -e "上面那个包的全称" --nodeps 卸载默认数据库

再用rpm -qa | grep mariadb 命令查看 mariadb 是否卸载
1
2
3
4
5

# 四、解压安装

通过 tar -xvf mysql-8.0.11-1.el7.x86_64.rpm-bundle.tar  命令解压 tar 包

解压后的包列表:
mysql-community-client-8.0.11-1.el7.x86_64.rpm
mysql-community-libs-8.0.11-1.el7.x86_64.rpm 
mysql-community-server-minimal-8.0.11-1.el7.x86_64.rpm
mysql-community-common-8.0.11-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.11-1.el7.x86_64.rpm
mysql-community-test-8.0.11-1.el7.x86_64.rpm
mysql-community-devel-8.0.11-1.el7.x86_64.rpm 
mysql-community-minimal-debuginfo-8.0.11-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.11-1.el7.x86_64.rpm
mysql-community-server-8.0.11-1.el7.x86_64.rpm

其中最简单的安装mysql-server服务,只需要安装如下4个软件包即可,我们可以使用rpm -ivh命令进行安装

sudo rpm -ivh mysql-community-common-8.0.11-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-libs-8.0.11-1.el7.x86_64.rpm --(依赖于common)
sudo rpm -ivh mysql-community-client-8.0.11-1.el7.x86_64.rpm --(依赖于libs)
sudo rpm -ivh mysql-community-server-8.0.11-1.el7.x86_64.rpm --(依赖于client、common)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

ps: 以上安装根据版本不同和机器的不同会提示缺少依赖包,根据提示进行安装即可,如果提示缺少的是mysql-XXXX这样的就看解压出来的有没有,有就安装,如果提示 缺少的是系统包如libaio.so.1(LIBAIO_0.4)(64bit) is needed by。。。。,使用命令安装依赖即可:

yum install libaio
1

# 五、配置文件修改

没有修改的配置文件如下:








 




 




 
 
 




 

 
 
 
 
 

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

# 1、大小写敏感

如果你不开启大小写不敏感,sql语句中的表名、字段名需要和数据库中的大小写一致,否则会报表不存在或者字段不存在!!!



 

vim /etc/my.cnf
# 在文件最后添加,关闭大小写敏感
lower_case_table_names=1
1
2
3

# 2、数据库数据存储位置

可以根据自己数据盘位置修改,因为我的服务器就一个盘,使用的默认的

datadir=/var/lib/mysql
1

# 六、初始化数据库

mysqld --initialize
//该命令会在/var/log/mysqld.log生成随机密码,
//可通过 cat /var/log/mysqld.log | grep password 命令查看数据库的密码
1
2
3

# 七、修改文件权限

修改mysql数据库目录(上面步骤如果修改了,请改成自己的)的所属用户及其所属组,然后启动mysql数据库

chown mysql:mysql /var/lib/mysql -R

systemctl start mysqld.service

systemctl enable mysqld;//开机启动
1
2
3
4
5

# 八、修改默认密码

更改root用户的密码,新版的mysql必须先修改root用户的密码,否则登录后是不能执行任何命令的

通过 cat /var/log/mysqld.log | grep password 命令查看数据库的密码

通过 mysql -uroot -p 敲回车键进入数据库登陆界面

通过 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root'; 命令来修改密码

通过 exit; 命令退出 MySQL,然后通过新密码再次登陆
1
2
3
4
5
6
7

如果报错:ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 执行如下语句:

mysql> set global validate_password.length=4;
mysql> set global validate_password.policy=0;
1
2

# 九、远程可访问

# 实现远程连接(授权法)
# 将host字段的值改为%就表示在任何客户端机器上能以root用户登录到mysql服务器,建议在开发时设为%。
update user set host ='%' where user = 'root';
# 刷新privileges
flush privileges;
# 如果提示ERROR 1046 (3D000): No database selected 执行use mysql; 选择数据库即可
1
2
3
4
5
6

# 十、其他说明

开启防火墙,允许非本机可访问数据库,下面只介绍iptables和firewalld两种防火墙。

  • iptables
# 启动防火墙并设置开机启动
systemctl enable iptables;
systemctl start iptables;

# 编辑防火墙,添加端口
vim /etc/sysconfig/iptables

# 在相关位置,写入以下内容
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

# 重启防火墙使配置生效
systemctl restart iptables.service 
1
2
3
4
5
6
7
8
9
10
11
12
  • firewalld
# 启动防火墙
systemctl start firewalld
或着
service firewalld start

# 设置开机启动
systemctl enable firewalld
# 开放端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
# 或者指定某IP可访问
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.111" port protocol="tcp" port="3306" accept"
# 重新加载配置使防火墙生效
firewall-cmd --reload
1
2
3
4
5
6
7
8
9
10
11
12
13

参数说明

  • --zone #作用域
  • --add-port=3306/tcp #添加端口,格式为:端口/通讯协议
  • --permanent #代表永久生效,没有此参数重启后失效

PS:如果是服务器的话,如果还是本地访问不到,那还需要再去看看服务器是不是有安全策略没有开通3306端口

# 十一、参考地址

https://blog.csdn.net/smiles13/article/details/80476665 (opens new window) https://www.cnblogs.com/lhdz_bj/p/9921436.html (opens new window) https://www.cnblogs.com/yanglang/p/10782941.html (opens new window)

# 十二、MAC相关知识

Mac的底层是Unix,所以Mac上的使用类似Linux,记录在这

1、启动mysql

sudo /usr/local/mysql/support-files/mysql.server start
2、停止mysql

sudo /usr/local/mysql/support-files/mysql.server stop
3、重启mysql

sudo /usr/local/mysql/support-files/mysql.server restart
1
2
3
4
5
6
7
8
9
更新日期: 2022/4/27 下午2:27:30