在Centos上安装MySQL

发布于:2022-12-17 ⋅ 阅读:(697) ⋅ 点赞:(0)

目录

  1. 下载安装包
  2. 上传并解压
  3. 卸载之前的MySQL数据库
  4. 安装MySQL
  5. 数据库初始化
  6. 设置MySQL远程登陆账号
  7. 远程连接验证

1.下载安装包

官网下载地址:MySQL :: Download MySQL Community Server

选择操作系统和操作系统的版本

选择RPM Bundle安装,点击下载

 

2.上传并解压

这里我选择的使用的是Xftp工具将安装包上传到Centos7上,上传的目录为:/root/bigdata

解压命令:tar  -xvf  mysql-8.0.30-1.el7.x86_64.rpm-bundle.tar 

3.卸载之前安装过的MySQL

  • 查看是否有MySQL

        命令:rpm -qa | grep -i mysql

 

 这里显然没有安装过MySQL。所以直接安装MySQL。如果显示有MySQL的话,我们需要进行如下操作。

  • 执行卸载mysql

        rpm -e mysql-community-common.xxx

  • 查看MySQL相关的文件存放路径

        whereis mysql 或者执行:find / -name mysql

  • 删除MySQL相关的文件

         rm MySQL文件路径 -rf

4.安装MySQL

按照顺序依次执行如下命令:

  • rpm -ivh mysql-community-common-8.0.30-1.el7.x86_64.rpm 

  • rpm -ivh mysql-community-client-plugins-8.0.30-1.el7.x86_64.rpm

  • rpm -ivh mysql-community-libs-8.0.30-1.el7.x86_64.rpm 

        执行这条命令时,报错了。

warning: mysql-community-libs-8.0.30-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
error: Failed dependencies:
    mariadb-libs is obsoleted by mysql-community-libs-8.0.30-1.el7.x86_64


原因:Centos7自带的mariadb数据库,里面的libs与安装的MySQL数据库的libs相冲突

解决步骤:把Centos7自带的mariadb数据库给删除掉,在重新执行上面的安装命令

删除mariadb数据库:yum remove mysql-libs

 重新执行上面的安装命令:rpm -ivh mysql-community-libs-8.0.30-1.el7.x86_64.rpm 

 

  •  rpm -ivh mysql-community-client-8.0.30-1.el7.x86_64.rpm 

  • yum install net-tools -y

执行后遇到了如下报错:

 原因:yum在安装包的过程中,虽然已经联网,但是没法解析远程包管理库对应的域名,所以我们只需要在网络配置中添加上DNS对应的ip地址即可。之前在设置IP地址的时候,我设置的DNS1=8.8.8.8 这次执行时解析不到,所以我又加了 DNS2=4.2.2.2  这次就可以了。

解决办法:

vi /etc/sysconfig/network-scripts/ifcfg-ens33

在文件末尾添加   DNS2=4.2.2.2

然后重启网络    systemctl restart network.service

 最后重新执行: yum install net-tools -y

  • rpm -ivh mysql-community-icu-data-files-8.0.30-1.el7.x86_64.rpm 

  • rpm -ivh mysql-community-server-8.0.30-1.el7.x86_64.rpm

执行后又报错了!!!!

内容如下:

warning: mysql-community-server-8.0.30-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
error: Failed dependencies:
    /usr/bin/perl is needed by mysql-community-server-8.0.30-1.el7.x86_64
    perl(Getopt::Long) is needed by mysql-community-server-8.0.30-1.el7.x86_64
    perl(strict) is needed by mysql-community-server-8.0.30-1.el7.x86_64

 原因:是因为我们缺少了perl.x86_64这个依赖。

解决办法;

查找依赖对应包      yum search perl

yum安装对应包        yum install -y perl-Module-Install.noarch

 

重新执行: rpm -ivh mysql-community-server-8.0.30-1.el7.x86_64.rpm

5.MySQL数据库初始化

  • 初始化数据库      mysqld --initialize --console
  • 通过chown改变文件的拥有者和群组。

        cd /var/lib  在执行: chown -R mysql:mysql /var/lib/mysql/

  • 启动MySQL服务

        启动MySQL:systemctl start mysqld

        查看MySQL状态: systemctl status mysqld

running: 表示启动成功

  • 加密      mysql_secure_installation

执行后报错了,如下:

解决方案: 在/etc/my.cnf文件尾加skip-grant-tables

vi /etc/my.cnf    添加skip-grant-tables

  • 免密登录mysql,设置mysql的root账号为空

先启动MySQL服务    service mysqld start

进入MySQL       mysql

输入如下命令:use mysql;   select host,user,authentication_string,plugin from user;

exit退出MySQL。

 重新进入MySQL,输入如下命令:use mysql;  

select host,user,authentication_string,plugin from user;

  • 退出mysql, 删除/etc/my.cnf文件最后的 skip-grant-tables 重启mysql服务;

重启MySQL    service mysqld start

  • 用户自己定义一个root密码

第一步:输入:mysql_secure_installation

后面细节如下:

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

  • 设置开机mysql自启动         systemctl enable mysqld     

 6.设置MySQL远程登录账号

  •   MySQL root 账号登录  mysql -u root -p Czw_123456
  • 查看用户表     user mysql;  select host,user,authentication_string,plugin from user;
  • 创建可远程连接用户   
mysql> create user 'root'@'%' identified by 'Czw_123456';
Query OK, 0 rows affected (0.46 sec)

mysql> grant all privileges on *.* to 'root'@'%' with grant option;
Query OK, 0 rows affected (0.16 sec)

mysql> alter user 'root'@'%' identified with mysql_native_password by 'Czw_123456';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

 7.远程连接验证

刚开始连接时,出现了2002报错:can't connect to sercer on ip 10060

原因是:没有添加mysql端口3306

解决办法:输入:firewall-cmd --zone=public --add-port=3306/tcp --permanent

                            firewall-cmd --reload

[root@master mysql]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@master mysql]# firewall-cmd --reload
success
[root@master mysql]# 

重新连接成功!!!!

  

欧耶!!!

 


网站公告

今日签到

点亮在社区的每一天
去签到