1、系统软件环境
系统信息:
[root@centos7 ~]# cat /proc/version
Linux version 3.10.0-862.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Fri Apr 20 16:44:24 UTC 2018
mysql版本:mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz(下载地址)
2、上传MySQL二进制包并解压
[root@centos7 mysql]# ls
mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
[root@centos7 mysql]# tar -xvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
3、创建/usr/local/mysql并转移二进制文件
[root@centos7 mysql]# mkdir /usr/local/mysql
[root@centos7 mysql]# mv mysql-5.7.28-linux-glibc2.12-x86_64/* /usr/local/mysql
[root@centos7 mysql]# cd /usr/local/mysql/
[root@centos7 mysql]# ll
总用量 292
drwxr-xr-x. 2 root root 4096 10月 14 17:37 bin
drwxr-xr-x. 2 root root 55 10月 14 17:37 docs
drwxr-xr-x. 3 root root 4096 10月 14 17:37 include
drwxr-xr-x. 5 root root 230 10月 14 17:37 lib
-rw-r--r--. 1 7161 31415 279547 9月 27 15:00 LICENSE
drwxr-xr-x. 4 root root 30 10月 14 17:37 man
-rw-r--r--. 1 7161 31415 587 9月 27 15:00 README
drwxr-xr-x. 28 root root 4096 10月 14 17:37 share
drwxr-xr-x. 2 root root 90 10月 14 17:37 support-files
4、创建mysql用户以及data目录
[root@centos7 mysql]# useradd -s /sbin/nologin -M mysql
[root@centos7 mysql]# mkdir data
[root@centos7 mysql]# pwd
/usr/local/mysql
[root@centos7 mysql]# chown -R mysql:mysql ./
[root@centos7 mysql]# ll
总用量 292
drwxr-xr-x. 2 mysql mysql 4096 10月 14 17:37 bin
drwxr-xr-x. 2 mysql mysql 6 10月 14 17:42 data
drwxr-xr-x. 2 mysql mysql 55 10月 14 17:37 docs
drwxr-xr-x. 3 mysql mysql 4096 10月 14 17:37 include
drwxr-xr-x. 5 mysql mysql 230 10月 14 17:37 lib
-rw-r--r--. 1 mysql mysql 279547 9月 27 15:00 LICENSE
drwxr-xr-x. 4 mysql mysql 30 10月 14 17:37 man
-rw-r--r--. 1 mysql mysql 587 9月 27 15:00 README
drwxr-xr-x. 28 mysql mysql 4096 10月 14 17:37 share
drwxr-xr-x. 2 mysql mysql 90 10月 14 17:37 support-files
useradd参数说明:-s /sbin/nologin(不允许登录),-M(不创建主目录)
5、接下来初始化数据库安装
[root@centos7 mysql]# cd /usr/local/mysql/bin/
[root@centos7 bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2019-10-14T09:44:56.563354Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-10-14T09:44:57.070733Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-10-14T09:44:57.275325Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-10-14T09:44:57.390688Z 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: 48524e06-ee67-11e9-b446-080027a2aac6.
2019-10-14T09:44:57.407672Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-10-14T09:44:59.129046Z 0 [Warning] CA certificate ca.pem is self signed.
2019-10-14T09:44:59.338406Z 1 [Note] A temporary password is generated for root@localhost: FiFOMii4ts+V
初始化的密码:FiFOMii4ts+V,看打印的日志最后的日志
6、设置mysql服务开机自启动
[root@centos7 mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@centos7 mysql]# chmod +x /etc/init.d/mysqld
[root@centos7 mysql]# chkconfig --add mysqld
[root@centos7 mysql]# chkconfig --list mysqld
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@centos7 mysql]# service mysqld status
ERROR! MySQL is not running
[root@centos7 mysql]# service mysqld start
Starting MySQL.2019-10-14T09:48:40.824879Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
ERROR! The server quit without updating PID file (/var/lib/mysql/centos7.pid).
通过打印的日志,可以看出是缺少日志文件和pid文件,通常这些文件是在my.cnf文件配置,添加my.cnf文件
7、修改/etc/my.cnf并创建/var/run/mysqld目录,并重启MySQL
[root@centos7 mysql]# vi /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
~
"/etc/my.cnf" 22L, 579C written
[root@centos7 mysql]# mkdir /var/lib/mysql
[root@centos7 mysql]# chown -R mysql:mysql /var/lib/mysql
[root@centos7 mysql]# service mysqld status
ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql) exists
[root@centos7 mysql]# service mysqld start
Starting MySQL. SUCCESS!
8、环境变量配置,将mysql的bin目录加入PATH环境变量,
[root@centos7 mysql]# vi /etc/profile
# /etc/profile
.....
unset i
unset -f pathmunge
PATH=$PATH:/usr/local/mysql/bin
export PATH
执行命令使其生效
[root@centos7 mysql]# source /etc/profile
[root@centos7 mysql]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
9、常见问题处理
1、
[root@centos7 mysql]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
解决方案:
[root@centos7 mysql]# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
2、忘记密码
my.cnf中[mysqld]下面加skip-grant-tables,重启MySQL,修改密码,删除my.cnf中[mysqld]下面skip-grant-tables,重启MySQL
[root@centos7 mysql]# mysql
mysql> use mysql;
mysql> update user set authentication_string=PASSWORD('test@123456') where user='root';
mysql> flush privileges;
mysql> exit
[root@centos7 mysql]# service mysql restart
3、给root授予在任意主机(%)访问任意数据库的所有权限
mysql> grant all privileges on *.* to 'root'@'%' identified by 'admin@123' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)