当前位置:   article > 正文

Linux 安装Mysql8.0_linux安装mysql8.0

linux安装mysql8.0

目录

一、官网下载Mysql安装包

​二、解压安装

三、创建mysql用户组及用户

四、初始化数据库

五、配置Mysql

六、建立Mysql服务

七、修改密码

八、开启远程访问

九、关闭Linux防火墙


一、官网下载Mysql安装包

我的环境是Centos选择对应系统

MySQL :: Download MySQL Community Server

二、解压安装

上传文件至服务器,本次演示mysql上传位置:/appusr/apphome

1.解压执行命令:tar -xvf mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz

tar -xvf mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz

2.重命名为:mysql-8.0

mv mysql-8.0.32-linux-glibc2.12-x86_64 mysql-8.0

3.进入mysql-8.0的目录,新建data文件夹

  1. [root@centos-tianmen apphome]# cd mysql-8.0/
  2. [root@centos-tianmen mysql-8.0]# mkdir data
  3. [root@centos-tianmen mysql-8.0]# ll
  4. 总用量 296
  5. drwxr-xr-x. 2 7161 31415 4096 12月 17 00:54 bin
  6. drwxr-xr-x. 2 root root 6 3月 19 14:42 data
  7. drwxr-xr-x. 2 7161 31415 56 12月 17 00:54 docs
  8. drwxr-xr-x. 3 7161 31415 4096 12月 17 00:54 include
  9. drwxr-xr-x. 6 7161 31415 201 12月 17 00:54 lib
  10. -rw-r--r--. 1 7161 31415 283374 12月 16 23:34 LICENSE
  11. drwxr-xr-x. 4 7161 31415 30 12月 17 00:54 man
  12. -rw-r--r--. 1 7161 31415 666 12月 16 23:34 README
  13. drwxr-xr-x. 28 7161 31415 4096 12月 17 00:54 share
  14. drwxr-xr-x. 2 7161 31415 77 12月 17 00:54 support-files

4.新增tmp、log目录后面使用

  1. [root@centos-tianmen mysql-8.0]# mkdir log
  2. [root@centos-tianmen mysql-8.0]# mkdir tmp

三、创建mysql用户组及用户

1.新增用户组mysql

groupadd mysql

2.新增用户mysql 密码mysql

useradd -g mysql mysql

3.授权,指向mysql的安装目录

chown -R mysql.mysql /appusr/apphome/mysql-8.0/

四、初始化数据库

1.进入mysql安装目录,我的安装目录为:/appusr/apphome/mysql-8.0

  1. [root@centos-tianmen mysql-8.0]# cd /appusr/apphome/mysql-8.0/
  2. [root@centos-tianmen mysql-8.0]# pwd
  3. /appusr/apphome/mysql-8.0

2.初始化数据库

执行命令:./bin/mysqld --user=mysql --basedir=/appusr/apphome/mysql-8.0/ --datadir=/appusr/apphome/mysql-8.0/data/ --initialize ;

  1. [root@centos-tianmen mysql-8.0]# ./bin/mysqld --user=mysql --basedir=/appusr/apphome/mysql-8.0/ --datadir=/appusr/apphome/mysql-8.0/data/ --initialize ;
  2. 2023-03-19T08:42:40.094583Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
  3. 2023-03-19T08:42:40.094889Z 0 [System] [MY-013169] [Server] /appusr/apphome/mysql-8.0/bin/mysqld (mysqld 8.0.32) initializing of server in progress as process 53132
  4. 2023-03-19T08:42:40.116956Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
  5. 2023-03-19T08:42:40.999879Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
  6. 2023-03-19T08:42:43.037019Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: =cFY9lpinfc6

记住临时密码:=cFY9lpinfc6

五、配置Mysql

1.修改配置文件

vi /etc/my.cnf

my.cnf配置文件自用:

  1. #[mysqld]
  2. #datadir=/var/lib/mysql
  3. #socket=/var/lib/mysql/mysql.sock
  4. # Disabling symbolic-links is recommended to prevent assorted security risks
  5. #symbolic-links=0
  6. # Settings user and group are ignored when systemd is used.
  7. # If you need to run mysqld under a different user or group,
  8. # customize your systemd unit file for mariadb according to the
  9. # instructions in http://fedoraproject.org/wiki/Systemd
  10. #[mysqld_safe]
  11. #log-error=/var/log/mariadb/mariadb.log
  12. #pid-file=/var/run/mariadb/mariadb.pid
  13. #
  14. # include all files from the config directory
  15. #
  16. #!includedir /etc/my.cnf.d
  17. [mysql]
  18. #basedir=/appusr/apphome/mysql-8.0
  19. #datadir=/appusr/apphome/mysql-8.0/data/
  20. socket=/appusr/apphome/mysql-8.0/tmp/mysql.sock
  21. port=3306
  22. user=mysql
  23. #skip-grant-tables
  24. # 指定日志时间为系统时间
  25. #log_timestamps=SYSTEM
  26. #log-error=/appusr/apphome/mysql-8.0/log/mysql.err
  27. # # 指定字符集为utf8,因为mysql8.0中的默认字符集为utfmb4,会和其他程序引起兼容性问题
  28. default-character-set=utf8
  29. #
  30. #
  31. [mysqld]
  32. basedir=/appusr/apphome/mysql-8.0
  33. datadir=/appusr/apphome/mysql-8.0/data
  34. socket=/appusr/apphome/mysql-8.0/tmp/mysql.sock
  35. port=3306
  36. user=mysql
  37. log_timestamps=SYSTEM
  38. collation-server = utf8_unicode_ci
  39. character-set-server = utf8
  40. # # 指定默认认证的加密方式,mysql8.0中默认方式为caching_sha2_password,引起老版本兼容性问题
  41. default_authentication_plugin= mysql_native_password
  42. #skip-grant-tables
  43. [mysqld_safe]
  44. log-error=/appusr/apphome/mysql-8.0/log/mysqld_safe.err
  45. pid-file=/appusr/apphome/mysql-8.0/tmp/mysqld.pid
  46. socket=/appusr/apphome/mysql-8.0/tmp/mysql.sock
  47. #skip-grant-tables
  48. [mysql.server]
  49. #basedir=/appusr/apphome/mysql-8.0
  50. datadir=/appusr/apphome/mysql-8.0/data
  51. socket=/appusr/apphome/mysql-8.0/tmp/mysql.sock
  52. port=3306
  53. user=mysql
  54. #skip-grant-tables
  55. [mysqladmin]
  56. socket=/appusr/apphome/mysql-8.0/tmp/mysql.sock

