赞
踩
在CentOS中默认安装有MariaDB,是MySQL的一个分支,主要由开源社区维护。
CentOS 7及以上版本已经不再使用MySQL数据库,而是使用MariaDB数据库。
如果直接安装MySQL,会和MariaDB的文件冲突。
因此,需要先卸载自带的MariaDB,再安装MySQL。
- [root@icoolkj soft]# rpm -qa | grep mysql
- [root@icoolkj soft]# rpm -qa | grep mariadb
- mariadb-libs-5.5.60-1.el7_5.x86_64
- [root@icoolkj soft]#
- [root@icoolkj soft]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
- [root@icoolkj soft]# rpm -qa | grep mariadb
- [root@icoolkj soft]#
- [root@icoolkj soft]# find / -name mysql
- /etc/selinux/targeted/active/modules/100/mysql
- /usr/lib64/mysql
- [root@icoolkj soft]# rm -rf /etc/selinux/targeted/active/modules/100/mysql
- [root@icoolkj soft]# rm -rf /usr/lib64/mysql
- [root@icoolkj soft]# find / -name mysql
- [root@icoolkj soft]#
MySQL :: Download MySQL Community Server
- ## .tar.gz后缀:tar -zxvf 文件名
- ## .tar.xz后缀:tar -Jxvf 文件名
-
- [root@icoolkj data]# ll
- total 590704
- drwx------. 2 root root 16384 Nov 18 11:31 lost+found
- -rw-r--r--. 1 root root 604856088 Nov 19 20:31 mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
- drwxr-xr-x. 2 root root 4096 Nov 18 11:41 soft
- [root@icoolkj data]# tar -Jxvf mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
-
- ## 重命名
- [root@icoolkj data]# mv mysql-8.0.31-linux-glibc2.12-x86_64/ mysql8
- [root@icoolkj data]# ll
- total 24
- drwx------. 2 root root 16384 Nov 18 11:31 lost+found
- drwxr-xr-x. 9 root root 4096 Nov 19 20:41 mysql8
- drwxr-xr-x. 2 root root 4096 Nov 19 20:42 soft
- [root@icoolkj data]#
-

- ## 创建用户组groupadd:mysql
- [root@icoolkj mysql8]# groupadd mysql
- ## 创建系统用户useradd:mysql,指定用户组为 mysql
- ## -r:创建系统用户
- ## -g:指定用户组
- [root@icoolkj mysql8]# useradd -r -g mysql mysql
- [root@icoolkj mysql8]#
- [root@icoolkj mysql8]# ll
- total 316
- drwxr-xr-x. 2 7161 31415 4096 Sep 14 04:08 bin
- drwxr-xr-x. 2 7161 31415 4096 Sep 14 04:08 docs
- drwxr-xr-x. 3 7161 31415 4096 Sep 14 04:08 include
- drwxr-xr-x. 6 7161 31415 4096 Sep 14 04:08 lib
- -rw-r--r--. 1 7161 31415 287627 Sep 14 00:15 LICENSE
- drwxr-xr-x. 4 7161 31415 4096 Sep 14 04:08 man
- -rw-r--r--. 1 7161 31415 666 Sep 14 00:15 README
- drwxr-xr-x. 28 7161 31415 4096 Sep 14 04:08 share
- drwxr-xr-x. 2 7161 31415 4096 Sep 14 04:08 support-files
- [root@icoolkj mysql8]# mkdir datas
- ## 更改属主和数组
- [root@icoolkj mysql8]# chown -R mysql:mysql /data/mysql8/datas
- ## 更改模式
- [root@icoolkj mysql8]# chmod -R 750 /data/mysql8/datas
- [root@icoolkj mysql8]# ll
- total 320
- drwxr-xr-x. 2 7161 31415 4096 Sep 14 04:08 bin
- drwxr-x---. 2 mysql mysql 4096 Nov 19 21:08 datas
- drwxr-xr-x. 2 7161 31415 4096 Sep 14 04:08 docs
- drwxr-xr-x. 3 7161 31415 4096 Sep 14 04:08 include
- drwxr-xr-x. 6 7161 31415 4096 Sep 14 04:08 lib
- -rw-r--r--. 1 7161 31415 287627 Sep 14 00:15 LICENSE
- drwxr-xr-x. 4 7161 31415 4096 Sep 14 04:08 man
- -rw-r--r--. 1 7161 31415 666 Sep 14 00:15 README
- drwxr-xr-x. 28 7161 31415 4096 Sep 14 04:08 share
- drwxr-xr-x. 2 7161 31415 4096 Sep 14 04:08 support-files
- [root@icoolkj mysql8]#

