第一步:检查服务器内部是不是有其他类型的数据库

rpm-qa|grep -i mysql 检测 mysql 数据库
rpm-qa|grep -i mariadb 检测 云服务器 mariadb 数据库
whereis mysql 查询mysql到安装的目录然后删除
whereis mariadb 查询mariadb安装的目录然后删除

第二步:卸载检测出来的数据库

卸载安装过的软件
yum remove MySQL-server MySQL-devel
如果卸载不成功强力卸载
rpm -e —nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
删除安装过的软件目录
rm –rf /usr/my.cnf
rm -rf /var/lib/mysql

第三步:安装数据库

3.1、创建下载安装包的目录(/usr/local/tools)

(一定要创建并进入创建文件,作为小白的我,不信鞋,两次没有安装成功)
mkdir -p /usr/local/tools
然后进入指定文件夹
cd /usr/local/tools

3.2、下载数据库文件

cd /usr/local/tools
wget http://dev.mysql.com/Downloads/MySQL-5.6/MySQL-server-5.6.21-1.rhel5.x86_64.rpm
wget http://dev.mysql.com/Downloads/MySQL-5.6/MySQL-devel-5.6.21-1.rhel5.x86_64.rpm
wget http://dev.mysql.com/Downloads/MySQL-5.6/MySQL-client-5.6.21-1.rhel5.x86_64.rpm

3.3、依次安装下载过的rpm 注意循序

rpm -ivh MySQL-server-5.6.21-1.rhel5.x86_64.rpm
rpm -ivh MySQL-client-5.6.21-1.rhel5.x86_64.rpm
rpm -ivh MySQL-devel-5.6.21-1.rhel5.x86_64.rpm

  1. [root@VM-16-9-centos tools]# ls
  2. MySQL-client-5.6.21-1.rhel5.x86_64.rpm MySQL-devel-5.6.21-1.rhel5.x86_64.rpm MySQL-server-5.6.21-1.rhel5.x86_64.rpm
  3. [root@VM-16-9-centos tools]# rpm -ivh MySQL-server-5.6.21-1.rhel5.x86_64.rpm
  4. Preparing... ################################# [100%]
  5. Updating / installing...
  6. 1:MySQL-server-5.6.21-1.rhel5 ################################# [100%]
  7. [root@VM-16-9-centos tools]# rpm -ivh MySQL-client-5.6.21-1.rhel5.x86_64.rpm
  8. Preparing... ################################# [100%]
  9. Updating / installing...
  10. 1:MySQL-client-5.6.21-1.rhel5 ################################# [100%]
  11. [root@VM-16-9-centos tools]# rpm -ivh MySQL-devel-5.6.21-1.rhel5.x86_64.rpm
  12. Preparing... ################################# [100%]
  13. Updating / installing...
  14. 1:MySQL-devel-5.6.21-1.rhel5 ################################# [100%]

第四步:修改配置文件、初始化Msql

4.1、修改配置文件路径到etc目录,命令:

cp /usr/share/mysql/my-default.cnf /etc/my.cnf

4.2、初始化Mysql ,命令:

