【linux】安装mysql

本文介绍如何在Linux环境下安装MySQL数据库,包括安装前的准备、安装过程及配置步骤,并提供了不同内存大小对应的my.cnf配置文件选择建议。

将mysql安装文件(下载地址:http://www.mysql.com/downloads/mysql-4.0.html

MySQL-client-community-5.0.37-0.rhel3.i386.rpm

MySQL-server-community-5.0.37-0.rhel3.i386.rpm

perl-DBI-1.53-2.fc7.i386.rpm

ftp上传到安装目录。用数据库用户登录系统,进行mysql的安装。

 

以下需要root权限:

查询是否安装过mysql

[root@宁波测试机 ~]$ rpm -qa|grep mysql

如果已经安装,需要卸载:

rpm -e MySQL-client-community MySQL-server-community

 

以下需要数据库用户权限:

[dbuser@宁波测试机 ~]$ rpm -ivh  MySQL-server-community-5.0.37-0.rhel3.i386.rpm

如果出现如下信息:
error: Failed dependencies:
        perl(DBI) is needed by MySQL-server-community-5.0.37-0.rhel3.i386


则需要执行:

[dbuser@宁波测试机 ~]$ rpm -ivh  perl-DBI-1.53-2.fc7.i386.rpm
error: can't create transaction lock on /var/lib/rpm/__db.000
说明权限不够,需要su到root用户,执行:

[root@宁波测试机 db]# rpm -ivh   perl-DBI-1.53-2.fc7.i386.rpm
Preparing...                ########################################### [100%]
   1:perl-DBI               ########################################### [100%]

 

执行安装

[root@宁波测试机 db]# rpm -ivh  MySQL-server-community-5.0.37-0.rhel3.i386.rpm
Preparing...                ########################################### [100%]
   1:MySQL-server-community ########################################### [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 宁波测试机 password 'new-password'
See the manual for more instructions.

Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
Starting MySQL[确定]

[root@宁波测试机 db]# rpm -ivh  MySQL-client-community-5.0.37-0.rhel3.i386.rpm
Preparing...                ########################################### [100%]
   1:MySQL-client-community ########################################### [100%]

 

安装完后,需要对mysql进行配置

见“mysql问题总结”中的my.cnf配置

 

注:my.cnf文件的获取:

如果你的内存≤64M,则复制/usr/share/mysql/my-small.cnf为/etc/my.cnf
# This is for a system with little memory (<= 64M) where MySQL is only used
# from time to time and it's important that the mysqld daemon
# doesn't use much resources.

如果内存是128M,则复制/usr/share/mysql/my-medium.cnf为/etc/my.cnf
# This is for a system with little memory (32M - 64M) where MySQL plays
# an important part, or systems up to 128M where MySQL is used together with
# other programs (such as a web server)

如果内存是512M,则复制/usr/share/mysql/my-large.cnf为/etc/my.cnf
# This is for a large system with memory = 512M where the system runs mainly
# MySQL.

如果内存是1-2G,则复制/usr/share/mysql/my-huge.cnf为/etc/my.cnf
# This is for a large system with memory of 1G-2G where the system runs mainly
# MySQL.

如果内存是4G,则复制/usr/share/mysql/my-innodb-heavy-4G.cnf为/etc/my.cnf
# This is a MySQL example config file for systems with 4GB of memory
# running mostly MySQL using InnoDB only tables and performing complex
# queries with few connections.

 

数据库启动命令权限分配

数据库启动命令现在只有root用户可以执行,我们想把权限分配给dbuser用户,给root分权,可以这样做:

      更改sudo配置文件/etc/sudoers,增加如下内容:

          Cmnd_Alias MYSQL=/etc/init.d/mysql, /usr/bin/mysqld_safe, /usr/bin/mysqladmin

          dbuser  ALL=(ALL) NOPASSWD:MYSQL

      之后就可以通过dbuser用户启动、停止mysql,命令如下:

 

启动:sudo /etc/init.d/mysql start
停止:sudo /etc/init.d/mysql stop

 

 

给root用户设置密码

刚安装好的mysql,root用户可以不用密码,直接登陆数据库,需要将root设置密码来降低风险。在mysql命令行输入:

set password for root@localhost =PASSWORD('newPassword');

 

 

 

 

 

### 安装 MySQL 的准备工作 在Linux系统上安装MySQL前,需确认当前环境中是否已存在旧版MySQL实例。这一步骤至关重要,因为残留的MySQL组件可能会引发冲突。可以利用`rpm -qa | grep mysql`来检测现有安装情况[^1]。 ### 获取并准备 MySQL 软件包 对于基于RPM的发行版,如CentOS或Red Hat Enterprise Linux (RHEL),可以通过wget命令下载官方提供的MySQL软件包。例如: ```bash wget http://dev.mysql.com/get/mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar ``` 此操作会获取适用于特定版本Linux系统的MySQL压缩文件[^2]。 ### 解压与部署 MySQL 文件夹 下载完成后,使用tar工具解压所获得的`.tar.gz`格式档案至指定位置,通常建议放置于`/usr/local/`路径下作为标准实践的一部分: ```bash tar -zxvf mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz cp -r mysql-5.6.33-linux-glibc2.5-x86_64 /usr/local/mysql ``` 上述指令实现了将解压后的MySQL目录复制到目标位置的操作[^3]。 ### 创建专用用户和服务脚本设置 为了安全性和权限管理的目的,应当创建专门用于运行MySQL进程的新用户和组。此外,还需把MySQL的服务控制脚本拷贝到初始化脚本的位置,并赋予其可执行权限以及注册为开机自启项之一: ```bash cp -a ./support-files/mysql.server /etc/init.d/mysql chmod +x /etc/init.d/mysql chkconfig --add mysql ``` 这些步骤确保了MySQL能够被正确识别为操作系统级别的服务[^4]。 ### 启动 MySQL 服务 完成以上配置之后,即可尝试启动MySQL服务以验证整个安装过程的成功与否: ```bash service mysql start ``` 如果一切顺利,则表示MySQL已经在Linux平台上成功安装完毕。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值