服务器CentOS7上安装MySql

1.确保服务器系统处于最新状态

[root@localhost ~]# yum -y update
如果显示以下内容说明已经更新完成
Replaced:
grub2.x86_64 1:2.02-0.64.el7.centos grub2-tools.x86_64 1:2.02-0.64.el7.centos
Complete!

2.重启服务器
[root@localhost ~]# reboot

3.首先检查是否已经安装,如果已经安装先删除以前版本,以免安装不成功
[root@localhost ~]# php -v

[root@localhost ~]# rpm -qa | grep mysql

[root@localhost ~]# yum list installed | grep mysql

如果显示以下内容说明没有安装服务
-bash: gerp: command not found

如果有,就删除

4.下载MySql安装包
[root@localhost ~]# rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

[root@localhost ~]# rpm -ivh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

5.安装MySql
[root@localhost ~]# yum install -y mysql-server

[root@localhost ~]# yum install mysql-community-server
如果显示以下内容说明安装成功
Complete!

6.设置开机启动Mysql
[root@localhost ~]# systemctl enable mysqld.service

7.检查是否已经安装了开机自动启动
[root@localhost ~]# systemctl list-unit-files | grep mysqld
如果显示以下内容说明已经完成自动启动安装
mysqld.service enabled

8.设置开启服务
[root@localhost ~]# systemctl start mysqld.service

9.查看MySql默认密码
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log

10.登陆MySql,输入用户名和密码
[root@localhost ~]# mysql -uroot -p

11.修改当前用户密码
mysql>SET PASSWORD = PASSWORD('Jxd67_');

12.开启远程登录,授权root远程登录
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'a123456!' WITH GRANT OPTION;

13.命令立即执行生效
mysql>flush privileges;

Mysql初始的时候并没有开启Root用户的远程访问权限,Root只能本地localhost访问,下面使用Xshell连接Linux服务器操作Mysql给Root用户添加远程访问权限。

下面就再添加一个root用户,密码暂时为空,允许任意Ip访问'%'     
   -> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的密码不能太简单' WITH GRANT OPTION;

接下来修改一下root用户的密码

-> update user set password=PASSWORD('123456') where user='root';

然后刷新一下mysql的权限

-> flush privileges;

# 删除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

# 查看MySql相关文件
[root@localhost ~]# find / -name mysql

# 重启MySql服务
[root@localhost ~]# service mysqld restart

# 查看MySql版本
[root@localhost ~]# yum repolist all | grep mysql

# 查看当前的启动的 MySQL 版本
[root@localhost ~]# yum repolist enabled | grep mysql

# 通过Yum来安装MySQL,会自动处理MySQL与其他组件的依赖关系
[root@localhost ~]# yum install mysql-community-server

# 查看MySQL安装目录
[root@localhost ~]# whereis mysql

# 启动MySQL服务
[root@localhost ~]# systemctl start mysqld

# 查看MySQL服务状态
[root@localhost ~]# systemctl status mysqld

# 关闭MySQL服务
[root@localhost ~]# systemctl stop mysqld

# 测试MySQL是否安装成功
[root@localhost ~]# mysql

# 查看MySql默认密码
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log

# 查看所有数据库
mysql>show databases;

# 退出登录数据库
mysql>exit;

# 查看所有数据库用户
mysql>SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user

posted @ 2020-02-27 18:57  darkly  阅读(246)  评论(1编辑  收藏  举报