Linux环境下安装MySQL数据库

时间:2024-04-12 21:34:18

  Linux安装mysql服务分两种安装方法:

  (1)、源码安装,优点是安装包比较小,只有十多M,缺点是安装依赖的库多,安装编译时间长,安装步骤复杂容易出错;

  (2)、使用官方编译好的二进制文件安装,优点是安装速度快,安装步骤简单,缺点是安装包很大,300M左右。以下介绍linux使用官方编译好的二进制包安装mysql。

  楼主用的是第二种方法,首先说一下环境参数:

    硬件配置:
      CPU: Xeon(R) CPU E5-2650 v4 @ 2.20GHz 8核
      内存:16G
      硬盘:系统盘200GB 数据盘1TB
    操作系统
      CentOS 7.2 64位
    数据库
      MySQL 5.6.43

  具体步骤如下:

  1、下载MySQL数据包,地址:https://dev.mysql.com/downloads/mysql/5.6.html#downloads,注意选择相应的数据包,楼主下载的是5.6.43通用版,Linux下64位

Linux环境下安装MySQL数据库

  

  2、检查当前环境下是否已安装MySQL服务

  输入命令:

find / -name mysql

  如果已安装会显示如下:

Linux环境下安装MySQL数据库

  如果要删除老版本或者以前的残余文件,可执行: find / -name mysql|xargs rm -rf,或者rm -rf 上边查找到的路径,多个路径用空格隔开。

  3、处理MySQL文件

  将下载的文件上传到Linux的/data下并解压,当然了,也可以放置在你想放置的目录下。

tar -zxvf mysql-5.6.-linux-glibc2.-x86_64.tar.gz

  将解压后的文件重命名为mysql

mv mysql-5.6.43-linux-glibc2.12-x86_64 mysql

  4、创建mysql用户组及用户

groupadd mysql
useradd -r -g mysql mysql

  5、进入到mysql目录,执行添加MySQL配置的操作

cp support-files/my-medium.cnf /etc/my.cnf
或:
cp support-files/my-default.cnf /etc/my.cnf

  是否覆盖?按y 回车

  6、编辑/etc/my.cnf文件

vim /etc/my.cnf

  进入编辑模式,添加或者修改相关配置,更改完成后保存退出

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL. [mysqld]
explicit_defaults_for_timestamp=true # Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at % of total RAM for dedicated server, else %.
# innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin # These are commonly set, remove the # and set as required.
basedir = /data/mysql
datadir = /data/mysql/data
port =
# server_id = .....
socket = /tmp/mysql.sock
character-set-server = utf8
skip-name-resolve
log-error = /data/mysql/data/error.log
pid-file = /data/mysql/data/mysql.pid # Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

  7、在mysql当前目录下设定目录的访问权限(注意后面的小点,表示当前目录)

chown -R mysql .
chgrp -R mysql .
scripts/mysql_install_db --user=mysql
chown -R root .
chown -R mysql data

  8、初始化数据(在mysql/bin或者mysql/scripts下有个 mysql_install_db 可执行文件初始化数据库),进入mysql/bin或者mysql/scripts目录下,执行下面命令

./mysql_install_db --verbose --user=root --defaults-file=/etc/my.cnf --datadir=/data/mysql/data --basedir=/data/mysql --pid-file=/data/mysql/data/mysql.pid --tmpdir=/tmp

  9、启动mysql,进入/usr/local/mysql/bin目录,执行下面命令,如果没有这个目录不执行也无所谓

./mysqld_safe --defaults-file=/etc/my.cnf --socket=/tmp/mysql.sock --user=root &

  10、设置开机启动,新开启shell中断后,进入mysql目录,执行下面命令

cp /data/mysql/support-files/mysql.server /etc/init.d/mysqld
cp /data/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql
chmod /etc/init.d/mysql
chkconfig --add mysqld
chkconfig --level mysqld on
chown mysql:mysql -R /data/mysql/

  重启linux

reboot

  查看mysql状态

service mysqld status

  11、添加远程访问权限

  (1)、添加mysql命令

ln  -s /data/mysql/bin/mysql  /usr/bin  (mysql的安装路径)

  (2)、更改访问权限

#登录mysql,执行下面命令
mysql -uroot -p
#密码为空直接回车,运行以下两条命令
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' with grant option;
Flush privileges;

  退出mysql,重启linux

exit
reboot

  注:本机访问mysql,root账户默认是没有密码的,端口号默认3306,如果需要修改root账户密码,在/data/mysql/bin目录下,执行下面命令

./mysqladmin -h 127.0.0.1 -P3306 -uroot password 'root'exit

  密码已经修改为root,可以登录使用了。