当前位置:   article > 正文

CentOS7安装Mysql完整步骤_mysql centos7

mysql centos7

在CentOS中默认安装有MariaDB,是MySQL的一个分支,主要由开源社区维护。
CentOS 7及以上版本已经不再使用MySQL数据库,而是使用MariaDB数据库。
如果直接安装MySQL,会和MariaDB的文件冲突。
因此,需要先卸载自带的MariaDB,再安装MySQL。

1.检查是否已经安装过MySQL

  1. [root@icoolkj soft]# rpm -qa | grep mysql
  2. [root@icoolkj soft]# rpm -qa | grep mariadb
  3. mariadb-libs-5.5.60-1.el7_5.x86_64
  4. [root@icoolkj soft]#

2.卸载MariaDB,并检查是否卸载干净

  1. [root@icoolkj soft]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
  2. [root@icoolkj soft]# rpm -qa | grep mariadb
  3. [root@icoolkj soft]#

3.查找遗留mysql文件进行删除

  1. [root@icoolkj soft]# find / -name mysql
  2. /etc/selinux/targeted/active/modules/100/mysql
  3. /usr/lib64/mysql
  4. [root@icoolkj soft]# rm -rf /etc/selinux/targeted/active/modules/100/mysql
  5. [root@icoolkj soft]# rm -rf /usr/lib64/mysql
  6. [root@icoolkj soft]# find / -name mysql
  7. [root@icoolkj soft]#

4.MySQL官网下载地址 ,下载所需版本的安装包

MySQL :: Download MySQL Community Server

 5. 解压文件,并重命名

  1. ## .tar.gz后缀:tar -zxvf 文件名
  2. ## .tar.xz后缀:tar -Jxvf 文件名
  3. [root@icoolkj data]# ll
  4. total 590704
  5. drwx------. 2 root root 16384 Nov 18 11:31 lost+found
  6. -rw-r--r--. 1 root root 604856088 Nov 19 20:31 mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
  7. drwxr-xr-x. 2 root root 4096 Nov 18 11:41 soft
  8. [root@icoolkj data]# tar -Jxvf mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
  9. ## 重命名
  10. [root@icoolkj data]# mv mysql-8.0.31-linux-glibc2.12-x86_64/ mysql8
  11. [root@icoolkj data]# ll
  12. total 24
  13. drwx------. 2 root root 16384 Nov 18 11:31 lost+found
  14. drwxr-xr-x. 9 root root 4096 Nov 19 20:41 mysql8
  15. drwxr-xr-x. 2 root root 4096 Nov 19 20:42 soft
  16. [root@icoolkj data]#

