我们平时都安装了很多软件,发现其实数据库是最繁琐的安装,Oracle,SQL Server,MySQL,安装起来都比其他的开发软件都复杂很多,各种配置,权限,用户,还有些是默认选项,都不知道什么意思,尤其是在windows上安装这些软件,啥都不懂,安装完了就一脸蒙蔽了,也不知道是对是错。如今我也算是终于弄懂了MySQL在Centos6.9上的安装,给大家分享一下吧。
一、MySQL5.6.36安装前准备

  1. 准备CentOS6.9,注意做好快照
    [root@db01 ~]# cat /etc/redhat-release 
    CentOS release 6.9 (Final)
    [root@db01 ~]# 

     

  2. IP:10.0.0.20 (eth0,eth1)  主机名:db01
    eth0:
    [root@db01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
    
    DEVICE=eth0
    TYPE=Ethernet
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=none
    IPADDR=10.0.0.20
    NETMASK=255.255.255.0
    GATEWAY=10.0.0.254
    USERCTL=no
    PEERDNS=yes
    IPV6INIT=no
    DNS1=223.5.5.5
    DNS2=223.6.6.6

    eth1: 

    [root@db01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1
    
    # Please read /usr/share/doc/initscripts-*/sysconfig.txt
    # for the documentation of these parameters.
    DEVICE=eth1
    BOOTPROTO=none
    NETMASK=255.255.255.0
    TYPE=Ethernet
    IPADDR=172.16.1.20
    NM_CONTROLLED=no
    PEERDNS=yes
    ONBOOT=yes
    IPV6INIT=no
    USERCTL=no

    hostname:

    [root@db01 ~]# vim /etc/sysconfig/network
    
    NETWORKING=yes
    HOSTNAME=db01

     

  3. 关闭iptables和selinux
    #关闭iptables
    /etc/init.d/iptables stop
    chkconfig iptables off
    [root@db01 ~]# service iptables status
    iptables: Firewall is not running.
    [root@db01 ~]# 

    #关闭selinux
    sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
    grep SELINUX=disabled /etc/selinux/config
    setenforce 0
    getenforce

    [root@db01 ~]# getenforce
    Disabled
    [root@db01 ~]# 

     

  4. 下载好mysql5.6.36
    #下载地址:https://downloads.mysql.com/archives/community/  选择5.6.36  source code  download地址
  5. 安装依赖包
    yum install -y ncurses-devel libaio-devel
    [root@db01 ~]# yum install -y ncurses-devel libaio-devel

     

  6. 安装cmake
    yum install cmake -y (mysql5.5以后将不再使用./configure,而是用cmake替代)
    [root@db01 ~]# yum install cmake -y

     

  7. 创建用户
    useradd -s /sbin/nologin -M mysql (-s /sbin/nologin 不允许该用户登陆,-M mysql 指定用户的家目录为mysql)
    id mysql
    [root@db01 ~]# useradd -s /sbin/nologin -M mysql
    [root@db01 ~]# id mysql
    uid=516(mysql) gid=516(mysql) groups=516(mysql)
    [root@db01 ~]# 

     

二、MySQL安装

  1. 创建/opt/software目录,将下载好的mysql-5.6.36.tar.gz上传到该目录中,然后解压。
    [root@db01 software]# ll
    total 31440
    -rw-r--r-- 1 root root 32192348 Mar 18  2017 mysql-5.6.36.tar.gz
    [root@db01 software]# tar xf mysql-5.6.36.tar.gz 
    [root@db01 software]# ll
    total 31444
    ldrwxr-xr-x 33 7161 31415     4096 Mar 18  2017 mysql-5.6.36
    -rw-r--r--  1 root root  32192348 Mar 18  2017 mysql-5.6.36.tar.gz
    [root@db01 software]# ls mysql-5.6.36
    BUILD           cmd-line-utils   dbug                 include   libmysqld    mysys      README   sql-bench   support-files  vio
    client          config.h.cmake   Docs                 INSTALL   libservices  mysys_ssl  regex    sql-common  tests          win
    cmake           configure.cmake  Doxyfile-perfschema  libevent  man          packaging  scripts  storage     unittest       zlib
    CMakeLists.txt  COPYING          extra                libmysql  mysql-test   plugin     sql      strings     VERSION
    [root@db01 software]# 

     

  2. 进入mysql-5.6.36,执行cmake,安装mysql。

    [root@db01 software]# cd mysql-5.6.36
    [root@db01 mysql-5.6.36]#cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.36 \
    -DMYSQL_DATADIR=/application/mysql-5.6.36/data \
    -DMYSQL_UNIX_ADDR=/application/mysql-5.6.36/tmp/mysql.sock \
    -DDEFAULT_CHARSET=utf8 \
    -DDEFAULT_COLLATION=utf8_general_ci \
    -DWITH_EXTRA_CHARSETS=all \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_FEDERATED_STORAGE_ENGINE=1 \
    -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
    -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
    -DWITH_ZLIB=bundled \
    -DWITH_SSL=bundled \
    -DENABLED_LOCAL_INFILE=1 \
    -DWITH_EMBEDDED_SERVER=1 \
    -DENABLE_DOWNLOADS=1 \
    -DWITH_DEBUG=0

    #-DMYSQL_DATADIR=/application/mysql-5.6.36/data  为mysql数据目录,自动创建。
    #-DMYSQL_UNIX_ADDR=/application/mysql-5.6.36/tmp/mysql.sock  为套接字文件,自动创建。
    如果上面的3个目录和文件没有被创建,请手动创建,否则后续将会有报错。
     

  3. 继续make && make install(时间有点长,很长很长的那种,没半年跑不完的那种)

    [root@db01 mysql-5.6.36]# make && make install

     

三、配置并启动

  1. 制作软连接:(管理员进入软连接,升级时,直接删除源目录,不影响管理员正常操作)
    ln -s /application/mysql-5.6.36/ /application/mysql
    [root@db01 application]# ln -s /application/mysql-5.6.36/ /application/mysql
    [root@db01 application]# ll
    total 4
    lrwxrwxrwx  1 root root   26 Jul 31 15:57 mysql -> /application/mysql-5.6.36/
    drwxr-xr-x 13 root root 4096 Jul 31 15:53 mysql-5.6.36
    [root@db01 application]# 
  2. 初始化数据库:
    [root@db01 application]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data --user=mysql
    Installing MySQL system tables...2019-07-31 16:09:02 0 [Warning] TIMESTAMP with implicit DEFAULT
    2019-07-31 16:09:03 17856 [Note] InnoDB: Using atomics to ref count buffer pool pages
    2019-07-31 16:09:03 17856 [Note] InnoDB: The InnoDB memory heap is disabled
    2019-07-31 16:09:03 17856 [Note] InnoDB: Completed initialization of buffer pool
    2019-07-31 16:09:03 17856 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
         .
         .
      省略部分
         .
         .
    
    PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
    To do so, start the server, then issue the following commands:
    #重置数据库密码
      /application/mysql//bin/mysqladmin -u root password 'new-password'
      /application/mysql//bin/mysqladmin -u root -h db01 password 'new-password'
    #或者
    Alternatively you can run:
    
      /application/mysql//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.
    
    You can start the MySQL daemon with:
    
      cd . ; /application/mysql//bin/mysqld_safe &
    
    You can test the MySQL daemon with mysql-test-run.pl
    
      cd mysql-test ; perl mysql-test-run.pl
    
    Please report any problems at http://bugs.mysql.com/
    
    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
    #初始化数据库时,/application/mysql/my.cnf被创建
    New default config file was created as /application/mysql//my.cnf and
    will be used by default by the server when you start it.
    You may edit this file to change server settings
    #检测到/etc/my.cnf已经存在,如果想指定启动mysql的配置文件,请用--defaults-file 参数
    WARNING: Default config file /etc/my.cnf exists on the system
    This file will be read by default by the MySQL server
    If you do not want to use this, either remove it, or use the
    --defaults-file argument to mysqld_safe when starting the server
    [root@db01 support-files]# cd ..
    [root@db01 mysql]# cd ..
    [root@db01 application]# 

    #注意在你初始化数据库的后,看是否自动创建/application/mysql-5.6.36/my.cnf这个文件,如果没有,请将/application/mysql-5.6.36/support-files/my*.cnf复制到/etc/my.cnf,否则会报错。

  3. 创建关键目录并设置权限

    [root@db01 mysql]# mkdir -p /application/mysql/tmp
    [root@db01 mysql]# chown -R mysql.mysql /application/mysql/tmp

     

  4. 复制启动脚本到/etc/init.d/mysqld

    [root@db01 mysql]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld

     

  5. 启动数据库(默认监听3306端口)

    [root@db01 mysql]# /etc/init.d/mysqld start
    Starting MySQL SUCCESS! 
    [root@db01 mysql]# netstat -lntup|grep 3306
    tcp        0      0 :::3306                     :::*                        LISTEN      20736/mysqld        
    [root@db01 mysql]# 

     

  6. 根据步骤3中的提示设置数据库密码(默认密码为空)

    [root@db01 mysql]#/application/mysql/bin/mysqladmin -u root password '123456'

    修改密码: 

    [root@db01 ~]# mysqladmin -uroot -p password 123123
    Enter password:    #原密码
    Warning: Using a password on the command line interface can be insecure.
    [root@db01 ~]# 

     

  7. 连接数据库

    [root@db01 mysql]# mysql -uroot -p123456
    Warning: Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.6.36 Source distribution
    
    Copyright (c) 2000, 2017, 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> select user,host,password from mysql.user;
    +------+-----------+-------------------------------------------+
    | user | host      | password                                  |
    +------+-----------+-------------------------------------------+
    | root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
    | root | db01      |                                           |
    | root | 127.0.0.1 |                                           |
    | root | ::1       |                                           |
    |      | localhost |                                           |
    |      | db01      |                                           |
    +------+-----------+-------------------------------------------+
    6 rows in set (0.04 sec)
    
    mysql> 
    


     

平时自己跟着老师一起安装mysql倒觉得还是比较简单,真正自己去研究发现,里面还是很多坑的,所以还得自己多实践,多碰壁,才能成长,今天安装mysql花了些时间还是值得的。

最后修改于 2019-07-31 14:38:28
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