linux安装mysql (rpm + yum + deb)

Mysql数据库的版本:mysql-5.7.26

方式一:rpm安装MySQL

一 安装前准备

1、检查是否已经安装过mysql,执行命令

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

 

 

 

 执行结果显示,有三个安装包,进行删除

[root@localhost ~]# yum remove mysql qt5-qtbase-mysql-5.9.2-3.el7.x86_64

 

 

 

 如果出现上述类似结果(此为安装完成后的版本),可以看出我们已经安装了mysql-server-5.7.26-1.el7.x86_64, mysql-community-common-5.7.26-1.el7.x86_64

执行删除命令

[root@localhost /]# rpm -e --nodeps mysql-server-5.7.26-1.el7.x86_64

 

再次执行查询命令,查看是否删除

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

 
 

2、查询所有Mysql对应的文件夹

 

 

 

 删除相关目录或文件

[root@localhost /]# rm -rf /usr/lib64/mysql /usr/include/mysql /usr/share/mysql

验证是否删除完毕

[root@localhost /]# whereis mysql
mysql:
[root@localhost /]# find / -name mysql
[root@localhost /]# 

3、检查mysql用户组和用户是否存在,如果没有,则创建

[root@localhost /]# cat /etc/group | grep mysql
[root@localhost /]# cat /etc/passwd |grep mysql
[root@localhost /]# groupadd mysql
[root@localhost /]# useradd -r -g mysql mysql
[root@localhost /]# 

4、从官网下载是用于Linux的Mysql安装包

直接到mysql官网 https://dev.mysql.com/downloads/repo/yum/ 选择所需版本进行下载。

 

二 安装Mysql

1、在执行wget命令的目录下或你的上传目录下找到Mysql安装包:

 

2、执行安装命令:

rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql80-community-release-el7-1.noarch.rpm

 

 出现如上错误,根据提示:1.要先安装mysql-community-common  

             2.依赖mariadb,需要删除mariadb (据说centos7 默认mariadb)

     删除mariadb命令: rpm -e --nodeps mariadb-libs-5.5.41-2.el7_0.x86_64

 

 

  出现如上错误,需要在安装命令加上 :

                                      --force --nodeps

3、安装完成后启动

  systemctl start mysqld

4、查看初始密码

   a: grep "password" /var/log/mysqld.log

   b: cat /var/log/mysqld.log | grep password

 

 5、使用初始密码登录mysql

  mysql -uroot -pxEoLfqm6Nr-*

 

  注:此处可能会遇到密码错误,或者格式不对的等异常, 如果出现此问题导致不能登录mysql,使用下面方法:

  关闭mysq服务:

   systemctl stop mysqld

  设置mysql不校验权限:

  systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"

  启动mysq服务:

   systemctl start mysqld

  登录mysql:

  mysql -uroot     

  继续步骤6,在步骤7进入”注“步骤

 

 6、设置密码

  先执行刷新权限,否则可能报错:

  flush privileges;

   ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysql@2020';

  或者 ALTER USER 'root'@'%' IDENTIFIED BY 'mysql@2020';

 7、退出mysql

  quit

  注:如果设置了步骤5中的不校验权限,需要进入如下操作

  关闭mysq服务:

   systemctl stop mysqld

  设置mysql校验权限:

  systemctl unset-environment MYSQLD_OPTS

  启动mysq服务:

   systemctl start mysqld

 

8、使用新密码登录

   mysql -uroot -pmysql@2020

 

9、开放远程连接

mysql>use mysql;
msyql>update user set user.Host='%' where user.User='root';
mysql>flush privileges;

 

10、设置开机自动启动

   systemctl enable mysqld
11、mysql操作命令
 