6.创建用户组和用户

  1. ## 创建用户组groupadd:mysql
  2. [root@icoolkj mysql8]# groupadd mysql
  3. ## 创建系统用户useradd:mysql,指定用户组为 mysql
  4. ## -r:创建系统用户
  5. ## -g:指定用户组
  6. [root@icoolkj mysql8]# useradd -r -g mysql mysql
  7. [root@icoolkj mysql8]#

 7.创建数据目录,并授权

  1. [root@icoolkj mysql8]# ll
  2. total 316
  3. drwxr-xr-x. 2 7161 31415 4096 Sep 14 04:08 bin
  4. drwxr-xr-x. 2 7161 31415 4096 Sep 14 04:08 docs
  5. drwxr-xr-x. 3 7161 31415 4096 Sep 14 04:08 include
  6. drwxr-xr-x. 6 7161 31415 4096 Sep 14 04:08 lib
  7. -rw-r--r--. 1 7161 31415 287627 Sep 14 00:15 LICENSE
  8. drwxr-xr-x. 4 7161 31415 4096 Sep 14 04:08 man
  9. -rw-r--r--. 1 7161 31415 666 Sep 14 00:15 README
  10. drwxr-xr-x. 28 7161 31415 4096 Sep 14 04:08 share
  11. drwxr-xr-x. 2 7161 31415 4096 Sep 14 04:08 support-files
  12. [root@icoolkj mysql8]# mkdir datas
  13. ## 更改属主和数组
  14. [root@icoolkj mysql8]# chown -R mysql:mysql /data/mysql8/datas
  15. ## 更改模式
  16. [root@icoolkj mysql8]# chmod -R 750 /data/mysql8/datas
  17. [root@icoolkj mysql8]# ll
  18. total 320
  19. drwxr-xr-x. 2 7161 31415 4096 Sep 14 04:08 bin
  20. drwxr-x---. 2 mysql mysql 4096 Nov 19 21:08 datas
  21. drwxr-xr-x. 2 7161 31415 4096 Sep 14 04:08 docs
  22. drwxr-xr-x. 3 7161 31415 4096 Sep 14 04:08 include
  23. drwxr-xr-x. 6 7161 31415 4096 Sep 14 04:08 lib
  24. -rw-r--r--. 1 7161 31415 287627 Sep 14 00:15 LICENSE
  25. drwxr-xr-x. 4 7161 31415 4096 Sep 14 04:08 man
  26. -rw-r--r--. 1 7161 31415 666 Sep 14 00:15 README
  27. drwxr-xr-x. 28 7161 31415 4096 Sep 14 04:08 share
  28. drwxr-xr-x. 2 7161 31415 4096 Sep 14 04:08 support-files
  29. [root@icoolkj mysql8]#

8.配置参数

在/data/mysql8/下,创建my.cnf配置文件,用于初始化MySQL数据库。

  1. # 添加配置文件
  2. [root@icoolkj mysql8]# vi my.cnf
  3. # For advice on how to change settings please see
  4. # https://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
  5. [mysqld]
  6. # Remove leading # to set options mainly useful for reporting servers.
  7. # The server defaults are faster for transactions and fast SELECTs.
  8. # Adjust sizes as needed, experiment to find the optimal values.
  9. # join_buffer_size = 128M
  10. # sort_buffer_size = 2M
  11. # read_rnd_buffer_size = 2M
  12. bind-address=0.0.0.0
  13. port=3306
  14. #use=mysql
  15. socket=/tmp/mysql.sock
  16. # 安装目录
  17. basedir=/data/mysql8
  18. # 数据存放目录
  19. datadir=/data/mysql8/datas/mysql
  20. log-bin=/data/mysql8/datas/mysql-bin
  21. innodb_data_home_dir=/data/mysql8/datas/mysql
  22. innodb_log_group_home_dir=/data/mysql8/datas/mysql
  23. # 日志及进程数据的存放目录
  24. log-error=/data/mysql8/datas/mysql/mysql.log
  25. pid-file=/data/mysql8/datas/mysql/mysql.pid
  26. # 服务端使用的字符集默认为8比特编码
  27. character_set_server=utf8mb4
  28. # Disabling symbolic-links is recommended to prevent assorted security risks
  29. symbolic-links=0
  30. explicit_defaults_for_timestamp=true
  31. [root@icoolkj mysql8]#