/usr/bin/mysql_install_db

  1. [root@VM-16-9-centos tools]# cp /usr/share/mysql/my-default.cnf /etc/my.cnf
  2. [root@VM-16-9-centos tools]# /usr/bin/mysql_install_db
  3. Installing MySQL system tables...2021-04-15 15:49:34 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
  4. 2021-04-15 15:49:34 23016 [Note] InnoDB: Using atomics to ref count buffer pool pages
  5. 2021-04-15 15:49:34 23016 [Note] InnoDB: The InnoDB memory heap is disabled
  6. 2021-04-15 15:49:34 23016 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
  7. 2021-04-15 15:49:34 23016 [Note] InnoDB: Memory barrier is not used
  8. 2021-04-15 15:49:34 23016 [Note] InnoDB: Compressed tables use zlib 1.2.3
  9. 2021-04-15 15:49:34 23016 [Note] InnoDB: Using Linux native AIO
  10. 2021-04-15 15:49:34 23016 [Note] InnoDB: Using CPU crc32 instructions
  11. 2021-04-15 15:49:34 23016 [Note] InnoDB: Initializing buffer pool, size = 128.0M
  12. 2021-04-15 15:49:34 23016 [Note] InnoDB: Completed initialization of buffer pool
  13. 2021-04-15 15:49:34 23016 [Note] InnoDB: Highest supported file format is Barracuda.
  14. 2021-04-15 15:49:34 23016 [Note] InnoDB: Log scan progressed past the checkpoint lsn 49463
  15. 2021-04-15 15:49:34 23016 [Note] InnoDB: Database was not shutdown normally!
  16. 2021-04-15 15:49:34 23016 [Note] InnoDB: Starting crash recovery.
  17. 2021-04-15 15:49:34 23016 [Note] InnoDB: Reading tablespace information from the .ibd files...
  18. 2021-04-15 15:49:34 23016 [Note] InnoDB: Restoring possible half-written data pages
  19. 2021-04-15 15:49:34 23016 [Note] InnoDB: from the doublewrite buffer...
  20. InnoDB: Doing recovery: scanned up to log sequence number 1600607
  21. 2021-04-15 15:49:34 23016 [Note] InnoDB: Starting an apply batch of log records to the database...
  22. InnoDB: Progress in percent: 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  23. InnoDB: Apply batch completed
  24. 2021-04-15 15:49:35 23016 [Note] InnoDB: 128 rollback segment(s) are active.
  25. 2021-04-15 15:49:35 23016 [Note] InnoDB: Waiting for purge to start
  26. 2021-04-15 15:49:35 23016 [Note] InnoDB: 5.6.21 started; log sequence number 1600607
  27. 2021-04-15 15:49:35 23016 [Warning] InnoDB: Cannot open table mysql/innodb_table_stats from the internal data dictionary of InnoDB though the .frm file for the table exists. See http://dev.mysql.com/doc/refman/5.6/en/innodb-troubleshooting.html for how you can resolve the problem.
  28. ERROR: 1146 Table 'mysql.innodb_table_stats' doesn't exist
  29. 2021-04-15 15:49:35 23016 [ERROR] Aborting
  30. 2021-04-15 15:49:35 23016 [Note] Binlog end
  31. 2021-04-15 15:49:35 23016 [Note] InnoDB: FTS optimize thread exiting.
  32. 2021-04-15 15:49:35 23016 [Note] InnoDB: Starting shutdown...
  33. 2021-04-15 15:49:37 23016 [Note] InnoDB: Shutdown completed; log sequence number 1600617
  34. 2021-04-15 15:49:37 23016 [Note] /usr/sbin/mysqld: Shutdown complete
  35. [root@VM-16-9-centos tools]#

4.3、启动数据库

service mysql start

  1. [root@VM-16-9-centos tools]# service mysql start
  2. Starting MySQL. SUCCESS!

4.4、进入数据库

mysql -uroot -p
默认是没有密码的 直接按回车键就可以了进去了。

  1. mysql -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 1
  5. Server version: 5.6.21 MySQL Community Server (GPL)
  6. Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
  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> show databases
  12. -> ;
  13. +--------------------+
  14. | Database |
  15. +--------------------+
  16. | information_schema |
  17. | mysql |
  18. | performance_schema |
  19. | test |
  20. +--------------------+

4.4、查看数据库状态

这是死亡状态

  1. ● mysql.service - LSB: start and stop MySQL
  2. Loaded: loaded (/etc/rc.d/init.d/mysql; bad; vendor preset: disabled)
  3. Active: inactive (dead)
  4. Docs: man:systemd-sysv-generator(8)

做个软件链接,(我也不知道是什么意思,知道的可以给我科普一下)
ln -s /usr/local/pnp4nagios/bin/npcd /sbin/npcd
然后再次执行:
systemctl status mysql

  1. [root@VM-16-9-centos ~]# ln -s /usr/local/pnp4nagios/bin/npcd /sbin/npcd
  2. [root@VM-16-9-centos ~]# systemctl status mysql
  3. ● mysql.service - LSB: start and stop MySQL
  4. Loaded: loaded (/etc/rc.d/init.d/mysql; bad; vendor preset: disabled)
  5. Active: inactive (dead)
  6. Docs: man:systemd-sysv-generator(8)
  7. [root@VM-16-9-centos ~]# systemctl restart mysql
  8. [root@VM-16-9-centos ~]# systemctl status mysql
  9. ● mysql.service - LSB: start and stop MySQL
  10. Loaded: loaded (/etc/rc.d/init.d/mysql; bad; vendor preset: disabled)
  11. Active: active (exited) since Thu 2021-04-15 16:52:07 CST; 3s ago
  12. Docs: man:systemd-sysv-generator(8)
  13. Process: 32345 ExecStart=/etc/rc.d/init.d/mysql start (code=exited, status=0/SUCCESS)
  14. Apr 15 16:52:07 VM-16-9-centos systemd[1]: Starting LSB: start and stop MySQL...
  15. Apr 15 16:52:07 VM-16-9-centos mysql[32345]: Starting MySQL SUCCESS!
  16. Apr 15 16:52:07 VM-16-9-centos systemd[1]: Started LSB: start and stop MySQL.
  17. [root@VM-16-9-centos ~]#

第五部:设置数据库远程访问账号

