CentOS7 yum安装MySQL5.6

687 阅读3分钟

CentOS 7和CentOS 7.1系统中,默认安装的mysql是它的分支mariadb。这里引用下 百度百科关于mariadb的描述:

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。 因此,下面这些正常的MySQL操作都是无效的:

[root@typecodes ~]# chkconfig mysqld on
error reading information on service mysqld: No such file or directory
#######启动不了mysql
[root@typecodes ~]# service mysqld start
Redirecting to /bin/systemctl start  mysqld.service
Failed to issue method call: Unit mysqld.service failed to load: No such file or directory.
#######没有安装mysql相关包
[root@typecodes ~]# rpm -qa |grep mysql
php-mysql-5.4.16-23.el7_0.3.x86_64
[root@typecodes ~]#

正确的安装方法:

Linux系统自带的repo是不会自动更新每个软件的最新版本(基本都是比较靠后的稳定版),所以无法通过yum方式安装MySQL的高级版本。所以我们需要先安装带有当前可用的mysql5系列社区版资源的rpm包。

  • 安装rpm包
[root@typecodes ~]# rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
Retrieving http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-release-el7-5    ################################# [100%]

这时查看当前可用的mysql安装资源:

[root@typecodes ~]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community                 14
mysql-tools-community/x86_64      MySQL Tools Community                      17
mysql56-community/x86_64          MySQL 5.6 Community Server                139

从上面的列表可以看出, mysql56-community/x86_64 和 MySQL 5.6 Community Server 可以使用。 因此,我们就可以直接用yum方式安装了MySQL5.6版本了。

[root@typecodes ~]# yum -y install mysql-community-server

MySQL安装完成后,进行相关配置

安装完MySQL后,需要进行一些基础配置工作:

  • 安装成功后,将其加入开机启动
[root@typecodes ~]# systemctl enable mysqld
  • 启动mysql服务进程
[root@typecodes ~]# systemctl start mysqld
  • 配置mysql(设置密码等)
    1. 为root用户设置密码
    2. 删除匿名账号
    3. 取消root用户远程登录
    4. 删除test库和对test库的访问权限
    5. 刷新授权表使修改生效
[root@typecodes ~]# mysql_secure_installation
[root@server1 ~]# 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] <– 是否设置root用户密码,输入y并回车或直接回车
New password: <– 设置root用户的密码
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] <– 是否删除匿名用户,生产环境建议删除,所以直接回车
… 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] <–是否禁止root远程登录,根据自己的需求选择Y/n并回车,建议禁止
… 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] <– 是否删除test数据库,直接回车
- Dropping test database…
… Success!
- 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] <– 是否重新加载权限表,直接回车
… Success!
Cleaning up…
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!

进入mysql

# mysql -uroot -p%密码%

用户授权

开启远程访问权限“%”代表所有人

mysql>use mysql;
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;