9.初始化

  1. # 进入MySQL的bin路径下
  2. [root@icoolkj bin]# pwd
  3. /data/mysql8/bin
  4. # 初始化
  5. [root@icoolkj bin]# ./mysqld --defaults-file=/data/mysql8/my.cnf --basedir=/data/mysql8/ --datadir=/data/mysql8/datas/mysql --user=mysql --initialize-insecure
  6. [root@icoolkj bin]#
  7. ## 参数(重要)
  8. # defaults-file:指定配置文件(要放在–initialize 前面)
  9. # user: 指定用户
  10. # basedir:指定安装目录
  11. # datadir:指定初始化数据目录
  12. # intialize-insecure:初始化无密码
  13. ## 查看mysql数据目录下情况
  14. [root@icoolkj bin]# cd /data/mysql8/datas/
  15. [root@icoolkj datas]# ll
  16. total 12
  17. drwxr-x---. 7 mysql mysql 4096 Nov 19 22:02 mysql
  18. -rw-r-----. 1 mysql mysql 180 Nov 19 22:02 mysql-bin.000001
  19. -rw-r-----. 1 mysql mysql 36 Nov 19 22:02 mysql-bin.index
  20. [root@icoolkj datas]# cd mysql
  21. [root@icoolkj mysql]# ll
  22. total 78280
  23. -rw-r-----. 1 mysql mysql 56 Nov 19 22:02 auto.cnf
  24. -rw-------. 1 mysql mysql 1676 Nov 19 22:02 ca-key.pem
  25. -rw-r--r--. 1 mysql mysql 1112 Nov 19 22:02 ca.pem
  26. -rw-r--r--. 1 mysql mysql 1112 Nov 19 22:02 client-cert.pem
  27. -rw-------. 1 mysql mysql 1676 Nov 19 22:02 client-key.pem
  28. -rw-r-----. 1 mysql mysql 196608 Nov 19 22:02 #ib_16384_0.dblwr
  29. -rw-r-----. 1 mysql mysql 8585216 Nov 19 22:02 #ib_16384_1.dblwr
  30. -rw-r-----. 1 mysql mysql 5554 Nov 19 22:02 ib_buffer_pool
  31. -rw-r-----. 1 mysql mysql 12582912 Nov 19 22:02 ibdata1
  32. drwxr-x---. 2 mysql mysql 4096 Nov 19 22:02 #innodb_redo
  33. drwxr-x---. 2 mysql mysql 4096 Nov 19 22:02 #innodb_temp
  34. drwxr-x---. 2 mysql mysql 4096 Nov 19 22:02 mysql
  35. -rw-r-----. 1 mysql mysql 25165824 Nov 19 22:02 mysql.ibd
  36. -rw-r-----. 1 mysql mysql 986 Nov 19 22:02 mysql.log
  37. drwxr-x---. 2 mysql mysql 4096 Nov 19 22:02 performance_schema
  38. -rw-------. 1 mysql mysql 1680 Nov 19 22:02 private_key.pem
  39. -rw-r--r--. 1 mysql mysql 452 Nov 19 22:02 public_key.pem
  40. -rw-r--r--. 1 mysql mysql 1112 Nov 19 22:02 server-cert.pem
  41. -rw-------. 1 mysql mysql 1676 Nov 19 22:02 server-key.pem
  42. drwxr-x---. 2 mysql mysql 4096 Nov 19 22:02 sys
  43. -rw-r-----. 1 mysql mysql 16777216 Nov 19 22:02 undo_001
  44. -rw-r-----. 1 mysql mysql 16777216 Nov 19 22:02 undo_002
  45. [root@icoolkj mysql]#

 10.启动mysql

  1. [root@icoolkj bin]# cd /data/mysql8/bin/
  2. ## 通过 MySQL的 bin路径下 mysqld_safe,用于后台安全启动MySQL。
  3. [root@icoolkj bin]# ./mysqld_safe --defaults-file=/data/mysql8/my.cnf &
  4. [1] 102020
  5. [root@icoolkj bin]# 2022-11-19T14:09:31.569559Z mysqld_safe Logging to '/data/mysql8/datas/mysql/mysql.log'.
  6. 2022-11-19T14:09:31.619918Z mysqld_safe Starting mysqld daemon with databases from /data/mysql8/datas/mysql
  7. ^C
  8. ## 查看是否启动
  9. [root@icoolkj bin]# ps -ef | grep mysql
  10. root 102020 94046 0 22:09 pts/1 00:00:00 /bin/sh ./mysqld_safe --defaults-file=/data/mysql8/my.cnf
  11. mysql 102254 102020 2 22:09 pts/1 00:00:01 /data/mysql8/bin/mysqld --defaults-file=/data/mysql8/my.cnf --basedir=/data/mysql8 --datadir=/data/mysql8/datas/mysql --plugin-dir=/data/mysql8/lib/plugin --user=mysql --log-error=/data/mysql8/datas/mysql/mysql.log --pid-file=/data/mysql8/datas/mysql/mysql.pid --socket=/tmp/mysql.sock --port=3306
  12. root 102349 94046 0 22:10 pts/1 00:00:00 grep --color=auto mysql
  13. [root@icoolkj bin]#

