Linux下yum安装mysql

Linux下yum安装mysql


Adding the MySQL Yum Repository

First, add the MySQL Yum repository to your system's repository list. Follow these steps:

  1. Go to the download page for MySQL Yum repository at https://dev.mysql.com/downloads/repo/yum/.

  2. Select and download the release package for your platform.

  3. Install the downloaded release package with the following command, replacing platform-and-version-specific-package-name with the name of the downloaded package:

    shell> sudo rpm -Uvh platform-and-version-specific-package-name.rpm

For example, for version n of the package for EL6-based systems, the command is:

shell> sudo rpm -Uvh mysql80-community-release-el6-n.noarch.rpm
  • Note

    Once the release package is installed on your system, any system-wide update by the yum update command (or dnf upgrade for dnf-enabled systems) will automatically upgrade MySQL packages on your system and also replace any native third-party packages, if Yum finds replacements for them in the MySQL Yum repository. See Upgrading MySQL with the MySQL Yum Repository and Replacing a Native Third-Party Distribution of MySQL for details.

  • Selecting a Release Series

    When using the MySQL Yum repository, the latest GA release of MySQL is selected for installation by default. If this is what you want, you can skip to the next step, Installing MySQL with Yum.

    Within the MySQL Yum repository (https://repo.mysql.com/yum/), different release series of the MySQL Community Server are hosted in different subrepositories. The subrepository for the latest GA series (currently MySQL 8.0) is enabled by default, and the subrepositories for all other series (for example, the MySQL 5.7 series) are disabled by default. Use this command to see all the subrepositories in the MySQL Yum repository, and see which of them are enabled or disabled (for dnf-enabled systems, replace yum in the command with dnf):

    shell> yum repolist all | grep mysql

To install the latest release from the latest GA series, no configuration is needed. To install the latest release from a specific series other than the latest GA series, disable the subrepository for the latest GA series and enable the subrepository for the specific series before running the installation command. If your platform supports the yum-config-manager or dnf config-manager command, you can do that by issuing, for example, the following commands, which disable the subrepository for the 8.0 series and enable the one for the 5.7 series; for platforms that are not dnf-enabled:

shell> sudo yum-config-manager --disable mysql80-community
shell> sudo yum-config-manager --enable mysql57-community

For dnf-enabled platforms:

shell> sudo dnf config-manager --disable mysql80-community
shell> sudo dnf config-manager --enable mysql57-community

Besides using yum-config-manager or the dnf config-manager command, you can also select a series by editing manually the /etc/yum.repos.d/mysql-community.repo file. This is a typical entry for a release series' subrepository in the file:

[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

Find the entry for the subrepository you want to configure, and edit the enabled option. Specify enabled=0 to disable a subrepository, or enabled=1 to enable a subrepository. For example, to install MySQL 5.7, make sure you have enabled=0 for the above subrepository entry for MySQL 8.0, and have enabled=1 for the entry for the 5.7 series:

# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

You should only enable subrepository for one release series at any time. When subrepositories for more than one release series are enabled, the latest series will be used by Yum.

Verify that the correct subrepositories have been enabled and disabled by running the following command and checking its output (for dnf-enabled systems, replace yum in the command with dnf):

shell> yum list | grep mysql

 

1、首先,在CentOS下,可以直接通过yum命令来安装数据库。在安装数据库之前,应该先查看自己的CentOS上是否已经安装了MySQL数据库,如果安装了就先卸载掉。

(1)查看是否安装了MySQL数据库

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

得到的结果如下:

这样说明已经安装了数据库,可以先对它进行卸载。。。

(2)卸载过程

卸载有两种方式,一种是普通删除,另一种是强力删除,当MySQL数据库有其它的依赖文件时,也进行删除。

分别是:rpm -e mysqlrpm -e --nodeps mysql

(3)安装过程

首先,我们通过命令:yum list | grep mysql来查看yum上提供的数据库可下载版本。我这里使用的是阿里云的镜像

32位系统 选择安装 mysql.i686,mysql-devel.i686,mysql-server.i686就行了。

64位系统 选择安装 mysql.x86_64 mysql-server.x86_64 mysql-devel.x86_64

我的是64位系统,使用命令:

yum -y install mysql.x86_64 mysql-server.x86_64 mysql-devel.x86_64

yum -y install mysql-community-client.x86_64 mysql-community-server.x86_64 mysql-community-devel.x86_64 

就可以安装好MySQL数据库了。

(4)数据库的相关配置

安装完MySQL数据库后,会发现多出了一个mysqld服务,这就是我们的数据库服务,启动它就是启动数据库。

启动方式:service mysqld start 第一次启动会初始化数据库

关闭方式:service mysqld stop

我们可以使用命令:chkconfig --list | grep mysqld 来查看是否开机自动启动。如果2~5的都是 on 或者是启用

说明是开机自动启动,否则如果不是。我们可以通过命令 chkconfig mysqld on 来设置成开机自动启动。

到了这里,我们就应该启动数据库设置密码了。

(5)数据库密码设置

先启动mysqld服务,即:service mysqld start,然后执行命令

mysqladmin -u root -p password '123456'

意思是把root用户的密码设置为123456,现在我们就可以登录MySQL数据库了。

(6) 远程连接设置

首先要开放 3306 端口 打开防火墙文件

vi /etc/sysconfig/iptables

添加端口开放记录

-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

然后如果还连接不了,就需要设置权限

GRANT ALL PRIVILEGES ON *.* TO 'root'@'101.88.255.202' IDENTIFIED BY '123456' WITH GRANT OPTION;

flush privileges;

Mysql 5.7 会出现

就执行以下两句即可

set global validate_password_policy=0;

set global validate_password_length=4;

原因可以参考此处: http://blog.csdn.net/maxsky/article/details/51171474

表示允许 101.88.255.202 服务器 使用 root用户登录,密码设置为 123456 拥有操作 所有的数据库的权限 

Linux MySQL的几个重要目录

数据库目录 /var/lib/mysql/

配置文件 /usr/share/mysql(mysql.service命令及配置文件)

相关命令 /usr/bin(mysqladmin mysqldump等命令)

启动脚本 /etc/rc.d/init.d/(启动脚本文件mysql的目录)

日志: /var/log/mysqld.log

授权用户从远程登录

注意:下面的2步骤都需要执行,步骤1.只是修改了用户的密码,但是并没有对用户进行授权,步骤2的作用就是,授权,让用户可以拥有指定的权限(连接数据库,查询数据库...);

1. 改表法。可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改称"%"

mysql -u root -pvmware;

mysql>use mysql;

mysql>update user set host = '%' where user = 'root';

mysql>select host, user from user;

2. 授权法。例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话。

GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;

如果你想允许用户myuser从ip为192.168.1.3的主机连接到mysql服务器,并使用mypassword作为密码

GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;

【下面这一句一定要执行,否则还是无法登陆】

mysql>flush privileges ;

如果用户无法从本地登陆,这个时候就执行如下

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost.localdomain' IDENTIFIED BY '123456' WITH GRANT OPTION;

别忘了加上如下的语句

flush privileges ;

猜你喜欢

转载自blog.csdn.net/u011537073/article/details/88605512