方式二:yum安装MySQL

  1. 运行以下命令更新YUM源。
    rpm -Uvh  http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

    8.0:

    yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm

  2. 运行以下命令安装MySQL。 
    yum -y install mysql-community-server
  3. 运行以下命令查看MySQL版本号。
    mysql -V
    返回结果如下,表示MySQL安装成功。 
    mysql  Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using  EditLine wrapper

    8.0.21版本 更改初始密码:
    1.在 /etc/my.cnf 中的 [mysqld] 中添加 skip-grant-table
    2.使用 [root]# server mysql restart 重启服务
    3.输入 [root]# mysql 无密码登录到mysql中
    4.使用 mysql > use mysql #选择数据库
    5.使用 mysql > update user set authentication_string='' where user='root'; #将root的权限验证字符串更新为空
    6.使用 mysql > select Host,User,authentication_string from user; #查询是否更新成功
    7.使用 mysql > flush privileges #刷新权限
    8.使用 mysql > exit #退出MySQL
    9.在配置文件[root]# vim /etc/my.cnf 中注释 #skip-grant-table
    10.重复第2步骤 重启服务
    11.使用 [root]# mysql -uroot -p 后直接回车登录到MySQL中 【注意其他语句可能有兼容问题导致【Mysql8.0】ERROR 1820 (HY000): You must reset your password using ALTER USER statement】
    12.使用 mysql > set password='your password'; #输入密码
    13.重复第7步。

    ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '新密码';

配置MySQL

  1. 运行以下命令启动MySQL服务。
    systemctl start mysqld
  2. 运行以下命令设置MySQL服务开机自启动。
     
    systemctl enable mysqld
  3. 运行以下命令查看/var/log/mysqld.log文件,获取并记录root用户的初始密码。
    # grep 'temporary password' /var/log/mysqld.log
    2019-04-28T06:50:56.674085Z 1 [Note] A temporary password is generated for root@localhost: 3w)WqGlM7-o,
    说明 下一步对MySQL进行安全性配置时,会使用该初始密码。
  4. 运行下列命令对MySQL进行安全性配置。
    mysql_secure_installation
    1. 重置root用户的密码。
      Enter password for user root: #输入上一步获取的root用户初始密码
      The 'validate_password' plugin is installed on the server.
      The subsequent steps will run with the existing configuration of the plugin.
      Using existing password for root.
      Estimated strength of the password: 100 
      Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y #是否更改root用户密码,输入Y
      New password: #输入新密码,长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
      Re-enter new password: #再次输入新密码
      Estimated strength of the password: 100 
      Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y #是否继续操作,输入Y
    2. 删除匿名用户账号。
      By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
      Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y  #是否删除匿名用户,输入Y
      Success.
    3. 禁止root账号远程登录。
      Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root远程登录,输入Y
      Success.
    4. 删除test库以及对test库的访问权限。
      Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否删除test库和对它的访问权限,输入Y
      - Dropping test database...
      Success.
    5. 重新加载授权表。
      Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加载授权表,输入Y
      Success.
      All done!
 

方式三:deepin(debian) deb安装MySQL

  1. 下载。
    为了避免各种依赖无法下载,选择所有依赖包集合bundle:
    mysql8.0的链接:mysql-server_8.0.22-1debian10_amd64.deb-bundle.tar
    wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-server_8.0.22-1debian10_amd64.deb-bundle.tar
    或者去MySQL官网下载对应版本,拷贝到服务器
  2. 解压。 
    解压.tar压缩包
    tar -xvf mysql-server_8.0.22-1debian10_amd64.deb-bundle.tar
    删除多余的test和debug包
    rm *test*
  3. 安装。 

    安装依赖包
    sudo apt-get install libmecab2 libjson-perl
    安装所有mysql包
    sudo dpkg -t mysql-*.deb
  4. 安装配置

    安装过程中会弹出设置密码对话框,输入密码,确认密码。

       5. 登录
            mysql -uroot -p  

            输入密码进入数据库

卸载

查看MySQL的依赖项:

dpkg --list|grep mysql

将列出来的相关依赖依次移除

sudo apt-get remove mysql-common

sudo apt-get remove mysql-apt-config

删除mysql

sudo apt-get autoremove --purge mysql-8.0

dpkg -l|grep ^rc|awk '{print$2}'|sudo xargs dpkg -P清除残留数据

再次查看mysql依赖

dpkg --list|grep mysql

清除完毕

posted @ 2020-01-21 11:10  Fernando_Y  阅读(680)  评论(0编辑  收藏  举报