11.登录mysql

  1. ## 无密码登录
  2. [root@icoolkj bin]# ./mysql -u root --skip-password
  3. mysql: [Warning] Using a password on the command line interface can be insecure.
  4. Welcome to the MySQL monitor. Commands end with ; or \g.
  5. Your MySQL connection id is 8
  6. Server version: 8.0.31 MySQL Community Server - GPL
  7. Copyright (c) 2000, 2022, 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>

12.设置root密码

  1. ## 修改密码
  2. mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
  3. Query OK, 0 rows affected (0.01 sec)
  4. ## 刷新权限
  5. mysql> FLUSH PRIVILEGES;
  6. Query OK, 0 rows affected (0.00 sec)
  7. ## 设置允许远程登录,到mysql里执行
  8. mysql> use mysql
  9. Reading table information for completion of table and column names
  10. You can turn off this feature to get a quicker startup with -A
  11. Database changed
  12. mysql> update user set user.Host='%'where user.User='root';
  13. Query OK, 1 row affected (0.01 sec)
  14. Rows matched: 1 Changed: 1 Warnings: 0
  15. mysql> flush privileges;
  16. Query OK, 0 rows affected (0.01 sec)
  17. mysql> quit
  18. Bye
  19. [root@icoolkj bin]#

到此,mysql数据库就装好了,可以Navicat上测试连接。

13.设置mysql开机自启

  1. ## 1.将服务文件拷贝到/etc/init.d下,并重命名为mysqld
  2. [root@icoolkj support-files]# pwd
  3. /data/mysql8/support-files
  4. [root@icoolkj support-files]# ll
  5. total 20
  6. -rwxr-xr-x. 1 7161 31415 1061 Sep 14 00:15 mysqld_multi.server
  7. -rw-r--r--. 1 7161 31415 2027 Sep 14 03:43 mysql-log-rotate
  8. -rwxr-xr-x. 1 7161 31415 10576 Sep 14 03:43 mysql.server
  9. [root@icoolkj support-files]# cp mysql.server /etc/init.d/mysqld
  10. ## 2.赋予mysql文件可执行权限
  11. [root@icoolkj support-files]# chmod +x /etc/init.d/mysqld
  12. ## 3.添加mysql服务
  13. [root@icoolkj support-files]# chkconfig --add mysqld
  14. ## 4.查看服务列表
  15. [root@icoolkj support-files]# chkconfig --list
  16. Note: This output shows SysV services only and does not include native
  17. systemd services. SysV configuration data might be overridden by native
  18. systemd configuration.
  19. If you want to list systemd services use 'systemctl list-unit-files'.
  20. To see services enabled on particular target use
  21. 'systemctl list-dependencies [target]'.
  22. ds_agent 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  23. mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  24. netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
  25. network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  26. [root@icoolkj support-files]#
  27. ## 5.如果看到mysql的服务3,4,5都是on的话则成功,否则执行以下命令
  28. [root@icoolkj support-files]# chkconfig --level 345 mysqld on
  29. ## 6.重启服务器
  30. [root@icoolkj support-files]# reboot
  31. ## 7.重启以后查看进程,mysql是否自启成功
  32. [root@icoolkj support-files]# ps -ef | grep mysql

