centos7安装mysql5.6

CentOS7上安装mysql5.6

(最好切换到root下操作!)

  1. CentOS7将默认数据库由mysql替换为Mariadb,因此需要先卸载Mariadb。方法为:
[root@gerrit+gitlab ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@gerrit+gitlab ~]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
  1. 删除my.conf:(如果存在的话)
[root@gerrit+gitlab ~]# rm /etc/my.cnf
  1. 创建mysql用户组:
[root@gerrit+gitlab ~]# groupadd mysql
  1. 创建mysql用户并加入用户组:
[root@gerrit+gitlab ~]# useradd -g mysql mysql
  1. 下载安装包:

从 https://dev.mysql.com/downloads/mysql/5.6.html#downloads下载mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz,适配操作系统选择Linux - Generic (glibc 2.12) (x86, 64-bit), Compressed TAR Archive版本的。

拷贝到/usr/local目录并解压:

[root@gerrit+gitlab local]# tar -zxvf mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz

然后重命名文件夹为mysql:

[root@gerrit+gitlab local]# mv mysql-5.6.42-linux-glibc2.12-x86_64 mysql
  1. /etc目录下新建my.cnf(或者从/usr/local/mysql/support-files将my-default.cnf拷贝过来并改名为my.cnf),内容修改为:
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock
[mysqld]
skip-name-resolve
#设置3306端口
port = 3306
socket=/var/lib/mysql/mysql.sock
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_name=1
max_allowed_packet=16M
  1. 安装:
[root@gerrit+gitlab ~]# cd /usr/local/mysql/
[root@gerrit+gitlab mysql]# chown -R mysql:mysql ./
[root@gerrit+gitlab mysql]# mkdir data
[root@gerrit+gitlab mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/

如果提示:FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:

Data::Dumper

那就:

[root@gerrit+gitlab mysql]# yum install autoconf -y

然后重新执行前一句安装命令即可。

完成后修改权限:

 

[root@gerrit+gitlab mysql]# chown -R mysql:mysql data
  1. 配置:

授予my.cnf最大权限:

[root@gerrit+gitlab ~]# chmod 777 /etc/my.cnf

设置开机自启动:

[root@gerrit+gitlab mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld

并增加权限:

[root@gerrit+gitlab mysql]# chmod +x /etc/rc.d/init.d/mysqld

将mysqld加入到系统服务:

[root@gerrit+gitlab mysql]# chkconfig --add mysqld

检查一下:

[root@gerrit+gitlab mysql]# chkconfig --list mysqld
      

Note: This output shows SysV services only and does not include native
 systemd services. SysV configuration data might be overridden by native
 systemd configuration.
      

 If you want to list systemd services use 'systemctl list-unit-files'.
 To see services enabled on particular target use
 'systemctl list-dependencies [target]'.
      

mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
  1. 启动:
[root@gerrit+gitlab mysql]# service mysqld start
Warning: World-writable config file '/etc/my.cnf' is ignored
Starting MySQL.Warning: World-writable config file '/etc/my.cnf' is ignored
Warning: World-writable config file '/etc/my.cnf' is ignored
Logging to '/usr/local/mysql/data/gerrit+gitlab.err'.
. SUCCESS!

上面的Warning提示是说my.cnf全局可写,不安全,那就修改一下:

[root@gerrit+gitlab mysql]# chmod 644 /etc/my.cnf

现在可以再试试service mysqld stop和service mysqld start了:

[root@gerrit+gitlab mysql]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[root@gerrit+gitlab mysql]# service mysqld start
Starting MySQL.190105 21:21:32 mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
 ERROR! The server quit without updating PID file (/usr/local/mysql/data/gerrit+gitlab.pid).

出现ERROR,解决方法如下:

[root@gerrit+gitlab mysql]# mkdir -p /var/run/mysqld
[root@gerrit+gitlab mysql]# chown mysql:mysql /var/run/mysqld/
[root@gerrit+gitlab mysql]# mv /etc/my.cnf /etc/my.cnf.bk
[root@gerrit+gitlab mysql]# service mysqld start
Starting MySQL. SUCCESS!

编辑~/.bash_profile,最后面增加:

export PATH=$PATH:/usr/local/mysql/bin

并立即生效:

[root@gerrit+gitlab mysql]# source ~/.bash_profile

现在就登录并设置root密码(初始密码为空,直接回车)和远程访问用户名密码吧:

[root@gerrit+gitlab mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.42 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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>

设置root的密码为root:

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 password=password('root') where user='root' and host='localhost';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>

设置远程访问用户名为zjd,密码为zjdpass:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'zjd'@'%' IDENTIFIED BY 'zjdpass' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

mysql>

      

posted @ 2019-01-06 11:02  鸟瞰的鸟  阅读(366)  评论(0编辑  收藏  举报