centos7之RPM安装MySQL5.5.48

来源:互联网 发布:手机扫描文档软件 编辑:程序博客网 时间:2024/06/11 09:18

背景:
一般项目系统开发都是在windows平台上,而一旦项目系统开发完成,出于安全性、稳定性的考虑,数据库服务器、应用服务器都会安装在Linux平台上,本篇讲解的就是如何在Linux的发行版centos7安装MySQL数据库。这里仅仅是做实验,所以是在虚拟机上安装的centos7之上安装MySQL。

前提:
1、在虚拟机centos7上安装MySQL必须保证主机与虚拟机之间是可以互相ping通的,这里不详细讲述,如有需要可以参考虚拟机静态配置IP地址一篇:http://blog.csdn.net/qq_34983808/article/details/78711666

2、centos的版本是centos7:http://isoredirect.centos.org/centos/7/isos/x86_64/

centos7

MySQL的版本是MySQL5.5.48:https://downloads.mysql.com/archives/community/

MySQL5.5.48


安装步骤:
1、上传下载到的MySQL tar包到虚拟机centos上并解压到指定的目录下

[root@localhost ~]# ll
总用量 186968
-rw——-. 1 root root 1740 12月 4 19:00 anaconda-ks.cfg
-rw-r–r–. 1 root root 191447040 12月 9 09:59 MySQL-5.5.48-1.linux2.6.x86_64.rpm-bundle.tar
drwxr-xr-x. 3 root root 4096 12月 9 10:00 soft

[root@localhost ~]# tar -xvf MySQL-5.5.48-1.linux2.6.x86_64.rpm-bundle.tar -
C soft/mysql/

MySQL-client-5.5.48-1.linux2.6.x86_64.rpm
MySQL-shared-compat-5.5.48-1.linux2.6.x86_64.rpm
MySQL-test-5.5.48-1.linux2.6.x86_64.rpm
MySQL-server-5.5.48-1.linux2.6.x86_64.rpm
MySQL-embedded-5.5.48-1.linux2.6.x86_64.rpm
MySQL-devel-5.5.48-1.linux2.6.x86_64.rpm
MySQL-shared-5.5.48-1.linux2.6.x86_64.rpm

[root@localhost ~]# cd soft/mysql/
[root@localhost mysql]# ll
总用量 186960
-rw-r–r–. 1 7155 wheel 17855952 1月 17 2016 MySQL-client-5.5.48-1.linux2.6.x86_64.rpm
-rw-r–r–. 1 7155 wheel 6522838 1月 17 2016 MySQL-devel-5.5.48-1.linux2.6.x86_64.rpm
-rw-r–r–. 1 7155 wheel 67718815 1月 17 2016 MySQL-embedded-5.5.48-1.linux2.6.x86_64.rpm
-rw-r–r–. 1 7155 wheel 50372369 1月 17 2016 MySQL-server-5.5.48-1.linux2.6.x86_64.rpm
-rw-r–r–. 1 7155 wheel 2038979 1月 17 2016 MySQL-shared-5.5.48-1.linux2.6.x86_64.rpm
-rw-r–r–. 1 7155 wheel 5180005 1月 17 2016 MySQL-shared-compat-5.5.48-1.linux2.6.x86_64.rpm
-rw-r–r–. 1 7155 wheel 41748495 1月 17 2016 MySQL-test-5.5.48-1.linux2.6.x86_64.rpm

2、卸载centos7自带的mariadb,不然安装MySQL的时候会和mariadb文件相冲突导致安装不成功。

[root@localhost mysql]# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@localhost mysql]# rpm -e mariadb-libs-5.5.56-2.el7.x86_64
–nodeps

3、安装MySQL服务端和客户端

[root@localhost mysql]# rpm -ivh MySQL-server-5.5.48-1.linux2.6.x86_64.rpm

[root@localhost mysql]# rpm -ivh MySQL-client-5.5.48-1.linux2.6.x86_64.rpm

tips
one:必须进到rpm所在目录进行安装,不然会报错说找不到rpm