14.mysql启动相关问题

  1. ## 问题1:mysql不能正常启动
  2. [root@icoolkj support-files]# ./mysql.server start
  3. ./mysql.server: line 239: my_print_defaults: command not found
  4. ./mysql.server: line 259: cd: /usr/local/mysql: No such file or directory
  5. Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)
  6. [root@icoolkj support-files]#
  7. ## 原因:启动时没找见(/usr/local/mysql/bin/mysqld_safe)这个命令,目前安装路径不是默认的;
  8. ## 解决方式:配置my.cnf文件的basedir为当前的安装路径(mysql5.7某版本之后不提供my-default.cnf文件了,将建好的my.cnf文件拷贝到/etc下)
  9. ## 问题2:启动提示某些地址找不到
  10. [root@icoolkj ~]# service mysql status
  11. Redirecting to /bin/systemctl status mysql.service
  12. ● mysqld.service - LSB: start and stop MySQL
  13. Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
  14. Active: active (exited) since Sun 2022-11-20 09:14:50 CST; 1h 30min ago
  15. Docs: man:systemd-sysv-generator(8)
  16. Process: 16330 ExecStop=/etc/rc.d/init.d/mysqld stop (code=exited, status=0/SUCCESS)
  17. Process: 52996 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
  18. Nov 20 09:14:50 icoolkj systemd[1]: Starting LSB: start and stop MySQL...
  19. Nov 20 09:14:50 icoolkj mysqld[52996]: /etc/rc.d/init.d/mysqld: line 239: my_print_defaults: command not found
  20. Nov 20 09:14:50 icoolkj mysqld[52996]: /etc/rc.d/init.d/mysqld: line 259: cd: /usr/local/mysql: No such file or directory
  21. Nov 20 09:14:50 icoolkj mysqld[52996]: Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)
  22. Nov 20 09:14:50 icoolkj systemd[1]: Started LSB: start and stop MySQL.
  23. [root@icoolkj ~]#
  24. ## 原因:因为新版本的mysql安全启动安装包只认/usr/local/mysql这个路径。
  25. ## 解决方式:建立软连接
  26. [root@icoolkj ~]# cd /usr/local
  27. [root@icoolkj local]# ll
  28. total 0
  29. drwxr-xr-x. 2 root root 6 Apr 11 2018 bin
  30. drwxr-xr-x. 2 root root 6 Apr 11 2018 etc
  31. drwxr-xr-x. 2 root root 6 Apr 11 2018 games
  32. drwxr-xr-x. 2 root root 6 Apr 11 2018 include
  33. drwxr-xr-x. 2 root root 6 Apr 11 2018 lib
  34. drwxr-xr-x. 2 root root 6 Apr 11 2018 lib64
  35. drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec
  36. drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin
  37. drwxr-xr-x. 5 root root 49 Dec 26 2019 share
  38. drwxr-xr-x. 2 root root 6 Apr 11 2018 src
  39. [root@icoolkj local]# mkdir mysql
  40. [root@icoolkj local]# cd mysql
  41. [root@icoolkj mysql]# ln -s /data/mysql8/bin/mysqld mysqld
  42. [root@icoolkj mysql]# ll
  43. total 0
  44. lrwxrwxrwx. 1 root root 23 Nov 20 10:59 mysqld -> /data/mysql8/bin/mysqld
  45. [root@icoolkj mysql]# service mysql restart
  46. Redirecting to /bin/systemctl restart mysql.service
  47. [root@icoolkj mysql]# service mysql status
  48. Redirecting to /bin/systemctl status mysql.service
  49. ● mysqld.service - LSB: start and stop MySQL
  50. Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
  51. Active: active (running) since Sun 2022-11-20 10:59:57 CST; 5s ago
  52. Docs: man:systemd-sysv-generator(8)
  53. Process: 60070 ExecStop=/etc/rc.d/init.d/mysqld stop (code=exited, status=0/SUCCESS)
  54. Process: 60104 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
  55. CGroup: /system.slice/mysqld.service
  56. ├─60121 /bin/sh /data/mysql8/bin/mysqld_safe --datadir=/data/mysql8/datas/mysql --pid-file=/data/mysql8/datas/mysql/mysql.pid
  57. └─60384 /data/mysql8/bin/mysqld --basedir=/data/mysql8 --datadir=/data/mysql8/datas/mysql --plugin-dir=/data/mysql8/lib/plugin --user=mysql --log-error=/data/mysql8/datas/mysql/mysql.log --pid-file=/da...
  58. Nov 20 10:59:55 icoolkj systemd[1]: Starting LSB: start and stop MySQL...
  59. Nov 20 10:59:57 icoolkj mysqld[60104]: Starting MySQL.. SUCCESS!
  60. Nov 20 10:59:57 icoolkj systemd[1]: Started LSB: start and stop MySQL.
  61. [root@icoolkj mysql]#

