当前位置:   article > 正文

Centos7环境下安装MySQL8详细教程_centos7 安装mysql8

centos7 安装mysql8

1、下载mysql安装包

下载哪个版本,首先需要确定一下系统的glibc版本,使用如下命令:

rpm -qa | grep glibc

​​​​​​​ 

2、检查是否安装过mysql

ps:因为以前用yum安装过,所以先用yum卸载。如果不是此方式或者没安装过则跳过 

  1. [root@localhost ~]# yum remove mysql
  2. 已加载插件:fastestmirror
  3. 参数 mysql 没有匹配
  4. 不删除任何软件包

 查看是否有mysql依赖

[root@localhost ~]# rpm -qa | grep mysql

如果有则卸载

  1. //普通删除模式
  2. rpm -e xxx(mysql_libs)
  3. //强力删除模式,如果上述命令删除时,提示有依赖其他文件,则可以用该命令对其进行强力删除
  4. rpm -e --nodeps xxx(mysql_libs)

3、检查是否有mariadb

  1. [root@localhost ~]# rpm -qa | grep mariadb
  2. mariadb-libs-5.5.68-1.el7.x86_64

如果有则卸载

  1. [root@localhost ~]# rpm -e --nodeps mariadb-libs
  2. [root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
  3. 错误:未安装软件包 mariadb-libs-5.5.68-1.el7.x86_64

4、安装mysql依赖包

[root@localhost home]# yum install libaio

5、解压

进入/opt目录下将mysql文件解压

  1. [root@localhost home]# cd /opt
  2. [root@localhost opt]# tar -xvf mysql-8.0.36-linux-glibc2.17-x86_64.tar
  3. mysql-test-8.0.36-linux-glibc2.17-x86_64.tar.xz
  4. mysql-8.0.36-linux-glibc2.17-x86_64.tar.xz
  5. mysql-router-8.0.36-linux-glibc2.17-x86_64.tar.xz
  6. [root@localhost opt]# tar -Jxvf mysql-8.0.36-linux-glibc2.17-x86_64.tar.xz
  7. [root@localhost opt]# mv mysql-8.0.36-linux-glibc2.17-x86_64 mysql

按照习惯,我们将文件移动到/usr/local目录下

[root@localhost opt]# mv /opt/mysql/ /usr/local/

我们切换到usr/local/目录下查看mysql是否存在

  1. [root@localhost opt]# cd /usr/local/
  2. [root@localhost local]# ll
  3. 总用量 0
  4. drwxr-xr-x. 2 root root 6 411 2018 bin
  5. drwxr-xr-x. 2 root root 6 411 2018 etc
  6. drwxr-xr-x. 2 root root 6 411 2018 games
  7. drwxr-xr-x. 2 root root 6 411 2018 include
  8. drwxr-xr-x. 2 root root 6 411 2018 lib
  9. drwxr-xr-x. 2 root root 6 411 2018 lib64
  10. drwxr-xr-x. 2 root root 6 411 2018 libexec
  11. drwxr-xr-x. 9 root root 129 42 21:20 mysql
  12. drwxr-xr-x. 11 root root 151 828 2023 nginx
  13. drwxr-xr-x. 2 root root 6 411 2018 sbin
  14. drwxr-xr-x. 5 root root 49 829 2023 share
  15. drwxr-xr-x. 2 root root 6 411 2018 src

创建数据库文件存放的文件夹。这个文件夹将来存放每个数据库的库文件

  1. [root@localhost local]# cd mysql
  2. [root@localhost mysql]# ls
  3. bin docs include lib LICENSE man README share support-files
  4. [root@localhost mysql]# mkdir mysqldb

7、mysql安装目录赋予权限

[root@localhost mysql]# chmod -R 777 /usr/local/mysql/

8、创建mysql组和用户

创建组

[root@localhost mysql]# groupadd mysql

创建用户(-s /bin/false参数指定mysql用户仅拥有所有权,而没有登录权限)

[root@localhost mysql]# useradd -r -g mysql -s /bin/false mysql

将用户添加到组中

[root@localhost mysql]# chown -R mysql:mysql ./

9、修改mysql配置文件

[root@localhost mysql]# vi /etc/my.cnf

将里面的命令都删除掉,然后添加以下命令,保存并退出(如果有一定经验,可以在里面添加一些其他的配置)

  1. [mysqld]
  2. # 设置3306端口
  3. port=3306
  4. # 设置mysql的安装目录
  5. basedir=/usr/local/mysql
  6. # 设置mysql数据库的数据的存放目录
  7. datadir=/usr/local/mysql/mysqldb
  8. # 允许最大连接数
  9. max_connections=10000
  10. # 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
  11. max_connect_errors=10
  12. # 服务端使用的字符集默认为UTF8
  13. character-set-server=utf8
  14. # 创建新表时将使用的默认存储引擎
  15. default-storage-engine=INNODB
  16. # 默认使用“mysql_native_password”插件认证
  17. default_authentication_plugin=mysql_native_password
  18. [mysql]
  19. # 设置mysql客户端默认字符集
  20. default-character-set=utf8
  21. [client]
  22. # 设置mysql客户端连接服务端时默认使用的端口
  23. port=3306
  24. default-character-set=utf8

10、安装mysql

进入mysql 安装目录下:

[root@localhost mysql]# cd /usr/local/mysql/bin/

安装mysql,并记住初始化随机密码

  1. [root@localhost bin]# ./mysqld --initialize --console
  2. 2024-04-02T13:25:55.133890Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead.
  3. 2024-04-02T13:25:55.133913Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.36) initializing of server in progress as process 2186
  4. 2024-04-02T13:25:55.139191Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
  5. 2024-04-02T13:25:55.154304Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
  6. 2024-04-02T13:25:55.706150Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
  7. 2024-04-02T13:25:57.058187Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: <;cdUJXy!91b
  8. 2024-04-02T13:25:57.159383Z 6 [Warning] [MY-013360] [Server] Plugin mysql_native_password reported: ''mysql_native_password' is deprecated and will be removed in a future release. Please use caching_sha2_password instead'