这里写图片描述

two:安装服务端会出现提示,提示很重要,很重要,很重要。涉及到MySQL的密码及其它的一些工作。

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password ‘new-password’
/usr/bin/mysqladmin -u root -h localhost.localdomain password ‘new-password’

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

所以安装完客户端之后,必须按照以上的提示修改密码:

[root@localhost mysql]# /usr/bin/mysql_secure_installation

问题:

Enter current password for root (enter for none):
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)

修改密码出现错误

原因:查看MySQL启动状态,可以看到MySQL服务没有打开,所以修改密码前必须先打开MySQL服务

[root@localhost mysql]# service mysql status
ERROR! MySQL is not running

4、开启MySQL服务

[root@localhost mysql]# service mysql start
[root@localhost mysql]# service mysql status
SUCCESS! MySQL running (1783)

5、修改密码

[root@localhost mysql]# /usr/bin/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] n
… skipping.

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…
… 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] y
… 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!

6、登录测试

[root@localhost mysql]# mysql -uroot -proot

7、开启远程MySQL数据库访问,这里测试我们的主机windows的客户端去连接虚拟机上centos上的MySQL数据库。

问题:无法访问
主机MySQL客户端访问Linux上的MySQL数据库

原因:centos7的防火墙设置、MySQL数据库远程访问的授权设置

[root@localhost mysql]# firewall-cmd –state
running
[root@localhost mysql]# firewall-cmd –list-ports
[root@localhost mysql]#

[root@localhost mysql]# mysql -uroot -proot
mysql> select * from mysql.user where user=’root’ \G;

***************** 1. row *****************
Host: localhost
User: root
Password: *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Reload_priv: Y
Shutdown_priv: Y
Process_priv: Y
File_priv: Y
Grant_priv: Y
References_priv: Y
Index_priv: Y
Alter_priv: Y
Show_db_priv: Y
Super_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Execute_priv: Y
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Create_user_priv: Y
Event_priv: Y
Trigger_priv: Y
Create_tablespace_priv: Y
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin:
authentication_string:
***************** 2. row *****************
Host: localhost.localdomain
User: root
Password: *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Reload_priv: Y
Shutdown_priv: Y
Process_priv: Y
File_priv: Y
Grant_priv: Y
References_priv: Y
Index_priv: Y
Alter_priv: Y
Show_db_priv: Y
Super_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Execute_priv: Y
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Create_user_priv: Y
Event_priv: Y
Trigger_priv: Y
Create_tablespace_priv: Y
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin:
authentication_string:
***************** 3. row *****************
Host: 127.0.0.1
User: root
Password: *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Reload_priv: Y
Shutdown_priv: Y
Process_priv: Y
File_priv: Y
Grant_priv: Y
References_priv: Y
Index_priv: Y
Alter_priv: Y
Show_db_priv: Y
Super_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Execute_priv: Y
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Create_user_priv: Y
Event_priv: Y
Trigger_priv: Y
Create_tablespace_priv: Y
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin:
authentication_string:
***************** 4. row *****************
Host: ::1
User: root
Password: *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Reload_priv: Y
Shutdown_priv: Y
Process_priv: Y
File_priv: Y
Grant_priv: Y
References_priv: Y
Index_priv: Y
Alter_priv: Y
Show_db_priv: Y
Super_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Execute_priv: Y
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Create_user_priv: Y
Event_priv: Y
Trigger_priv: Y
Create_tablespace_priv: Y
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin:
authentication_string:
4 rows in set (0.00 sec)

从以上结果可以看到:
防火墙处于开启状态且没有开启任何端口可以访问
MySQL数据库查询结果列Host的值,并没有是%或者指定远程主机名或ip地址,所以并没有授权远程访问登录设置。

解决:原因找到了之后,就好解决了。

打开MySQL数据库远程访问权限

[root@localhost mysql]# mysql -uroot -proot
mysql> GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY ‘root’ WITH GRANT OPTION; FLUSH PRIVILEGES;