在/data/mysql8/下,创建my.cnf配置文件,用于初始化MySQL数据库。
- # 添加配置文件
- [root@icoolkj mysql8]# vi my.cnf
- # For advice on how to change settings please see
- # https://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
-
- [mysqld]
- # Remove leading # to set options mainly useful for reporting servers.
- # The server defaults are faster for transactions and fast SELECTs.
- # Adjust sizes as needed, experiment to find the optimal values.
- # join_buffer_size = 128M
- # sort_buffer_size = 2M
- # read_rnd_buffer_size = 2M
- bind-address=0.0.0.0
- port=3306
- #use=mysql
- socket=/tmp/mysql.sock
-
- # 安装目录
- basedir=/data/mysql8
-
- # 数据存放目录
- datadir=/data/mysql8/datas/mysql
- log-bin=/data/mysql8/datas/mysql-bin
- innodb_data_home_dir=/data/mysql8/datas/mysql
- innodb_log_group_home_dir=/data/mysql8/datas/mysql
-
- # 日志及进程数据的存放目录
- log-error=/data/mysql8/datas/mysql/mysql.log
- pid-file=/data/mysql8/datas/mysql/mysql.pid
-
- # 服务端使用的字符集默认为8比特编码
- character_set_server=utf8mb4
-
- # Disabling symbolic-links is recommended to prevent assorted security risks
- symbolic-links=0
- explicit_defaults_for_timestamp=true
-
-
- [root@icoolkj mysql8]#

- # 进入MySQL的bin路径下
- [root@icoolkj bin]# pwd
- /data/mysql8/bin
- # 初始化
- [root@icoolkj bin]# ./mysqld --defaults-file=/data/mysql8/my.cnf --basedir=/data/mysql8/ --datadir=/data/mysql8/datas/mysql --user=mysql --initialize-insecure
- [root@icoolkj bin]#
-
- ## 参数(重要)
- # defaults-file:指定配置文件(要放在–initialize 前面)
- # user: 指定用户
- # basedir:指定安装目录
- # datadir:指定初始化数据目录
- # intialize-insecure:初始化无密码
-
-
- ## 查看mysql数据目录下情况
- [root@icoolkj bin]# cd /data/mysql8/datas/
- [root@icoolkj datas]# ll
- total 12
- drwxr-x---. 7 mysql mysql 4096 Nov 19 22:02 mysql
- -rw-r-----. 1 mysql mysql 180 Nov 19 22:02 mysql-bin.000001
- -rw-r-----. 1 mysql mysql 36 Nov 19 22:02 mysql-bin.index
- [root@icoolkj datas]# cd mysql
- [root@icoolkj mysql]# ll
- total 78280
- -rw-r-----. 1 mysql mysql 56 Nov 19 22:02 auto.cnf
- -rw-------. 1 mysql mysql 1676 Nov 19 22:02 ca-key.pem
- -rw-r--r--. 1 mysql mysql 1112 Nov 19 22:02 ca.pem
- -rw-r--r--. 1 mysql mysql 1112 Nov 19 22:02 client-cert.pem
- -rw-------. 1 mysql mysql 1676 Nov 19 22:02 client-key.pem
- -rw-r-----. 1 mysql mysql 196608 Nov 19 22:02 #ib_16384_0.dblwr
- -rw-r-----. 1 mysql mysql 8585216 Nov 19 22:02 #ib_16384_1.dblwr
- -rw-r-----. 1 mysql mysql 5554 Nov 19 22:02 ib_buffer_pool
- -rw-r-----. 1 mysql mysql 12582912 Nov 19 22:02 ibdata1
- drwxr-x---. 2 mysql mysql 4096 Nov 19 22:02 #innodb_redo
- drwxr-x---. 2 mysql mysql 4096 Nov 19 22:02 #innodb_temp
- drwxr-x---. 2 mysql mysql 4096 Nov 19 22:02 mysql
- -rw-r-----. 1 mysql mysql 25165824 Nov 19 22:02 mysql.ibd
- -rw-r-----. 1 mysql mysql 986 Nov 19 22:02 mysql.log
- drwxr-x---. 2 mysql mysql 4096 Nov 19 22:02 performance_schema
- -rw-------. 1 mysql mysql 1680 Nov 19 22:02 private_key.pem
- -rw-r--r--. 1 mysql mysql 452 Nov 19 22:02 public_key.pem
- -rw-r--r--. 1 mysql mysql 1112 Nov 19 22:02 server-cert.pem
- -rw-------. 1 mysql mysql 1676 Nov 19 22:02 server-key.pem
- drwxr-x---. 2 mysql mysql 4096 Nov 19 22:02 sys
- -rw-r-----. 1 mysql mysql 16777216 Nov 19 22:02 undo_001
- -rw-r-----. 1 mysql mysql 16777216 Nov 19 22:02 undo_002
- [root@icoolkj mysql]#
-

