centos二进制安装mysql8.0(centos6和7都可以按此方法装,只是启动命令不同)
首先检查是否已经安装过mysql:
[root@localhost ~]# rpm -qa | grep mysql
[root@localhost ~]# rpm -e --nodeps
1.#定义数据库安装目录
[root@localhost local]#mkdir -p /usr/local/mysql/data
[root@localhost local]#mysql_install=/usr/local/mysql
[root@localhost local]#mysql_db=/usr/local/mysql/data
2.#创建用户和组
[root@localhost local]#groupadd mysql
[root@localhost local]#useradd -r -g mysql mysql
3.#目录授权
[root@localhost local]#chown -R mysql:mysql /usr/local/mysql/data
4安装依赖包
[root@localhost local]#yum -y install wget cmake gcc gcc-c++ ncurses ncurses-devel libaio-devel openssl openssl-devel
[root@localhost local]#wget https://dev.mysql.com//Downloads/MySQL-8.0/mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz
[root@localhost local]]# tar -xvf mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz
[root@localhost local]]# cp -rf mysql-8.0.11-linux-glibc2.12-x86_64/* mysql/
5初始化
[root@localhost mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --pid-file=/usr/local/mysql/mysql.pid
#记录mysql的初始密码H*my,%DYf5lU
6环境变量
[root@localhost mysql]#vim /etc/profile
export MYSQL_HOME=/usr/local/mysql
export PATH= P A T H : PATH: PATH:MYSQL_HOME/bin
[root@localhost mysql]#source /etc/profile
7配置文件
[root@localhost mysql]#vi /etc/my.cnf
[mysqld]
port=3306
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/log/mysql-err.log
user=root
pid-file = /usr/local/mysql/mysql.pid
default_authentication_plugin=mysql_native_password
[client]
socket=/tmp/mysql.sock
[root@localhost mysql]# mkdir /usr/local/mysql/log/
8启动文件
[root@localhost mysql]#cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# vi /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
[root@localhost local]# chmod 755 /etc/init.d/mysqld
[root@localhost mysql]# /etc/init.d/mysql start
注意:centos7系统 cp $mysql_install/support-files/mysql.server /etc/systemd/system/mysql.service
启动:/etc/systemd/system/mysql.service start
*若忘记记录mysql密码,修改mysql密码
[root@localhost local]]#vim /etc/my.cnf #添加这行
skip-grant-tables
[root@localhost local]]#/etc/systemd/system/mysql.service restart 重启
[root@localhost local]]# mysql -u root
mysql> flush privileges;
mysql> ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘MAbd17d4!2’
去掉my.cnf配置,重启mysql*
9.权限配置
[root@localhost local]]# mysql -u root -p
mysql> ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘MAbd17d4!2’;(密码需要大小写英文+数字+字符)
mysql> use mysql
mysql>update user set host = “%” where user=‘root’;
mysql>FLUSH PRIVILEGES; #刷新权限
mysql>SHOW VARIABLES LIKE ‘character_set_%’; 查看字符集
10.防火墙配置(centos6):
[root@localhost ~]# /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
[root@localhost ~]# /etc/rc.d/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[root@localhost ~]# /etc/rc.d/init.d/iptables status
centos7防火墙配置
[root@localhost ~]# service firewalld start
[root@localhost ~]# firewall-cmd --permanent --add-port=3306/tcp
[root@localhost ~]# firewall-cmd --permanent --add-rich-rule ‘rule family=ipv4 source address=ip/2 port port=3306 protocol=tcp accept’
[root@localhost ~]# firewall-cmd --reload