centos7 安装mysql 5.7.31
一、卸载原有mysql / mariadb
卸载系统中初始安装的mysql或者mariadb,如有,卸载:
rpm -qa | grep mysql
rpm -qa | grep mariadb
find / -name mysql //找到后删除,同mariadb
find / -name mariadb
类似 mysql-libs-5.1.52-1.el6_0.1.x86_64
选择进行卸载: rpm -e mysql-libs-5.1.52-1.el6_0.1.x86_64 // 普通删除模式
rpm -e mysql-libs-5.1.52-1.el6_0.1.x86_64 --nodeps // 忽略依赖删除
检查否存在 mariadb 数据库,
类似 mariadb-libs-5.5.56-2.el7.x86_64
卸载 rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
二、帐号准备
检查mysql组和用户是否存在,如无创建
cat /etc/group | grep mysql
类似
mysql:x:27:
cat /etc/passwd | grep mysql
mysql:x:27:27::/home/mysql:/sbin/nologin
如无,添加:
groupadd mysql
useradd -r -g mysql mysql
useradd -r参数表示mysql用户是系统用户,不可用于登录系统
三、安装
从官方或开源镜像站获取(如清华、阿里、网易等开源镜像)tar.gz解压,
tar zxvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz或者 mysql-5.7.31-el7-x86_64.tar.gz
改名并移动至/ssd目录下
/ssd/mysql/support-files目录下创建my_default.cnf
编辑内容:
vim my_default.cnf
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
basedir = /ssd/mysql
datadir = /ssd/mysql/data
port = 3306
socket = /tmp/mysql.sock
character-set-server=utf8
log-error = /ssd/mysql/data/mysqld.log
pid-file = /ssd/mysql/data/mysqld.pid
四、初始化数据库:
mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/ssd/mysql/ --datadir=/ssd/mysql/data/
mysqld: Can't change dir to '/ssd/mysql/data/' (Errcode: 13 - Permission denied)
2020-09-08T08:45:36.736413Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-09-08T08:45:36.741031Z 0 [ERROR] failed to set datadir to /ssd/mysql/data/
2020-09-08T08:45:36.741068Z 0 [ERROR] Aborting
/ssd为追加硬盘,mount方式是/dev下设备名,未使用uuid之类
改到/opt目录下执行则成功,其他未做任何修改
mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/opt/mysql/ --datadir=/opt/mysql/data/
2020-09-08T09:18:46.807926Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-09-08T09:18:47.105305Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-09-08T09:18:47.162378Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-09-08T09:18:47.220091Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 4cbeca18-f1b4-11ea-8389-90e2ba0620e8.
2020-09-08T09:18:47.221088Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-09-08T09:18:48.197576Z 0 [Warning] CA certificate ca.pem is self signed.
2020-09-08T09:18:48.881761Z 1 [Note] A temporary password is generated for root@localhost: wi&bekMKY7y<
排查再上一级目录,ssd 权限700,
drwx------ 5 root root 169 9月 8 17:57 ssd
修改为755后将mysql移回/ssd下,通过正常初始化
mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/ssd/mysql/ --datadir=/ssd/mysql/data/
2020-09-08T10:02:51.878496Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-09-08T10:02:52.128342Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-09-08T10:02:52.173952Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-09-08T10:02:52.230032Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 754b46f0-f1ba-11ea-b375-90e2ba0620e8.
2020-09-08T10:02:52.230822Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-09-08T10:02:53.004203Z 0 [Warning] CA certificate ca.pem is self signed.
2020-09-08T10:02:53.427709Z 1 [Note] A temporary password is generated for root@localhost: oyRlfJoj)2e8
五、启动数据库,配置帐号及防火墙
复制有关的启动及服务配置文件:
# cp support-files/my_default.cnf /etc/my.cnf
# cp support-files/mysql.server /etc/init.d/mysql
# systemctl start mysqld
Failed to start mysqld.service: Unit not found.
# service mysql start
Starting MySQL.Logging to '/ssd/mysql/data/mysqld.log'.
[ 确定 ]
#
查看mysql服务是否在服务配置中
chkconfig --list mysql
若没有,则把mysql注册为开机启动的服务,然后在进行查看
chkconfig --add mysql
chkconfig --list mysql
修改密码,初始化时提示有密码,也可在log文件中查找。
# ./bin/mysql -u root -p
Enter password: //输入初始化密码oyRlfJoj)2e8
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31
Copyright (c) 2000, 2020, 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('新密码');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
或者:
mysql> update user set authentication_string=password('新密码') where user='root';
mysql> flush privileges;
添加远程访问权限
mysql> use mysql;
mysql> update user set host='%' where user = 'root';
mysql> flush privileges;
mysql> exit;
添加防火墙
firewall-cmd --new-service=mysql --permanent
firewall-cmd --service=mysql --add-port=3306/tcp --permanent
firewall-cmd --service=mysql --set-description='This is a mysql service !' --permanent
firewall-cmd --service=mysql --set-short=mysql --permanent
firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" source ipset="admiplist" service name=mysql accept'
firewall-cmd --reload
浙公网安备 33010602011771号