当前位置:   article > 正文

CentOS8安装Mysql8_centos8安装mysql8.0

centos8安装mysql8.0

CentOs安装Mysql8非常简,只需要下载、安装、配置自动启动这三个步骤即可。配置自动启动是可选项,一般来说都需要设置自动启动。

1、下载

打开mysql官网:MySQL

页面滚动到下方,点击:MySQL Community (GPL) Downloads »进入软件下载社区

点击MySQL Community Server

选择linux-Generic,选择需要下载的版本

2、安装

2.1解压文件

  1. # 解压至指定目录。
  2. tar -zvxf mysql-8.0.33-linux-glibc2.28-x86_64.tar.gz -C /usr/local
  3. # 修改mysql目录名称。
  4. mv /usr/local/mysql-8.0.33-linux-glibc2.28-x86_64 /usr/local/mysql

2.2配置环境变量

  1. # 使用vim命令打开环境变量配置文件
  2. vim /etc/profile
  3. # 按PageDown键,将光标移动到文件最后端键入下面两行内容。
  4. # 按i键进入编辑文档模式。即可输入内容。
  5. export MYSQL_HOME=/usr/local/mysql
  6. export PATH=$PATH:MYSQL_HOME/bin; # 将二进制执行路径拼接到原path后方。
  7. # 按键盘的Esc键退出编辑模式,并直接输入两个大写的字母Z,即可退出并保存该文件。
  8. # 然后使用source命令重新加载此配置文件使配置生效。
  9. source /etc/profile

2.3初始化数据库

  1. # 使用mysqld命令初始化数据库。 mysqld命令的路径已经被包含在环境配置中的PATH路径上了。所以这里可以直接执行。
  2. mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
  3. # 下面是输出内容
  4. 2023-05-29T03:49:56.481868Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.33) initializing of server in progress as process 39315
  5. 2023-05-29T03:49:56.489884Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
  6. 2023-05-29T03:49:57.417576Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
  7. 2023-05-29T03:49:59.479434Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: z/yH%7!EXj9L

2.4启动数据库

  1. # 进入数据库中的support-files目录
  2. cd /usr/local/mysql/support-files/
  3. # 使用mysql.server start 命令启动mysql服务
  4. ./mysql.server start
  5. # 这时一般会看到以下报错。
  6. # Starting MySQL.Logging to '/usr/local/mysql/data/VM-12-5-xxx.err'.
  7. #  ERROR! The server quit without updating PID file (/usr/local/mysql/data/VM-12-5-opencloudos.pid).
  8. # 这是由于mysql服务器启动失败导致的。我们需要修改一下这个文件,让他可以正常启动mysql服务。
  9. vim /usr/local/mysql/support-files/mysql.server
  10. # 在vim中 set nu打开行号显示
  11. :set nu
  12. # 找到 $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null & wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$? 这句话。笔者这里是266行的位置,读者需要自己在266行附近寻找
  13. # 这句命令会使用mysqld_safe来启动mysql服务,但如果使用root账号使用这个服务需要增加一个启动用户的参数。我们这里正巧使用的是root账号。所以对他进行如下修改。
  14. # 使用i键进入输入模式,在$bindir/mysqld_safe 后方,--datadir="$datadir"前方增加 --user=root这个参数如:$bindir/mysqld_safe --user=root --datadir="$datadir"
  15. # 修改好后按ESC键退出编辑模式,输入两个大写的Z字母退出并保存。
  16. # 然后我们再次启动服务。这次我们可以看到启动成功。
  17. ./mysql.server start 
  18. # Starting MySQL.Logging to '/usr/local/mysql/data/VM-12-5-opencloudos.err'.
  19. #  SUCCESS! 

2.5登录数据库

  1. mysql -u root -p
  2. # 输入你的默认密码,笔者这里是06A+0Rau9neo,密码输入正确后会看到以下信息
  3. # Enter password: 
  4. # Welcome to the MySQL monitor.  Commands end with ; or \g.
  5. # Your MySQL connection id is 8
  6. # Server version: 8.0.33
  7. # Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  8. # Oracle is a registered trademark of Oracle Corporation and/or its
  9. # affiliates. Other names may be trademarks of their respective
  10. # owners.
  11. # Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. # mysql> 
  13. # 由于给的默认密码太随机,根本记不住。所以我们需要给他改一个正常的密码。
  14. alter user 'root'@'localhost' identified by 'root';
  15. # 语法没问题就可以看见 Query OK, 0 rows affected (0.01 sec)。
  16. # 这时我们使用exit命令退出数据库并重新使用新密码链接。发现已经可以正常使用。
  17. exit
  18. mysql -u root -p

3、配置服务器开机自启动

  1. # 将mysql启动文件丢到init.d目录中
  2. cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
  3. # 配置mysql自启
  4. chkconfig --add mysqld
  5. chkconfig mysqld on

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

闽ICP备14008679号