11、启动mysql服务

进入mysql.server服务目录下并启动服务

  1. [root@localhost bin]# cd /usr/local/mysql/support-files
  2. [root@localhost support-files]# ./mysql.server start
  3. Starting MySQL.Logging to '/usr/local/mysql/mysqldb/localhost.localdomain.err'.
  4. ERROR! The server quit without updating PID file (/usr/local/mysql/mysqldb/localhost.localdomain.pid).

 如果第一次启动,当初始化执行会有报错

此时不要担心,重新给mysql安装目录赋予一下权限后,再次执行。

  1. [root@localhost support-files]# chmod -R 777 /usr/local/mysql
  2. [root@localhost support-files]# ./mysql.server start
  3. Starting MySQL. SUCCESS!

12、将mysql添加到系统进程中

[root@localhost support-files]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

此时我们就可以使用服务进程操作mysql了

13、设置mysql自启动

  1. [root@localhost support-files]# chmod +x /etc/init.d/mysqld
  2. [root@localhost support-files]# systemctl enable mysqld
  3. mysqld.service is not a native service, redirecting to /sbin/chkconfig.
  4. Executing /sbin/chkconfig mysqld on

此时mysql自启动就已经设置好了

14、修改root用户登录密码

登录mysql

  1. [root@localhost support-files]# cd /usr/local/mysql/bin/
  2. [root@localhost bin]# ./mysql -u root -p

执行后,输入我们初始化时记录下的随机密码,就会进入mysql。

修改密码:

  1. mysql> alter user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
  2. Query OK, 0 rows affected (0.01 sec)

15、设置允许远程登录

  1. mysql> use mysql
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. mysql> update user set user.Host='%'where user.User='root';
  6. Query OK, 1 row affected (0.01 sec)
  7. Rows matched: 1 Changed: 1 Warnings: 0
  8. mysql> flush privileges;
  9. Query OK, 0 rows affected (0.00 sec)
  10. mysql> quit
  11. Bye

16、重启服务且测试

centos6与centos7的服务命令都支持

  1. [root@localhost bin]# systemctl restart mysql
  2. [root@localhost bin]# service mysql restart
  3. Redirecting to /bin/systemctl restart mysql.service

查看mysql是否启动

systemctl status mysql

查看防火墙开放端口

[root@localhost bin]# firewall-cmd --list-all

在防火墙中将3306端口开放

  1. [root@localhost bin]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
  2. success
  3. //--permanent为永久生效,没有此参数 服务器重启后配置失效
  4. [root@localhost bin]# firewall-cmd --reload
  5. success

在Navicat上测试连接

重启linux后测试自启动(可选)

reboot

 测试mysql服务是否自启动

测试远程访问

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/697962
推荐阅读
相关标签
  

闽ICP备14008679号