Linux 环境下安装MySql数据库

来源:互联网 发布:mac系统截图快捷键 编辑:程序博客网 时间:2024/05/21 11:00

      在Hadoop开发的时候我们要用到mysql数据库,因此我们有必要学习一下怎样在Linux下安装mysql。

第一步:下载MySQL-client-5.1.73-1.glibc23.x86_64.rpm和MySQL-server-5.1.73-1.glibc23.x86_64.rpm

     我们在地址栏输入:http://ftp.ntu.edu.tw/pub/MySQL/Downloads/MySQL-5.1/回车就会看到如下图所示界面,我们找到我们要下载的文件进行下载。

第二步:将上面下载的两个文件上传到服务器,这里我用的是CentOS虚拟机,我们可以使用FileZilla工具将文件上传上去。如下图所示。

第三步:先安装MySQL-server-5.1.73-1.glibc23.x86_64.rpm

[root@itcast05 ~]# ls
anaconda-ks.cfg                Desktop    hadoop-2.2.0         install.log.syslog    MySQL-client-5.1.73-1.glibc23.x86_64.rpm  protobuf-2.5.0.tar.gz  Templates
apache-ant-1.9.2-bin.tar.gz    Documents  hadoop-2.2.0.tar.gz  jdk-7u80-linux-x64.gz MySQL-server-5.1.73-1.glibc23.x86_64.rpm  Public                 Videos
apache-maven-3.0.5-bin.tar.gz  Downloads  install.log          Music                  Pictures                                  target

        我们使用命令rpm -ivh MySQL-server-5.1.73-1.glibc23.x86_64.rpm来进行安装,其中i是安装的意思,v是显示详情的意思,h是显示进度条的意思。我们会发现与本地已经安装的mysql数据库发生了冲突。
[root@itcast05 ~]# rpm -ivh MySQL-server-5.1.73-1.glibc23.x86_64.rpm
Preparing...                ########################################### [100%]
        file /usr/share/mysql/charsets/README from install of MySQL-server-5.1.73-1.glibc23.x86_64 conflicts with file from package mysql-libs-5.1.73-7.el6.x86_64
        file /usr/share/mysql/charsets/Index.xml from install of MySQL-server-5.1.73-1.glibc23.x86_64 conflicts with file from package mysql-libs-5.1.73-7.el6.x86_64
        file /usr/share/mysql/charsets/armscii8.xml from install of MySQL-server-5.1.73-1.glibc23.x86_64 conflicts with file from package mysql-libs-5.1.73-7.el6.x86_64
        file /usr/share/mysql/charsets/ascii.xml from install of MySQL-server-5.1.73-1.glibc23.x86_64 conflicts with file from package mysql-libs-5.1.73-7.el6.x86_64
        省略了一部分信息
[root@itcast05 ~]#

        既然发生了冲突,我们便来解决这个问题,我们先找到本地已有的mysql,我们使用命令rpm -qa | grep mysql可以找到本地的mysql。

[root@itcast05 ~]# rpm -qa | grep mysql
mysql-libs-5.1.73-7.el6.x86_64

         找到mysql-libs-5.1.73-7.el6.x86_64之后我们来卸载它,使用的命令是rpm -e mysql-libs-5.1.73-7.el6.x86_64,我们会发现报错了,说有依赖项。
[root@itcast05 ~]# rpm -e mysql-libs-5.1.73-7.el6.x86_64
error: Failed dependencies:
        libmysqlclient.so.16()(64bit) is needed by (installed) postfix-2:2.6.6-6.el6_7.1.x86_64
        libmysqlclient.so.16(libmysqlclient_16)(64bit) is needed by (installed) postfix-2:2.6.6-6.el6_7.1.x86_64
        mysql-libs is needed by (installed) postfix-2:2.6.6-6.el6_7.1.x86_64

        那么我们就使用强制卸载的方式来卸载,方法是在命令后加--nodeps,意思是强制解除依赖关系。
[root@itcast05 ~]# rpm -e mysql-libs-5.1.73-7.el6.x86_64 --nodeps
[root@itcast05 ~]#

        卸载完了原来的mysql,我们现在再来安装server,如下所示,发现安装成功了。

[root@itcast05 ~]# rpm -ivh MySQL-server-5.1.73-1.glibc23.x86_64.rpm
Preparing...                ########################################### [100%]
   1:MySQL-server           ########################################### [100%]
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 itcast05 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.
See the manual for more instructions.
Please report any problems with the /usr/bin/mysqlbug script!
Starting MySQL. SUCCESS! 
[root@itcast05 ~]#
第四步:安装MySQL-client-5.1.73-1.glibc23.x86_64.rpm
       我们为了配置方便,再安装一下client,如下所示
[root@itcast05 ~]# rpm -ivh MySQL-client-5.1.73-1.glibc23.x86_64.rpm
Preparing...                ########################################### [100%]
   1:MySQL-client           ########################################### [100%]
[root@itcast05 ~]#
第五步:初始化Mysql
      由于我们现在还不知道mysql的用户名和密码,因此我们需要对mysql进行初始化,那么怎么初始化呢?其实我们安装MySQL-server-5.1.73-1.glibc23.x86_64.rpm的时候人家已经告诉我们了,在安装的信息当中有这么一段话,意思是我们可以使用/usr/bin/mysql_secure_installation这条命令来进行初始化。
Alternatively you can run:
/usr/bin/mysql_secure_installation
      那我们便使用/usr/bin/mysql_secure_installation这个脚本来初始化一下
[root@itcast05 ~]# /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.
      接下来会让我们选择是否现在设置密码,我们选择"Y"
Set root password? [Y/n] Y
      这里输入密码,比如我们就用root作为密码
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.
       提示是否删除所有匿名用户,我们选择"Y"进行删除。
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.
      提示是否不允许远程连接,我们选择"n"意思是允许远程连接
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.
       提示是否删除测试数据库,我们不删除,选择n
Remove test database and access to it? [Y/n] n
 ... skipping.
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
      提示是否重新刷新表权限,我们选择"Y"
Reload privilege tables now? [Y/n] Y
 ... Success!
Cleaning up...
      到此我们的mysql初始化便完成了。
All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!

[root@itcast05 ~]#
第六步:检验安装成果
        我们登录mysql,我们使用我们刚才设置的root密码来登录,发现登录成功了!
[root@itcast05 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.1.73 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
       我们来查看一下当前都有哪些数据库,发现正确查到了结果,说明我们的mysql安装成功了!!
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)
mysql>
原创粉丝点击