5.1查询出 数据库远程链接情况,可以根据需求自己动态授权,执行步骤如下:

  1. [root@iZ8vb6cnxashtjld7ke1k2Z ~]# mysql -u root -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 256607
  5. Server version: 5.6.21 MySQL Community Server (GPL)
  6. Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
  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> use mysql;
  12. Reading table information for completion of table and column names
  13. You can turn off this feature to get a quicker startup with -A
  14. Database changed
  15. mysql> select host,user,pasword form user;
  16. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'user' at line 1
  17. mysql> select host,user,password from user;
  18. +-------------------------+--------+-------------------------------------------+
  19. | host | user | password |
  20. +-------------------------+--------+-------------------------------------------+
  21. | localhost | root | *8271DB73330FB2E01C50FE4954141E52A696F947 |
  22. | iz8vb6cnxashtjld7ke1k2z | root | |
  23. | 127.0.0.1 | root | |
  24. | ::1 | root | |
  25. | localhost | | |
  26. | iz8vb6cnxashtjld7ke1k2z | | |
  27. | 192.168.1.3 | myuser | *FABE5482D5AADF36D028AC443D117BE1180B9725 |
  28. | 192.168.2.249 | root | *8271DB73330FB2E01C50FE4954141E52A696F947 |
  29. | % | root | *8271DB73330FB2E01C50FE4954141E52A696F947 |
  30. | 124.238.165.118 | root | *8271DB73330FB2E01C50FE4954141E52A696F947 |
  31. | 24.238.165.118 | root | *8271DB73330FB2E01C50FE4954141E52A696F947 |
  32. +-------------------------+--------+-------------------------------------------+
  33. 11 rows in set (0.00 sec)
  34. mysql> exit;
  35. Bye

5.2host 代表访问主机IP,(%代表任何主机都可以访问)

5.3更具自己需求自动添加。

例如,你想rendy(账号),使用rendy123(密码)从任何主机连接到mysql服务器的话。
GRANT ALL PRIVILEGES ON *.* TO 'rendy'@'%' IDENTIFIED BY 'rendy123' WITH GRANT OPTION;
如果你想允许用户myuser从ip为192.168.1.64的主机连接到mysql服务器,并使用root作为密码
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.64' IDENTIFIED BY 'root' WITH GRANT OPTION;
最后执行:
flush privileges;

  1. mysql> select Host,User,Password from user;
  2. +-------------------------+--------+-------------------------------------------+
  3. | Host | User | Password |
  4. +-------------------------+--------+-------------------------------------------+
  5. | localhost | root | *8271DB73330FB2E01C50FE4954141E52A696F947 |
  6. | iz8vb6cnxashtjld7ke1k2z | root | |
  7. | 127.0.0.1 | root | |
  8. | ::1 | root | |
  9. | localhost | | |
  10. | iz8vb6cnxashtjld7ke1k2z | | |
  11. | 192.168.1.3 | myuser | *FABE5482D5AADF36D028AC443D117BE1180B9725 |
  12. | 192.168.2.249 | root | *8271DB73330FB2E01C50FE4954141E52A696F947 |
  13. | % | root | *68E0E420DCE15221B1A34B700E715254D8BE2B1C |
  14. | 24.238.165.118 | root | *8271DB73330FB2E01C50FE4954141E52A696F947 |
  15. +-------------------------+--------+-------------------------------------------+
  16. 10 rows in set (0.00 sec)
  17. mysql> GRANT ALL PRIVILEGES ON *.* TO 'rendy'@'%' IDENTIFIED BY 'rendy123' WITH GRANT OPTION;
  18. Query OK, 0 rows affected (0.00 sec)
  19. mysql> flush privileges;­
  20. Query OK, 0 rows affected (0.00 sec)
  21. mysql> select Host,User,Password from user;
  22. +-------------------------+--------+-------------------------------------------+
  23. | Host | User | Password |
  24. +-------------------------+--------+-------------------------------------------+
  25. | localhost | root | *8271DB73330FB2E01C50FE4954141E52A696F947 |
  26. | iz8vb6cnxashtjld7ke1k2z | root | |
  27. | 127.0.0.1 | root | |
  28. | ::1 | root | |
  29. | localhost | | |
  30. | iz8vb6cnxashtjld7ke1k2z | | |
  31. | 192.168.1.3 | myuser | *FABE5482D5AADF36D028AC443D117BE1180B9725 |
  32. | 192.168.2.249 | root | *8271DB73330FB2E01C50FE4954141E52A696F947 |
  33. | % | root | *68E0E420DCE15221B1A34B700E715254D8BE2B1C |
  34. | % | rendy | *98843068F7A84FF9B934E5E2F39880CEB5D4A22C |
  35. | 24.238.165.118 | root | *8271DB73330FB2E01C50FE4954141E52A696F947 |
  36. +-------------------------+--------+-------------------------------------------+
  37. 11 rows in set (0.00 sec)
  38. mysql>

Centos7 安装Mysql - 图1

5.4 开启的创建BUG旅程吧!