Centos 7安装Mysql5.7

    xiaoxiao2021-03-25  132

    Centos 7 安装mysql

    1.下载mysql

    mysql镜像站:mirrors.sohu.com/mysql,在镜像站中下载所需的版本

    也可以到官网进行下载:

    也可以在Linux上直接下载:

    下载命令,如:wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz

    2.卸载旧版本

    如果新的部署环境,是不会存在mysql的,所以可以忽略;如果已经安装过mysql的系统环境,则需要卸载以前的mysql。过程中的路径以实际情况为准。

    1. 停止服务

    /etc/init.d/mysqld stop

    2.停止并删除开机服务

    chkconfig mysqld off chkconfig --del mysqld

    3.删除MySQL目录

    rm -rf /usr/local/mysql rm -f /etc/my.cnf rm -f /etc/init.d/mysqld rm -f /root/.mysql_secret

    4.删除用户和用户组

    userdel -r mysql

    3.安装

    1.检查文件是否存在,如有则删除

    [root@slave ~]# rpm -qa | grep mysql mysql-libs-5.1.71-1.el6.x86_64 [root@slave ~]# rpm -e mysql-libs-5.1.71-1.el6.x86_64 --nodeps [root@slave ~]# rpm -qa | grep mysql

    2.检查mysql组和用户是否存在,如无创建。

    [root@cmwap /]# cat /etc/group | grep mysql [root@cmwap /]# cat /etc/passwd | grep mysql //useradd -r参数表示mysql用户是系统用户,不可用于登录系统。 [root@cmwap /]# groupadd mysql [root@cmwap /]# useradd -r -g mysql mysql

    3.解压tar包,重命名并复制到/usr/local目录下

    [root@cmwap yangbo]# tar -zvxf mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz [root@cmwap yangbo]# mv mysql-5.7.16-linux-glibc2.5-x86_64 mysql [root@cmwap yangbo]# cp -r mysql /usr/local

    4.切换到/usr/local下,并修改mysql目录的所属组和用户

    cd /usr/local [root@cmwap local]# chown -R mysql:mysql mysql/ 结果:drwxr-xr-x 9 mysql mysql 4096 3月 8 13:46 mysql

    4.安装和初始化数据库

    1.配置OS

    [root@cmwap local]# cd mysql/ [root@cmwap mysql]# vim /etc/security/limits.conf 在limits.conf中添加如下两条: mysql hard nofile 65535 mysql soft nofile 65535

    2.安装数据库

    [root@cmwap mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ 2017-03-08 14:09:40 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize 2017-03-08 14:09:57 [WARNING] The bootstrap log isn't empty: 2017-03-08 14:09:57 [WARNING] 2017-03-08T06:09:41.312624Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead 2017-03-08T06:09:41.322502Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000) 2017-03-08T06:09:41.322519Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000) //复制配置文件到/etc/my.cnf,因为我们一般不改变mysql的配置,而是将配置复制,避免破坏mysql [root@cmwap mysql]# cp -a ./support-files/my-default.cnf /etc/my.cnf [root@cmwap mysql]# cp -a ./support-files/mysql.server /etc/init.d/mysqld [root@cmwap mysql]# cd bin //开启数据库安全服务 [root@cmwap bin]# ./mysqld_safe --user=mysql & [1] 4098 [root@cmwap bin]# 2017-03-08T06:24:45.521077Z mysqld_safe Logging to '/usr/local/mysql/data/cmwap.cqmc.com.err'. 2017-03-08T06:24:45.620617Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data //重启mysql [root@cmwap bin]# /etc/init.d/mysqld Usage: mysqld {start|stop|restart|reload|force-reload|status} [ MySQL server options ] [root@cmwap bin]# /etc/init.d/mysqld restart Shutting down MySQL..2017-03-08T06:27:15.757702Z mysqld_safe mysqld from pid file /usr/local/mysql/data/ cmwap.cqmc.com.pid ended SUCCESS! Starting MySQL. SUCCESS! [1]+ 完成 ./mysqld_safe --user=mysql //设置开机启动 [root@cmwap bin]# chkconfig --level 35 mysqld on

    3.初始化密码

    //获得系统初始化密码 [root@cmwap bin]# cat /root/.mysql_secret # Password set for user 'root@localhost' at 2017-03-08 14:09:40 n4eas8Leinln //进入mysql [root@cmwap bin]# ./mysql -uroot -p Enter password: //密码就是系统的初始化密码,如上面的n4eas8Leinln Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.16 Copyright (c) 2000, 2016, 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> //设置密码 mysql> set password = password('123456'); Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)

    4.添加远程访问权限

    在centos7中,因为有防火墙的存在,特定的端口不能被外界访问,所以我们需要将mysql的端口开放出去,这样才能让远程主机访问。centos7默认的防火墙为firewall,如果不习惯可以将其屏蔽,转而使用iptables作为防火墙,具体请参见这篇博文,对于firewall防火墙,开放端口如下:

    //开放端口 [root@cmwap bin]# firewall-cmd --zone=public --add-port=3306/tcp --permanent //重启防火墙 [root@cmwap bin]# firewall-cmd --reload

    在完成端口的开放之后,就可以进行远程主机访问权限的配置了,配置如下:

    //切换数据库到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> select host,user from user; +-----------+-----------+ | host | user | +-----------+-----------+ | % | root | | localhost | mysql.sys | +-----------+-----------+ 2 rows in set (0.00 sec) //重启生效 [root@cmwap bin]# /etc/init.d/mysqld restart

    5.修改字符编码及端口号

    //在配置文件中添加 [root@cmwap bin]# vim /etc/my.cnf character-set-server=utf8 port=XXXX //重启生效 [root@cmwap bin]# /etc/init.d/mysqld restart

    5.将mysql加入环境变量(可无)

    //进入配置文件 [root@cmwap bin]# vim /etc/profile //在配置文件中添加 MYSQL_HOME=/usr/local/mysql export MYSQL_HOME export PATH=$MYSQL_HOME/bin:$PATH //更新配置 [root@cmwap bin]# source /etc/profile

    经过上面的配置之后,就可以直接使用mysql提供的命令了,如:

    [root@cmwap bin]# mysql -uroot -p

    6.安装总结

    以上是mysql5.7在Centos 7上面的安装步骤,只要严格地按照要求来执行,应该不会有问题。

           

    作者:潼城旧事

    时间:2017/03/08

    转载请注明原文地址: https://ju.6miu.com/read-3132.html

    最新回复(0)