首先推荐一个类似xshell的服务器管理软件FinalShell
下载地址:/ 非常好用!
1、确保服务器系统处于最新状态
[root@centos7 ~]# yum -y update
等待更新完成,会出现以下提示:
Replaced:grub2.x86_64 1:2.02-0.64.el7.centosgrub2-tools.x86_64 1:2.02-0.64.el7.centosComplete!
2、重启服务器
[root@centos7 lib]# reboot
3、查看系统中是否已安装 MySQL 服务,以下提供两种方式:
rpm -qa | grep mysqlyum list installed | grep mysql
4、如果已安装则删除 MySQL 及其依赖的包:
yum -y remove mysql-libs.x86_64
5、下载MySql安装包
[root@centos7 ~]# rpm -ivh /get/mysql57-community-release-el7-8.noarch.rpm
或者
[root@localhost ~]# rpm -ivh /get/mysql-community-release-el7-5.noarch.rpm
6、安装MySQL
[root@centos7 ~]# yum install -y mysql-server
或者
[root@localhost ~]# yum install mysql-community-server
出现以下提示,安装成功
Installed:mysql-community-libs.x86_64 0:5.7.24-1.el7 mysql-community-libs-compat.x86_64 0:5.7.24-1.el7 mysql-community-server.x86_64 0:5.7.24-1.el7 Dependency Installed:mysql-community-client.x86_64 0:5.7.24-1.el7mysql-community-common.x86_64 0:5.7.24-1.el7 Replaced:mariadb-libs.x86_64 1:5.5.60-1.el7_5 Complete!
7、设置开机启动MySQL
[root@centos7 ~]# systemctl enable mysqld.service
8、检查是否已经安装了开机自动启动
[root@centos7 ~]# systemctl list-unit-files | grep mysqldmysqld.service enabled mysqld@.service disabled
上述情况内容说明已经完成自动启动安装
9、设置开启服务
[root@centos7 ~]# systemctl start mysqld.service
10、查看MySQL默认密码
[root@centos7 ~]# grep 'temporary password' /var/log/mysqld.log-10-23T07:26:03.122784Z 1 [Note] A temporary password is generated for root@localhost: SO#%2<oGFjos
这里的默认密码SO#%2<oGFjos是MySQL安装的时候随机生成的,
11、登陆MySQL,输入用户名和默认密码
[root@centos7 ~]# mysql -uroot -pEnter password:
登陆成功后,如下图所示:
12、修改当前用户密码
mysql> set password = password('Root_123!');
如下图所示,修改密码成功!
13、开启远程登录,授权root远程登录
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Root_123!' WITH GRANT OPTION;Query OK, 0 rows affected, 1 warning (0.00 sec)
14、命令立即执行生效
mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)
其他一些功能:
1、检查并且显示Apache相关安装包
[root@centos7 ~]# rpm -qa | grep mysqlmysql57-community-release-el7-8.noarchmysql-community-client-5.7.24-1.el7.x86_64mysql-community-libs-5.7.24-1.el7.x86_64mysql-community-common-5.7.24-1.el7.x86_64mysql-community-libs-compat-5.7.24-1.el7.x86_64mysql-community-server-5.7.24-1.el7.x86_64
2、删除MySQL
[root@localhost ~]# yum remove -y mysql mysql mysql-server mysql-libs compat-mysql51
或者
[root@localhost ~]# rpm -e mysql-community-libs-5.7.20-1.el7.x86_64 --nodeps
或者
[root@localhost ~]# yum -y remove mysql-community-libs-5.7.20-1.el7.x86_64
3、查看MySQL相关文件
[root@centos7 ~]# find / -name mysql/var/spool/mail/mysql/var/lib/mysql/var/lib/mysql/mysql/etc/selinux/targeted/active/modules/100/mysql/etc/logrotate.d/mysql/home/mysql/home/ftp/sh-1.5.5/mysql/home/ftp/sh-1.5.5/php-5.5.7/ext/mysql/home/ftp/sh-1.5.5/php-5.5.7/travis/ext/mysql/usr/lib64/mysql/usr/share/mysql/usr/bin/mysql
4、重启MySQL服务
[root@centos7 ~]# service mysqld restart
5、查看MySQL版本
[root@centos7 ~]# yum repolist all | grep mysqlmysql-connectors-community/x86_64 MySQL Connectors Communityenabled:74mysql-connectors-community-source MySQL Connectors Community - S disabledmysql-tools-community/x86_64MySQL Tools Communityenabled:74mysql-tools-community-sourceMySQL Tools Community - Source disabledmysql-tools-preview/x86_64 MySQL Tools Preview disabledmysql-tools-preview-source MySQL Tools Preview - Source disabledmysql55-community/x86_64MySQL 5.5 Community Serverdisabledmysql55-community-sourceMySQL 5.5 Community Server - S disabledmysql56-community/x86_64MySQL 5.6 Community Serverdisabledmysql56-community-sourceMySQL 5.6 Community Server - S disabledmysql57-community/x86_64MySQL 5.7 Community Serverenabled: 307mysql57-community-sourceMySQL 5.7 Community Server - S disabled
6、查看当前的启动的 MySQL 版本
[root@centos7 ~]# yum repolist enabled | grep mysqlmysql-connectors-community/x86_64 MySQL Connectors Community 74mysql-tools-community/x86_64MySQL Tools Community 74mysql57-community/x86_64MySQL 5.7 Community Server 307
7、通过Yum来安装MySQL,会自动处理MySQL与其他组件的依赖关系
[root@localhost ~]# yum install mysql-community-server
8、查看MySQL安装目录
[root@centos7 ~]# whereis mysqlmysql: /usr/bin/mysql /usr/lib64/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
9、启动MySQL服务
[root@centos7 ~]# systemctl start mysqld
10、查看MySQL服务状态
[root@centos7 ~]# systemctl status mysqld● mysqld.service - MySQL ServerLoaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)Active: active (running) since Tue -10-23 17:12:39 CST; 6min agoDocs: man:mysqld(8)/doc/refman/en/using-systemd.htmlProcess: 19048 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)Process: 19030 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)Main PID: 19051 (mysqld)CGroup: /system.slice/mysqld.service└─19051 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pidOct 23 17:12:38 centos7 systemd[1]: Starting MySQL Server...Oct 23 17:12:39 centos7 systemd[1]: Started MySQL Server.
11、关闭MySQL服务
[root@localhost ~]# systemctl stop mysqld
12、测试MySQL是否安装成功
[root@localhost ~]# mysql
13、查看MySQL默认密码
[root@centos7 ~]# grep 'temporary password' /var/log/mysqld.log-10-23T07:26:03.122784Z 1 [Note] A temporary password is generated for root@localhost: SO#%2<oGFjos
14、查看所有数据库
mysql>show databases;
15、退出登录数据库
mysql>exit;
16、查看所有数据库用户
mysql>SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;