15.设置命令全局(将mysql添加到环境路径中)

  1. ## 设置命令全局(将mysql添加到环境路径中)
  2. [root@icoolkj bin]# ln -s /data/mysql8/bin/mysql /usr/bin/
  3. [root@icoolkj bin]#

16.mysql8密码重置

  1. ## 1.在配置文件关闭密码登陆
  2. ## 找到mysql配置文件:my.cnf
  3. ## [mysqld] 下 添加skip-grant-tables 保存退出
  4. [root@icoolkj ~]# vi /etc/my.cnf
  5. [root@icoolkj ~]# cat /etc/my.cnf
  6. [mysqld]
  7. skip-grant-tables
  8. ## 重启mysql服务: systemctl mysqld restart
  9. [root@icoolkj ~]# service mysqld restart
  10. Shutting down MySQL. SUCCESS!
  11. Starting MySQL.. SUCCESS!
  12. [root@icoolkj ~]#
  13. ## 2.将原密码置空
  14. [root@icoolkj ~]# mysql -u root;
  15. Welcome to the MySQL monitor. Commands end with ; or \g.
  16. Your MySQL connection id is 9
  17. Server version: 8.0.31 MySQL Community Server - GPL
  18. Copyright (c) 2000, 2022, Oracle and/or its affiliates.
  19. Oracle is a registered trademark of Oracle Corporation and/or its
  20. affiliates. Other names may be trademarks of their respective
  21. owners.
  22. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  23. mysql> update user set authentication_string="" where user = "root";
  24. ERROR 1046 (3D000): No database selected
  25. mysql> use mysql
  26. Reading table information for completion of table and column names
  27. You can turn off this feature to get a quicker startup with -A
  28. Database changed
  29. mysql> update user set authentication_string="" where user = "root";
  30. Query OK, 0 rows affected (0.01 sec)
  31. Rows matched: 1 Changed: 0 Warnings: 0
  32. mysql>
  33. ## exit/quit;(两者都是退出的意思,任选其一即可)
  34. ## 3.配置文件重新开启密码登陆
  35. ## 找到mysql配置文件: my.cnf
  36. ## 删除步骤1添加的 skip-grant-tables 保存退出
  37. ## 重启服务: service mysqld restart
  38. ## 4.进入mysql修改密码
  39. [root@icoolkj ~]# mysql -u root
  40. Welcome to the MySQL monitor. Commands end with ; or \g.
  41. Your MySQL connection id is 8
  42. Server version: 8.0.31 MySQL Community Server - GPL
  43. Copyright (c) 2000, 2022, Oracle and/or its affiliates.
  44. Oracle is a registered trademark of Oracle Corporation and/or its
  45. affiliates. Other names may be trademarks of their respective
  46. owners.
  47. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  48. mysql> use mysql
  49. Reading table information for completion of table and column names
  50. You can turn off this feature to get a quicker startup with -A
  51. Database changed
  52. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
  53. Query OK, 0 rows affected (0.01 sec)
  54. mysql> flush privileges;
  55. Query OK, 0 rows affected (0.01 sec)
  56. mysql> quit;
  57. Bye
  58. [root@icoolkj ~]#
  59. ## 使用密码登陆:mysql -u root -p;

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

闽ICP备14008679号