Linux环境安装Mysql

一. mysql社区版安装:

1先检查有没有安装mariadb,有的话将其卸载,不然会和mysql冲突。

*1**)检查当前系统是否安装过**MySQL*

[root@wangxin mysql]# yum list | grep mariadb

mariadb-libs.x86_64                      1:5.5.68-1.el7                @anaconda
mariadb.x86_64                           1:5.5.68-1.el7                base
mariadb-bench.x86_64                     1:5.5.68-1.el7                base
mariadb-devel.i686                       1:5.5.68-1.el7                base
mariadb-devel.x86_64                     1:5.5.68-1.el7                base
mariadb-embedded.i686                    1:5.5.68-1.el7                base
mariadb-embedded.x86_64                  1:5.5.68-1.el7                base
mariadb-embedded-devel.i686              1:5.5.68-1.el7                base
mariadb-embedded-devel.x86_64            1:5.5.68-1.el7                base
mariadb-libs.i686                        1:5.5.68-1.el7                base
mariadb-server.x86_64                    1:5.5.68-1.el7                base
mariadb-test.x86_64                      1:5.5.68-1.el7                base
卸载mariadb,按Y确认。
[root@wangxin mysql]# rpm -qa|grep mariadb
[root@wangxin mysql]# yum remove mariadb-libs.x86_64

2.下载mysql社区版

https://dev.mysql/downloads/

选择版本,下载

点击download,可以下载到电脑,然后上传到服务器,也可以右键红色框选择复制链接。然后直接在服务器开始下载。

用wget命令直接在服务器下载。在哪个目录输入命令,就下载到当前目录。
[root@wangxin software]# wget https://dev.mysql/get/Downloads/MySQL-5.7/mysql-5.7.35-1.el7.x86_64.rpm-bundle.tar

3.解压mysql安装包:

-C 参数后面可以指定解压位置。
[root@wangxin software]# tar -xvf mysql-5.7.35-1.el7.x86_64.rpm-bundle.tar -C /opt/mysql/
[root@wangxin software]# cd ../mysql/
[root@wangxin mysql]# ls
mysql-community-client-5.7.35-1.el7.x86_64.rpm           mysql-community-embedded-devel-5.7.35-1.el7.x86_64.rpm
mysql-community-common-5.7.35-1.el7.x86_64.rpm           mysql-community-libs-5.7.35-1.el7.x86_64.rpm
mysql-community-devel-5.7.35-1.el7.x86_64.rpm            mysql-community-libs-compat-5.7.35-1.el7.x86_64.rpm
mysql-community-embedded-5.7.35-1.el7.x86_64.rpm         mysql-community-server-5.7.35-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.35-1.el7.x86_64.rpm  mysql-community-test-5.7.35-1.el7.x86_64.rpm
依次安装下面的安装包:
注意:按照顺序依次执行
[root@wangxin mysql]# rpm -ivh mysql-community-common-5.7.35-1.el7.x86_64.rpm
如果报出如下错误,是mariadb没卸载,参照第一步,
warning: mysql-community-common-5.7.35-1.el7.x86_64.rpm: Header V3 DSA/SHA256 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
        file /usr/share/mysql/czech/errmsg.sys from install of mysql-community-common-5.7.35-1.el7.x86_64 conflicts with file from package mariadb-libs-1:5.5.68-1.el7.x86_64


然后继续安装:
[root@wangxin mysql]# rpm -ivh mysql-community-libs-5.7.35-1.el7.x86_64.rpm

[root@wangxin mysql]# rpm -ivh mysql-community-libs-compat-5.7.35-1.el7.x86_64.rpm

[root@wangxin mysql]# rpm -ivh mysql-community-client-5.7.35-1.el7.x86_64.rpm

[root@wangxin mysql]# rpm -ivh mysql-community-server-5.7.35-1.el7.x86_64.rpm
若是这一步报下面的错误,需要安装libaio依赖,再次执行这一步,
[root@wangxin mysql]# rpm -ivh mysql-community-server-5.7.35-1.el7.x86_64.rpm
warning: mysql-community-server-5.7.35-1.el7.x86_64.rpm: Header V3 DSA/SHA256 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
        libaio.so.1()(64bit) is needed by mysql-community-server-5.7.35-1.el7.x86_64
        libaio.so.1(LIBAIO_0.1)(64bit) is needed by mysql-community-server-5.7.35-1.el7.x86_64
        libaio.so.1(LIBAIO_0.4)(64bit) is needed by mysql-community-server-5.7.35-1.el7.x86_64
安装libaio依赖,按Y确认。
 [root@wangxin mysql]# yum install libaio

4.配置mysql

查看/etc/myf文件,查看datadir指向的文件夹下面是否为空,不为空则清空此文件夹。一般是指向/var/lib/mysql
[root@wangxin mysql]# vi /etc/myf

[root@wangxin mysql]# cd /var/lib/mysql
[root@wangxin mysql]# ls
初始化数据库
[root@wangxin mysql]# mysqld --initialize --user=root
打开/var/log/mysqld.log,查看root的初始密码。把密码复制下来。
[root@wangxin mysql]# cat /var/log/mysqld.log

5.启动数据库

[root@wangxin mysql]# systemctl start mysqld
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
启动的时候报了一个错,这是因为数据库的文件夹权限不够所致。赋权限即可。然后再次运行数据库。
[root@wangxin mysql]# chmod -R 777 /var/lib/mysql
登录数据库:密码就是刚刚查看的随机密码。
[root@wangxin mysql]# mysql -uroot -p
修改密码:
mysql> set password = password("123456");
设置所有IP可以链接。
mysql> update mysql.user set host='%' where user='root';
刷新:
mysql> flush privileges;

6.修改连接端口号

打开文件 /etc/myf
vi /etc/myf
在【mysqld】下面加你想改的端口号,

然后重启数据库即可。
systemctl restart mysqld

端口号

打开文件 /etc/myf
vi /etc/myf
在【mysqld】下面加你想改的端口号,

[外链图片转存中…(img-5Td59DRW-1627548977504)]

然后重启数据库即可。
systemctl restart mysqld
数据库到这里就安装好了,可以用数据库连接工具链接一下试一试。

更多推荐

Linux环境安装Mysql(史上最详细教学)