六、建立Mysql服务

1.进入mysql安装目录

cd /appusr/apphome/mysql-8.0/

2. 添加Mysql到系统服务

  1. cp -a ./support-files/mysql.server /etc/init.d/mysql
  2. chmod +x /etc/init.d/mysql
  3. chkconfig --add mysql

3.检查服务是否生效

chkconfig --list mysql

如图:

  1. [root@centos-tianmen mysql-8.0]# cp -a ./support-files/mysql.server /etc/init.d/mysql
  2. [root@centos-tianmen mysql-8.0]# chmod +x /etc/init.d/mysql
  3. [root@centos-tianmen mysql-8.0]# chkconfig --add mysql
  4. [root@centos-tianmen mysql-8.0]# chkconfig --list mysql
  5. 注:该输出结果只显示 SysV 服务,并不包含
  6. 原生 systemd 服务。SysV 配置数据
  7. 可能被原生 systemd 配置覆盖。
  8. 要列出 systemd 服务,请执行 'systemctl list-unit-files'
  9. 查看在具体 target 启用的服务请执行
  10. 'systemctl list-dependencies [target]'
  11. mysql 0:关 1:关 2:开 3:开 4:开 5:开 6:关
  12. [root@centos-tianmen mysql-8.0]#

4.启动mysql服务

service mysql start;

如有报错文件不存在,新建缺失的文件,授权mysql  

  1. [root@centos-tianmen mysql-8.0]# service mysql start;
  2. Starting MySQL... SUCCESS!

5.查看服务状态

service mysql status;
  1. [root@centos-tianmen mysql-8.0]# service mysql status;
  2. SUCCESS! MySQL running (54691)

七、修改密码

1.进入mysql安装目录的bin目录

cd /appusr/apphome/mysql-8.0/bin/

2.使用上面的临时密码登录,执行命令

./mysql -uroot -p

如图: 

  1. [root@centos-tianmen bin]# ./mysql -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 8
  5. Server version: 8.0.32
  6. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql>

3.修改密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';

OK,到此安装完成了!

八、开启远程访问

1.在登录状态执行下述SQL

  1. mysql> CREATE USER 'root'@'%' IDENTIFIED BY '密码';
  2. Query OK, 0 rows affected (0.03 sec)
  3. mysql> GRANT ALL ON *.* TO 'root'@'%';
  4. Query OK, 0 rows affected (0.02 sec)
  5. mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码';
  6. Query OK, 0 rows affected (0.01 sec)
  7. mysql> FLUSH PRIVILEGES;
  8. Query OK, 0 rows affected (0.02 sec)

九、关闭Linux防火墙

1.查看防火墙状态

systemctl status firewalld.service
  1. [root@centos-tianmen bin]# systemctl status firewalld.service
  2. ● firewalld.service - firewalld - dynamic firewall daemon
  3. Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
  4. Active: active (running) since 日 2023-03-19 13:39:15 CST; 4h 52min ago
  5. Docs: man:firewalld(1)
  6. Main PID: 704 (firewalld)
  7. Tasks: 2
  8. CGroup: /system.slice/firewalld.service
  9. └─704 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
  10. 3月 19 13:39:11 centos-tianmen systemd[1]: Starting firewalld - dynamic firewall daemon...
  11. 3月 19 13:39:15 centos-tianmen systemd[1]: Started firewalld - dynamic firewall daemon.
  12. 3月 19 13:39:15 centos-tianmen firewalld[704]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future release. Please conside...abling it now.
  13. Hint: Some lines were ellipsized, use -l to show in full.

2.目前开启状态,关闭防火墙

systemctl stop firewalld.service

3.再查看防火墙状态,已经关闭

  1. [root@centos-tianmen bin]# systemctl status firewalld.service
  2. ● firewalld.service - firewalld - dynamic firewall daemon
  3. Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
  4. Active: inactive (dead) since 日 2023-03-19 18:31:56 CST; 1s ago
  5. Docs: man:firewalld(1)
  6. Process: 704 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
  7. Main PID: 704 (code=exited, status=0/SUCCESS)
  8. 3月 19 13:39:11 centos-tianmen systemd[1]: Starting firewalld - dynamic firewall daemon...
  9. 3月 19 13:39:15 centos-tianmen systemd[1]: Started firewalld - dynamic firewall daemon.
  10. 3月 19 13:39:15 centos-tianmen firewalld[704]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future release. Please conside...abling it now.
  11. 3月 19 18:31:50 centos-tianmen systemd[1]: Stopping firewalld - dynamic firewall daemon...
  12. 3月 19 18:31:56 centos-tianmen systemd[1]: Stopped firewalld - dynamic firewall daemon.
  13. Hint: Some lines were ellipsized, use -l to show in full.

4.设置永久关闭防火墙

systemctl disable firewalld.service
  1. [root@centos-tianmen bin]# systemctl disable firewalld.service
  2. Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
  3. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

5.链接Mysql

 OK,完活

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号