第一步:检查服务器内部是不是有其他类型的数据库
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
[root@VM-16-9-centos tools]# lsMySQL-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[root@VM-16-9-centos tools]# rpm -ivh MySQL-server-5.6.21-1.rhel5.x86_64.rpmPreparing... ################################# [100%]Updating / installing...1:MySQL-server-5.6.21-1.rhel5 ################################# [100%][root@VM-16-9-centos tools]# rpm -ivh MySQL-client-5.6.21-1.rhel5.x86_64.rpmPreparing... ################################# [100%]Updating / installing...1:MySQL-client-5.6.21-1.rhel5 ################################# [100%][root@VM-16-9-centos tools]# rpm -ivh MySQL-devel-5.6.21-1.rhel5.x86_64.rpmPreparing... ################################# [100%]Updating / installing...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
[root@VM-16-9-centos tools]# cp /usr/share/mysql/my-default.cnf /etc/my.cnf[root@VM-16-9-centos tools]# /usr/bin/mysql_install_dbInstalling 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).2021-04-15 15:49:34 23016 [Note] InnoDB: Using atomics to ref count buffer pool pages2021-04-15 15:49:34 23016 [Note] InnoDB: The InnoDB memory heap is disabled2021-04-15 15:49:34 23016 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins2021-04-15 15:49:34 23016 [Note] InnoDB: Memory barrier is not used2021-04-15 15:49:34 23016 [Note] InnoDB: Compressed tables use zlib 1.2.32021-04-15 15:49:34 23016 [Note] InnoDB: Using Linux native AIO2021-04-15 15:49:34 23016 [Note] InnoDB: Using CPU crc32 instructions2021-04-15 15:49:34 23016 [Note] InnoDB: Initializing buffer pool, size = 128.0M2021-04-15 15:49:34 23016 [Note] InnoDB: Completed initialization of buffer pool2021-04-15 15:49:34 23016 [Note] InnoDB: Highest supported file format is Barracuda.2021-04-15 15:49:34 23016 [Note] InnoDB: Log scan progressed past the checkpoint lsn 494632021-04-15 15:49:34 23016 [Note] InnoDB: Database was not shutdown normally!2021-04-15 15:49:34 23016 [Note] InnoDB: Starting crash recovery.2021-04-15 15:49:34 23016 [Note] InnoDB: Reading tablespace information from the .ibd files...2021-04-15 15:49:34 23016 [Note] InnoDB: Restoring possible half-written data pages2021-04-15 15:49:34 23016 [Note] InnoDB: from the doublewrite buffer...InnoDB: Doing recovery: scanned up to log sequence number 16006072021-04-15 15:49:34 23016 [Note] InnoDB: Starting an apply batch of log records to the database...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 99InnoDB: Apply batch completed2021-04-15 15:49:35 23016 [Note] InnoDB: 128 rollback segment(s) are active.2021-04-15 15:49:35 23016 [Note] InnoDB: Waiting for purge to start2021-04-15 15:49:35 23016 [Note] InnoDB: 5.6.21 started; log sequence number 16006072021-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.ERROR: 1146 Table 'mysql.innodb_table_stats' doesn't exist2021-04-15 15:49:35 23016 [ERROR] Aborting2021-04-15 15:49:35 23016 [Note] Binlog end2021-04-15 15:49:35 23016 [Note] InnoDB: FTS optimize thread exiting.2021-04-15 15:49:35 23016 [Note] InnoDB: Starting shutdown...2021-04-15 15:49:37 23016 [Note] InnoDB: Shutdown completed; log sequence number 16006172021-04-15 15:49:37 23016 [Note] /usr/sbin/mysqld: Shutdown complete[root@VM-16-9-centos tools]#
4.3、启动数据库
service mysql start
[root@VM-16-9-centos tools]# service mysql startStarting MySQL. SUCCESS!
4.4、进入数据库
mysql -uroot -p
默认是没有密码的 直接按回车键就可以了进去了。
mysql -uroot -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.6.21 MySQL Community Server (GPL)Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases-> ;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || test |+--------------------+
4.4、查看数据库状态
这是死亡状态
● mysql.service - LSB: start and stop MySQLLoaded: loaded (/etc/rc.d/init.d/mysql; bad; vendor preset: disabled)Active: inactive (dead)Docs: man:systemd-sysv-generator(8)
做个软件链接,(我也不知道是什么意思,知道的可以给我科普一下)ln -s /usr/local/pnp4nagios/bin/npcd /sbin/npcd
然后再次执行:systemctl status mysql
[root@VM-16-9-centos ~]# ln -s /usr/local/pnp4nagios/bin/npcd /sbin/npcd[root@VM-16-9-centos ~]# systemctl status mysql● mysql.service - LSB: start and stop MySQLLoaded: loaded (/etc/rc.d/init.d/mysql; bad; vendor preset: disabled)Active: inactive (dead)Docs: man:systemd-sysv-generator(8)[root@VM-16-9-centos ~]# systemctl restart mysql[root@VM-16-9-centos ~]# systemctl status mysql● mysql.service - LSB: start and stop MySQLLoaded: loaded (/etc/rc.d/init.d/mysql; bad; vendor preset: disabled)Active: active (exited) since Thu 2021-04-15 16:52:07 CST; 3s agoDocs: man:systemd-sysv-generator(8)Process: 32345 ExecStart=/etc/rc.d/init.d/mysql start (code=exited, status=0/SUCCESS)Apr 15 16:52:07 VM-16-9-centos systemd[1]: Starting LSB: start and stop MySQL...Apr 15 16:52:07 VM-16-9-centos mysql[32345]: Starting MySQL SUCCESS!Apr 15 16:52:07 VM-16-9-centos systemd[1]: Started LSB: start and stop MySQL.[root@VM-16-9-centos ~]#
第五部:设置数据库远程访问账号
5.1查询出 数据库远程链接情况,可以根据需求自己动态授权,执行步骤如下:
[root@iZ8vb6cnxashtjld7ke1k2Z ~]# mysql -u root -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 256607Server version: 5.6.21 MySQL Community Server (GPL)Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.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 namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select host,user,pasword form user;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 1mysql> select host,user,password from user;+-------------------------+--------+-------------------------------------------+| host | user | password |+-------------------------+--------+-------------------------------------------+| localhost | root | *8271DB73330FB2E01C50FE4954141E52A696F947 || iz8vb6cnxashtjld7ke1k2z | root | || 127.0.0.1 | root | || ::1 | root | || localhost | | || iz8vb6cnxashtjld7ke1k2z | | || 192.168.1.3 | myuser | *FABE5482D5AADF36D028AC443D117BE1180B9725 || 192.168.2.249 | root | *8271DB73330FB2E01C50FE4954141E52A696F947 || % | root | *8271DB73330FB2E01C50FE4954141E52A696F947 || 124.238.165.118 | root | *8271DB73330FB2E01C50FE4954141E52A696F947 || 24.238.165.118 | root | *8271DB73330FB2E01C50FE4954141E52A696F947 |+-------------------------+--------+-------------------------------------------+11 rows in set (0.00 sec)mysql> exit;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;
mysql> select Host,User,Password from user;+-------------------------+--------+-------------------------------------------+| Host | User | Password |+-------------------------+--------+-------------------------------------------+| localhost | root | *8271DB73330FB2E01C50FE4954141E52A696F947 || iz8vb6cnxashtjld7ke1k2z | root | || 127.0.0.1 | root | || ::1 | root | || localhost | | || iz8vb6cnxashtjld7ke1k2z | | || 192.168.1.3 | myuser | *FABE5482D5AADF36D028AC443D117BE1180B9725 || 192.168.2.249 | root | *8271DB73330FB2E01C50FE4954141E52A696F947 || % | root | *68E0E420DCE15221B1A34B700E715254D8BE2B1C || 24.238.165.118 | root | *8271DB73330FB2E01C50FE4954141E52A696F947 |+-------------------------+--------+-------------------------------------------+10 rows in set (0.00 sec)mysql> GRANT ALL PRIVILEGES ON *.* TO 'rendy'@'%' IDENTIFIED BY 'rendy123' WITH GRANT OPTION;Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> select Host,User,Password from user;+-------------------------+--------+-------------------------------------------+| Host | User | Password |+-------------------------+--------+-------------------------------------------+| localhost | root | *8271DB73330FB2E01C50FE4954141E52A696F947 || iz8vb6cnxashtjld7ke1k2z | root | || 127.0.0.1 | root | || ::1 | root | || localhost | | || iz8vb6cnxashtjld7ke1k2z | | || 192.168.1.3 | myuser | *FABE5482D5AADF36D028AC443D117BE1180B9725 || 192.168.2.249 | root | *8271DB73330FB2E01C50FE4954141E52A696F947 || % | root | *68E0E420DCE15221B1A34B700E715254D8BE2B1C || % | rendy | *98843068F7A84FF9B934E5E2F39880CEB5D4A22C || 24.238.165.118 | root | *8271DB73330FB2E01C50FE4954141E52A696F947 |+-------------------------+--------+-------------------------------------------+11 rows in set (0.00 sec)mysql>

5.4 开启的创建BUG旅程吧!
