Yum 安装并设置 MySQL
Posted LarryZeal
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Yum 安装并设置 MySQL相关的知识,希望对你有一定的参考价值。
本质上都是设置一个yum源,然后yum安装即可。
不过添加yum源有两个方法,一种是自己新建一个 /etc/yum.repos.d/mysql-community.repo ;另一种是使用MySQL提供的rpm包来安装yum源。二者本质一样。
第一种
①、新建
vim /etc/yum.repos.d/mysql-community.repo
②、填写内容
[[email protected] ~]# vim /etc/yum.repos.d/mysql-community.repo [mysql-connectors-community] name=MySQL Connectors Community baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/6/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-tools-community] name=MySQL Tools Community baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/6/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql # Enable to use MySQL 5.5 [mysql55-community] name=MySQL 5.5 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/6/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql # Enable to use MySQL 5.6 [mysql56-community] name=MySQL 5.6 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/ enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql80-community] name=MySQL 8.0 Community Server baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/6/$basearch/ enabled=0 gpgcheck=1
注意:需要安装哪个版本,就将那个版本的enabled设为1。上面的设置是启用了5.6版本。
③清除yum缓存
yum clean all
④更新yum缓存
yum makecache
⑤安装MySQL
sudo yum install mysql-community-server
⑥启动MySQL
sudo service mysqld start
第二种
①、下载rpm
网址:https://dev.mysql.com/downloads/repo/yum/
找到下面这一段,然后根据需要来下载。CentOS等同于Red Hat。我用的是CentOS 6.8,所以需要下载 mysql57-community-release-el6-9.noarch.rpm 。
Red Hat Enterprise Linux 7 / Oracle Linux 7 (Architecture Independent), RPM Package | 9.0K | |||
(mysql57-community-release-el7-9.noarch.rpm) | MD5: 1a29601dc380ef2c7bc25e2a0e25d31e | |||
Red Hat Enterprise Linux 6 / Oracle Linux 6 (Architecture Independent), RPM Package | 9.0K | |||
(mysql57-community-release-el6-9.noarch.rpm) | MD5: 63eb7275f5473a7383c6e929e7ebfb96 | |||
Red Hat Enterprise Linux 5 / Oracle Linux 5 (Architecture Independent), RPM Package | 8.2K | |||
(mysql57-community-release-el5-7.noarch.rpm) | MD5: 0b141183300cfd0b22c2896f61440554 | |||
Fedora 25 (Architecture Independent), RPM Package | 13.2K | |||
(mysql57-community-release-fc25-9.noarch.rpm) | MD5: bea9b259f21bd745e70344e3d201f86f | |||
Fedora 24 (Architecture Independent), RPM Package | 13.1K | |||
(mysql57-community-release-fc24-9.noarch.rpm) | MD5: 503bf2218267a22425c914c31ab6908c | |||
Fedora 23 (Architecture Independent), RPM Package | 13.0K | |||
(mysql57-community-release-fc23-9.noarch.rpm) | MD5: 822d142b5d9e97b22adc8763ccfa62bb |
②安装 mysql57-community-release-el6-9.noarch.rpm
没什么好说的。
rpm -ivh mysql57-community-release-el6-9.noarch.rpm
③修改MySQL的yum源
第三步及以后的做法参见第一种的做法,完全一致。
另外,MySQL 5.6 和MySQL 5.7 安装完之后需要的操作并不相同,务必注意。
还是写一下吧。
对于MySQL 5.6,可以使用 mysql_secure_installation 来执行重要的操作,如设置root密码、移除匿名用户等等。
注意,千万不要在安装完MySQL 5.7之后执行该操作,因为Yum安装时已经执行了该操作!!!
注意,务必记住你的root密码。
来看看官方对于 mysql_secure_installation 的介绍:
- 你可以为root 账号设置密码。
- 你可以移除来自外部和local host可访问的root 账号。
- 你可以移除匿名账户。
- 你可以移除test 数据库(该数据库默认可以被所有用户访问 -- 包括匿名账户),还可以移除允许任意人访问 以test_开头的数据库的权限。
注意:自MySQL 5.7.2起,mysql_secure_installation 变成了一个可执行的二进制文件,能够在所有的平台上使用。在此之前,mysql_secure_installation 只是一个脚本,仅能用于Unix和Unix-like的系统上。
常规的用法是:①先启动 MySQL server;②在localhost上直接调用 mysql_secure_installation 。
当执行以后,会让你选择执行什么动作,如下。
[[email protected] ~]# service mysqld status mysqld (pid 1293) 正在运行... [[email protected] ~]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MySQL to secure it, we‘ll need the current password for the root user. If you‘ve just installed MySQL, and you haven‘t set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MySQL root user without the proper authorisation. Set root password? [Y/n] Y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! 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? [Y/n] Y ... Success! Normally, root should only be allowed to connect from ‘localhost‘. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] Y ... Success! By default, MySQL comes with a database named ‘test‘ that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] Y - Dropping test database... ERROR 1008 (HY000) at line 1: Can‘t drop database ‘test‘; database doesn‘t exist ... Failed! Not critical, keep moving... - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] Y ... Success! All done! If you‘ve completed all of the above steps, your MySQL installation should now be secure. Thanks for using MySQL! Cleaning up... [[email protected] ~]#
注意:请确认mysqld已经在运行。
一开始让你输入root用户的密码,如果从来没设置过,直接回车即可:
Enter current password for root (enter for none):
是否需要设置root 账号的密码,建议最好设一个,所以输入Y并回车:
Set root password? [Y/n] Y
输入要设置的密码,会重复一次,两次一致的话会提示密码更新成功:
New password: Re-enter new password: Password updated successfully!
是否需要删除匿名用户,当然需要,输入Y并回车:
Remove anonymous users? [Y/n] Y
是否需要禁止root 账号的远程登录,这个看情况了,我需要,所以输入Y并回车:
Disallow root login remotely? [Y/n] Y
是否移除test 数据库以及对其的访问权限,需要,输入Y并回车:
Remove test database and access to it? [Y/n] Y
是否重新加载权限表,需要,输入Y并回车:
Reload privilege tables now? [Y/n] Y
OK,至此已经搞定MySQL 5.6了,可以愉快的玩耍了。
---------------下面的内容其实是MySQL 5.7.2以后的了---------------
另外,自MySQL 5.7.2起,mysql_secure_installation 支持这些额外的功能:
- validate_password插件用于密码强度检查。如果没有安装,mysql_secure_installation 会询问用户是否安装它。
- 多数常用的MySQL client options如
--host
和--port
,既可以放在命令当中,也可以放入option 文件中。
对于MySQL 5.7,安装完成之后的第一次启动时,后台会发生这些操作:
- 初始化服务器。
- 在data目录下生成一个SSL认证和key文件。
- 安装并启用 validate_password plugin 。--这个插件的作用就是要求密码包含最少一个大写字符、一个小写字符、一个数字、以及一个特殊字符,且总长度不低于8位。
- 创建了一个超级用户:
‘root‘@‘localhost‘
。其初始密码存储于错误日志文件中,可以使用下面的命令来查看:
sudo grep ‘temporary password‘ /var/log/mysqld.log
请务必在第一次登录之后更改密码!!!
#登录,记得输入初始密码 shell> mysql -uroot -p #进入mysql之后就可以更改密码了。 mysql> ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘{你的新密码}‘;
官方网址:
https://dev.mysql.com/downloads/repo/yum/
https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/
https://dev.mysql.com/doc/refman/5.7/en/mysql-secure-installation.html
以上是关于Yum 安装并设置 MySQL的主要内容,如果未能解决你的问题,请参考以下文章
CentOS6.8下使用yum安装mysql,并远程登陆mysql