MySql教程:CentOS7安装mysql5.6

  1. MySQL官方网站下载:
    https://downloads.mysql.com/archives/community/
  2. 解压文件:
    MySQL压缩文件放到你想要解压的服务器目录: /home/software/
    tar -zxvf mysql-5.6.45-linux-glibc2.12-x86_64.tar.gz
  3. 准备文件夹:
    把解压的MySQL文件夹软连接到: /usr/local/mysql
    ln -sv /home/software/mysql-5.6.45-linux-glibc2.12-x86_64/ /usr/local/mysql
    特别提醒: 请使用完整路径,否则软连接会异常,报红色警告,找不到文件夹。 创建MySQL数据文件夹:
    mkdir /home/data/mysql/data -p
  4. 新建mysql用户组和mysql用户:
    groupadd mysql useradd -g mysql -r -s /sbin/nologin -M -d /home/data/mysql/data mysql
    可以使用查看命令确认: groups mysql
  5. 文件夹赋权限:
    chown -R mysql:mysql /usr/local/mysql
    chown -R mysql:mysql /home/data/mysql/data
    可以使用命令确认文件夹是否已经更改了所属用户:
    drwxr-xr-x. 2 mysql mysql 6 4月 17 17:31 mysql
  6. 初始化mysqld(与5.7的区别):
    ./scripts/mysql_install_db –user=mysql –basedir=/usr/local/mysql –datadir=/home/data/mysql/data
    操作示例:
    [root@localhost data]# cd /usr/local/mysql/ [root@localhost mysql]# ./scripts/mysql_install_db –user=mysql –basedir=/usr/local/mysql –datadir=/data/mysql
  7. 编辑配置文件及将mysql的服务脚本放到系统服务中,并配置环境变量让系统可以直接使用mysql的相关命令
    [root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@localhost mysql]# ldconfig [root@localhost mysql]# echo “PATH=$PATH:/usr/local/mysql/bin” > /etc/profile.d/mysql.sh [root@localhost mysql]# source /etc/profile.d/mysql.sh [root@localhost mysql]# chkconfig mysqld on
  8. 修改MySQL配置文件: /etc/my.cnf
    [mysqld] basedir=/usr/local/mysql datadir=/home/software/data/mysql socket=/tmp/mysql.sock user=mysql server_id=1 port=3306
  9. 启动MySQL服务:
    service mysqld start
    运行效果:
    [root@localhost mysql]# service mysqld start Starting MySQL.Logging to ‘/home/software/data/mysql/localhost.localdomain.err’. . SUCCESS!
    特别提示: mysqld 是MySQL服务端, mysql是MySQL客户端
  10. 登录MySQL并修改初始密码:
    [root@localhost mysql]# mysql mysql> GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY ‘123456’ WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
  11. 修改MySQL连接服务器限制:
    mysql> use mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> update user set host=’%’ where user=’root’; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
    特别提示: 注意服务器防火墙对端口3306的限制,可能会导致远程连接不上。 安装异常:
    参考: https://blog.51cto.com/u_15073468/2793326 报错如下: -bash: ./scripts/mysql_install_db: /usr/bin/perl: bad interpreter: No such file or directory
    分析原因:缺 perl perl-devel包。
    解决办法: yum install -y perl perl-devel
    安装后执行mysql初始化命令可能还会继续报错: FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db: Data::Dumper
    解决办法:安装autoconf库 yum -y install autoconf