再次查询MySQL数据库远程访问是否授权:

mysql> select * from mysql.user where user=’root’ \G;

可以看到多了一条记录HOST值为:%,说明授权了。

***************** 5. row *****************
Host: %
User: root
Password: *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
Create_priv: Y
Drop_priv: Y
Reload_priv: Y
Shutdown_priv: Y
Process_priv: Y
File_priv: Y
Grant_priv: Y
References_priv: Y
Index_priv: Y
Alter_priv: Y
Show_db_priv: Y
Super_priv: Y
Create_tmp_table_priv: Y
Lock_tables_priv: Y
Execute_priv: Y
Repl_slave_priv: Y
Repl_client_priv: Y
Create_view_priv: Y
Show_view_priv: Y
Create_routine_priv: Y
Alter_routine_priv: Y
Create_user_priv: Y
Event_priv: Y
Trigger_priv: Y
Create_tablespace_priv: Y
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin:
authentication_string: NULL
5 rows in set (0.00 sec)

添加MySQL端口运行远程访问

firewall-cmd –zone=public –add-port=3306/tcp –permanent

查询防火墙开放的端口:

[root@localhost mysql]# firewall-cmd –list-ports
3306/tcp

到这里,主机MySQL客户端就可以顺利连接centos7的MySQL数据库了。
主机客户端成功连接centos7MySQL数据库


总结:
本篇主要讲述了centos7安装MySQL数据库,前提是centos的版本和MySQL的版本必须一致才能按照以上的方法,因为不同版本的Linux、MySQL只有其中一个不同,安装的方式就有可能不一样。

安装MySQL数据库:
1、采用的是rpm安装

2、安装前需卸载掉centos7自带的mariadb,MariaDB数据库管理系统是MySQL的一个分支,不然安装的时候会互相冲突导致安装不成功。

3、安装
rpm -ivh MySQL-server-5.5.48-1.linux2.6.x86_64.rpm
rpm -ivh MySQL-client-5.5.48-1.linux2.6.x86_64.rpm

4、修改密码前需开启服务,开启服务后方可修改密码

5、本地登录MySQL数据库,OK没有问题
远程主机登录出现无法访问的情况,需设置防火墙开启3306端口可访问
其次,设置本地MySQL数据库运行远程主机登录访问授权。

以上就是安装的大体步骤,原理是一样的。


补充
关于卸载:
卸载步骤:
1、查看已经安装的MySQL服务:

[root@localhost mysql]# rpm -qa | grep -i mysql
MySQL-client-5.5.48-1.linux2.6.x86_64
MySQL-server-5.5.48-1.linux2.6.x86_64

2、卸载MySQL服务

[root@localhost mysql]# rpm -e MySQL-server-5.5.48-1.linux2.6.x86_64
[root@localhost mysql]# rpm -e MySQL-client-5.5.48-1.linux2.6.x86_64
[root@localhost mysql]# rpm -qa | grep -i mysql
[root@localhost mysql]#

注意:删除的时候没有带.rpm的后缀

3、查找MySQL相关文件夹

[root@localhost mysql]# find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
/usr/lib64/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/root/soft/mysql

注意:/root/soft/mysql是我们手动新建用来存放MySQL rpm的,所以并不需要把它卸载。

4、删除MySQL相关文件夹

[root@localhost mysql]# rm -rf /etc/selinux/targeted/active/modules/100/mysql
[root@localhost mysql]# rm -rf /usr/lib64/mysql
[root@localhost mysql]# rm -rf /var/lib/mysql
[root@localhost mysql]# rm -rf /var/lib/mysql/mysql
[root@localhost mysql]# rm -rf /usr/lib64/mysql

5、重新安装

[root@localhost mysql]# rpm -ivh MySQL-server-5.5.48-1.linux2.6.x86_64.rpm
[root@localhost mysql]# rpm -ivh MySQL-client-5.5.48-1.linux2.6.x86_64.rpm

接下来的流程就和上面的安装一样了。

原创粉丝点击