- [root@icoolkj bin]# cd /data/mysql8/bin/
- ## 通过 MySQL的 bin路径下 mysqld_safe,用于后台安全启动MySQL。
- [root@icoolkj bin]# ./mysqld_safe --defaults-file=/data/mysql8/my.cnf &
- [1] 102020
- [root@icoolkj bin]# 2022-11-19T14:09:31.569559Z mysqld_safe Logging to '/data/mysql8/datas/mysql/mysql.log'.
- 2022-11-19T14:09:31.619918Z mysqld_safe Starting mysqld daemon with databases from /data/mysql8/datas/mysql
- ^C
- ## 查看是否启动
- [root@icoolkj bin]# ps -ef | grep mysql
- root 102020 94046 0 22:09 pts/1 00:00:00 /bin/sh ./mysqld_safe --defaults-file=/data/mysql8/my.cnf
- 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
- root 102349 94046 0 22:10 pts/1 00:00:00 grep --color=auto mysql
- [root@icoolkj bin]#
- ## 无密码登录
- [root@icoolkj bin]# ./mysql -u root --skip-password
- mysql: [Warning] Using a password on the command line interface can be insecure.
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 8
- Server version: 8.0.31 MySQL Community Server - GPL
-
- Copyright (c) 2000, 2022, Oracle and/or its affiliates.
-
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
-
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
- mysql>

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

到此,mysql数据库就装好了,可以Navicat上测试连接。
- ## 1.将服务文件拷贝到/etc/init.d下,并重命名为mysqld
- [root@icoolkj support-files]# pwd
- /data/mysql8/support-files
- [root@icoolkj support-files]# ll
- total 20
- -rwxr-xr-x. 1 7161 31415 1061 Sep 14 00:15 mysqld_multi.server
- -rw-r--r--. 1 7161 31415 2027 Sep 14 03:43 mysql-log-rotate
- -rwxr-xr-x. 1 7161 31415 10576 Sep 14 03:43 mysql.server
- [root@icoolkj support-files]# cp mysql.server /etc/init.d/mysqld
-
- ## 2.赋予mysql文件可执行权限
- [root@icoolkj support-files]# chmod +x /etc/init.d/mysqld
-
- ## 3.添加mysql服务
- [root@icoolkj support-files]# chkconfig --add mysqld
-
- ## 4.查看服务列表
- [root@icoolkj support-files]# chkconfig --list
-
- Note: This output shows SysV services only and does not include native
- systemd services. SysV configuration data might be overridden by native
- systemd configuration.
-
- If you want to list systemd services use 'systemctl list-unit-files'.
- To see services enabled on particular target use
- 'systemctl list-dependencies [target]'.
-
- ds_agent 0:off 1:off 2:on 3:on 4:on 5:on 6:off
- mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
- netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
- network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
- [root@icoolkj support-files]#
-
- ## 5.如果看到mysql的服务3,4,5都是on的话则成功,否则执行以下命令
- [root@icoolkj support-files]# chkconfig --level 345 mysqld on
-
-
- ## 6.重启服务器
- [root@icoolkj support-files]# reboot
-
- ## 7.重启以后查看进程,mysql是否自启成功
-
- [root@icoolkj support-files]# ps -ef | grep mysql
-
-

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

-
- ## 设置命令全局(将mysql添加到环境路径中)
- [root@icoolkj bin]# ln -s /data/mysql8/bin/mysql /usr/bin/
- [root@icoolkj bin]#
- ## 1.在配置文件关闭密码登陆
- ## 找到mysql配置文件:my.cnf
- ## [mysqld] 下 添加skip-grant-tables 保存退出
- [root@icoolkj ~]# vi /etc/my.cnf
- [root@icoolkj ~]# cat /etc/my.cnf
- [mysqld]
- skip-grant-tables
- ## 重启mysql服务: systemctl mysqld restart
- [root@icoolkj ~]# service mysqld restart
- Shutting down MySQL. SUCCESS!
- Starting MySQL.. SUCCESS!
- [root@icoolkj ~]#
-
- ## 2.将原密码置空
- [root@icoolkj ~]# mysql -u root;
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 9
- Server version: 8.0.31 MySQL Community Server - GPL
-
- Copyright (c) 2000, 2022, Oracle and/or its affiliates.
-
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
-
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
- mysql> update user set authentication_string="" where user = "root";
- ERROR 1046 (3D000): No database selected
- mysql> use mysql
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
-
- Database changed
- mysql> update user set authentication_string="" where user = "root";
- Query OK, 0 rows affected (0.01 sec)
- Rows matched: 1 Changed: 0 Warnings: 0
-
- mysql>
- ## exit/quit;(两者都是退出的意思,任选其一即可)
-
- ## 3.配置文件重新开启密码登陆
- ## 找到mysql配置文件: my.cnf
- ## 删除步骤1添加的 skip-grant-tables 保存退出
- ## 重启服务: service mysqld restart
-
- ## 4.进入mysql修改密码
- [root@icoolkj ~]# mysql -u root
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 8
- Server version: 8.0.31 MySQL Community Server - GPL
-
- Copyright (c) 2000, 2022, Oracle and/or its affiliates.
-
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
-
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
- mysql> use mysql
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
-
- Database changed
- mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
- Query OK, 0 rows affected (0.01 sec)
-
- mysql> flush privileges;
- Query OK, 0 rows affected (0.01 sec)
-
- mysql> quit;
- Bye
- [root@icoolkj ~]#
-
-
- ## 使用密码登陆:mysql -u root -p;
-

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。