下载二进制格式的mysql软件包
[root@lzh ~]# cd /usr/src/
[root@lzh src]# wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
此处下载过程省略...
[root@lzh src]# ls //可提前下载好传入
debug kernels mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
创建用户和组
[root@lzh src]# groupadd -r mysql
[root@lzh src]# useradd -M -s /sbin/nologin -g mysql mysql
解压软件至 /usr/local
[root@lzh src]# tar xf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
为mysql目录创建软连接
[root@lzh src]# cd /usr/local/
[root@lzh local]# ln -sv mysql-5.7.25-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.25-linux-glibc2.12-x86_64/"
修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql
[root@localhost local]# ll -d /usr/local/mysql
lrwxrwxrwx. 1 mysql mysql 36 5月 8 13:59 /usr/local/mysql -> mysql-5.7.25-linux-glibc2.12-x86_64/
添加环境变量
[root@lzh ~]# ls /usr/local/mysql
bin COPYING docs include lib man README share support-files
[root@lzh ~]# /usr/local/mysql/bin/mysql start //如果不做环境变量则这样启动
[root@lzh ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@lzh ~]# source /etc/profile.d/mysql.sh
[root@lzh ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
建立数据存放目录
[root@lzh ~]# mkdir /opt/data
[root@lzh ~]# chown -R mysql.mysql /opt/data/
[root@lzh ~]# ll /opt/data/ -d
drwxr-xr-x. 2 mysql mysql 6 5月 7 13:27 /opt/data/
初始化数据库
[root@lzh ~]# mysqld --initialize --user=mysql --datadir=/opt/data/
2020-05-07T05:37:34.318784Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-05-07T05:37:34.956022Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-05-07T05:37:34.996118Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-05-07T05:37:35.056392Z 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: dab253f4-9024-11ea-985a-000c291a328e.
2020-05-07T05:37:35.057253Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-05-07T05:37:35.058240Z 1 [Note] A temporary password is generated for root@localhost: bXznl/uHd47t
//请注意,这个命令生成的临时密码为 bXznl/uHd47t
//此密码为随机密码,你的密码与我的不一样 ,记住这个随机密码,登录时会用到
生成配置文件
[root@lzh ~]# cat > /etc/my.cnf <<EOF
> [mysqld] //描述信息
> basedir = /usr/local/mysql //解压后的mysql默认路径
> datadir = /opt/data //数据存放目录
> socket = /tmp/mysql.sock //套接字,一般存放到临时目录
> port = 3306
> pid-file = /opt/data/mysql.pid //记录当前mysqld进程的 pid
> user = mysql
> skip-name-resolve //登录时跳过域名解析
> EOF
配置服务启动脚本
[root@lzh ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lzh ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@lzh ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
启动MySQL
[root@lzh ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/opt/data/lzh.err'.
. SUCCESS!
[root@lzh ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
使用临时密码登录
修改密码
[root@lzh ~]# mysql -uroot -p'bXznl/uHd47t'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25
Copyright (c) 2000, 2019, 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> set password = password('123.com');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
添加隐藏文件,不输入账号密码直接登录
[root@reserve ~]# vim .my.cnf
[client]
user=root
password=123.com
[root@reserve ~]# ls -a
. anaconda-ks.cfg .bash_logout .bashrc .my.cnf .tcshrc
.. .bash_history .bash_profile .cshrc .mysql_history .viminfo
[root@reserve ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.25 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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> exit
Bye