mysql5.7的常规安装启动和找回密码的方法
1:将压缩包上传至自定义的/server/tools目录中并解压,
[root@tyjs09 ~]#mkdir /server/tools -p
[root@tyjs09 ~]#cd /server/tools
[root@tyjs09 tools]#wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
然后将解压目录重命名后移动到自定义目录/application中
删除mariadb库,添加一个用户,并在文件etc/profile中设置mysql的环境变,最后使用mysql -V测试版本:
export PATH==/application/mysql/bin:$PATH
[root@instance-r5y0pf5d /server/tools]# ls mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz [root@instance-r5y0pf5d /server/tools]# tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz [root@instance-r5y0pf5d /server/tools]# ls mysql-5.7.26-linux-glibc2.12-x86_64 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz [root@instance-r5y0pf5d /server/tools]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /application/mysql
[root@instance-r5y0pf5d /application]# rpm -qa|grep mariadb
[root@instance-r5y0pf5d /application]# useradd -s /sbin/nologin mysql
[root@instance-r5y0pf5d /application]# vim /etc/profile
[root@instance-r5y0pf5d /application]# source /etc/profile
[root@instance-r5y0pf5d /application]# mysql -V
2如果mysql -V查看版本报错说明没有安装成功,缺少依赖,请完成下面的依赖安装
yum install -y libaio-devel
yum install glibc.i686
yum install libgcc.i686
yum install libncurses*
yum install libncurses.so.5
3创建授权目录,初始化mysql
实际工作中用于存放数据时可能会单独设立一块盘,例如:
添加一块sdb新磁盘并对这个磁盘进行格式化,然后将磁盘挂载到存放数据的data目录
查看磁盘状况:fdisk -l
格式化磁盘:mkfs.xfs /dev/sdb
创建data目录:mkdir /data
查看磁盘的UUID号:blkid
将磁盘挂载到data目录:
1)设置开机自动挂载:vim /etc/fstab
UUID=”UUID="8495ffghj-4dbd-784-aaa78" TYPE="ext9"” /data xfs default 0 0
2)重新加载/etc/fstab文件:mount -a
3)reboot重启后查看挂载情况:df -h
没有磁盘可挂载,就直接创建存放数据的文件夹即可!
[root@创建目录 ~]# mkdir -p /data/mysql/data [root@对软件目录授权 ~]# chown -R mysql.mysql /application/* [root@对数据目录授权d ~]# chown -R mysql.mysql /data [root@对日志目录授权 ~]# chown -R mysql:mysql /var/log [root@初始化mysql ~]# /application/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data [root@编写配置文件 ~]# cat >/etc/my.cnf <<EOF > [mysqld] > user=mysql > basedir=/application/mysql > datadir=/data/mysql/data > socket=/tmp/mysql.sock > server_id=6 > port=3306 > [mysql] > socket=/tmp/mysql.sock > EOF
[mysqld]
user=mysql
skip-grant-tables
basedir=/application/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=6
port=3306
log_error=/tmp/mysql.err
log_warnings=9
[mysql]
socket=/tmp/mysql.sock
[root@配置启动文件 ~]# cat > /etc/systemd/system/mysqld.service <<EOF > [Unit] > Description=Mysql Server > Documentation=man:mysqld(8) > Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html > After=network.target > After=syslog.target > [Install] > WantedBy=multi-user.target > [Service] > User=mysql > Group=mysql > ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf > LimitNOFILE = 5000 > EOF [root@启动mysql ~]# systemctl start mysqld [root@进入mysql ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.26 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>
备注启动数据库的方法:
启动数据库:
方法1去目录里执行启动
cd /application/mysql/support-files/
./mysql.server start
方法2将目录文件拷贝到/etc/init.d目录里
cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
service mysqld start或/etc/init.d/mysqld start
方法3
1)定制一个启动脚本
cat > /etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=Mysql Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
EOF
2)启动:systemctl start mysqld
备注判断数据库是否启动的方法:
判断数据库是否启动:
1)netstat -lnp|grep mysqld
2)netstat -lnp|grep 3306
3)ps -ef|grep mysqld
4)Systemctl status mysqld
5)ss -tulpn|grep mysqld
6)Ss -tulpn|grep 3306
7)lsof -i :3306
8)mysql
4设置mysql密码
[root@instance-r5y0pf5d ~]# mysqladmin -uroot -p password 123456
-bash: mysqladmin: command not found
[root@instance-r5y0pf5d ~]# ln -s /application/mysql/bin/mysql /usr/bin
ln: failed to create symbolic link ‘/usr/bin/mysql’: File exists
[root@给命令设置软连接 ~]# ln -s /application/mysql/bin/mysqladmin /usr/bin
[root@instance-r5y0pf5d ~]# mysqladmin -uroot -p password 123456
Enter password:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'
[root@设置密码 ~]# mysqladmin -uroot -p password 123456
Enter password:
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@登陆 ~]# mysql -uroot -p123456
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 8
Server version: 5.7.26 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>
5找回管理员密码:
1)先关闭数据库,用什么方法启动的数据库就用什么方法关闭,例如:
/etc/init.d/mysqld stop
systemctl stop mysqld
[root@查看运行状态 ~]# ps -ef |grep mysqld
mysql 594 1 0 Jan03 ? 00:00:22 /application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
root 31129 31067 0 09:46 pts/0 00:00:00 grep --color=auto mysqld
[root@停止数据库 ~]# systemctl stop mysqld
[root@instance-r5y0pf5d ~]# ps -ef |grep mysqld
root 31177 31067 0 09:47 pts/0 00:00:00 grep --color=auto mysqld
[root@instance-r5y0pf5d ~]#
2)启动数据库到维护模式
mysqld_safe --skip-grant-tables --skip-networking &
[root@instance-r5y0pf5d ~]# ps -ef |grep mysqld
root 31177 31067 0 09:47 pts/0 00:00:00 grep --color=auto mysqld
[root@启动维护模式 ~]# mysqld_safe --skip-grant-tables --skip-networking &
[1] 31913
[root@instance-r5y0pf5d ~]# Logging to '/data/mysql/data/instance-r5y0pf5d.err'.
2021-01-04T02:09:22.225238Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/data
2021-01-04T02:09:22.378493Z mysqld_safe mysqld from pid file /data/mysql/data/instance-r5y0pf5d.pid ended
[root@查看是否启动成功 ~]# ps -ef|grep mysqld
root 32191 31067 0 10:12 pts/0 00:00:00 grep --color=auto mysqld
[root@instance-r5y0pf5d ~]# mysqld_safe --skip-grant-table --skip-networking &
[1] 11785
[root@instance-r5y0pf5d ~]# 2021-01-04T03:55:00.521347Z mysqld_safe Logging to '/tmp/mysql.err'.
2021-01-04T03:55:00.548132Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/data
2021-01-04T03:55:02.612114Z mysqld_safe mysqld from pid file /data/mysql/data/instance-r5y0pf5d.pid ended
[root@instance-r5y0pf5d ~]# ps -ef|grep mysql
root 12043 11585 0 11:55 pts/3 00:00:00 grep --color=auto mysql
[1]+ Done mysqld_safe --skip-grant-table --skip-networking
[root@instance-r5y0pf5d ~]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
如果上面的方法无法启动维护模式,请试试下面的这个方法
pkill mysql
mysqld_safe --skip-grant-tables --syslog --skip-networking &
mysql
[root@instance-r5y0pf5d ~]# pkill mysql
[root@instance-r5y0pf5d ~]# ps -ef|grep mysql
root 11745 11585 0 11:53 pts/3 00:00:00 grep --color=auto mysql
[root@instance-r5y0pf5d ~]# mysqld_safe --skip-grant-tables --syslog --skip-networking &
[1] 12075
[root@instance-r5y0pf5d ~]# 2021-01-04T03:56:18.988946Z mysqld_safe Logging to syslog.
2021-01-04T03:56:18.993928Z mysqld_safe Logging to '/tmp/mysql.err'.
2021-01-04T03:56:19.020327Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/data
[root@instance-r5y0pf5d ~]# ps -ef|grep mysql
root 12075 11585 0 11:56 pts/3 00:00:00 /bin/sh /application/mysql/bin/mysqld_safe --skip-grant-tables --syslog --skip-networking
mysql 12297 12075 1 11:56 pts/3 00:00:00 /application/mysql/bin/mysqld --basedir=/application/mysql --datadir=/data/mysql/data --plugin-dir=/application/mysql/lib/plugin --user=mysql --skip-grant-tables --skip-networking --log-error=/tmp/mysql.err --pid-file=instance-r5y0pf5d.pid --socket=/tmp/mysql.sock --port=3306 --log-syslog=1 --log-syslog-facility=daemon --log-syslog-tag=
root 12340 11585 0 11:56 pts/3 00:00:00 grep --color=auto mysql
[root@instance-r5y0pf5d ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 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>
3)修改数据库密码的方法:
pkill mysql
mysqld_safe --skip-g
rant-tables --syslog --skip-networking &
mysql
flush privileges;
alter user root@'localhost' identified by '12345678';
[root@instance-r5y0pf5d ~]# pkill mysql
[root@instance-r5y0pf5d ~]# mysqld_safe --skip-grant-tables --syslog --skip-networking &
[1] 15407
[root@instance-r5y0pf5d ~]# 2021-01-04T05:41:30.684648Z mysqld_safe Logging to syslog.
2021-01-04T05:41:30.689655Z mysqld_safe Logging to '/tmp/mysql.err'.
2021-01-04T05:41:30.716186Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/data
[root@instance-r5y0pf5d ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 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> alter user root@'localhost' identified by '12345678';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> alter user root@'localhost' identified by '12345678';
Query OK, 0 rows affected (0.00 sec)
mysql>
4)正常启动mysql测试密码是否修改成功
pkill mysql
systemctl start mysqld
mysql -uroot -p12345678
mysql> exit
Bye
[root@instance-r5y0pf5d ~]# ps -ef|grep mysql
root 15407 15201 0 13:41 pts/1 00:00:00 /bin/sh /application/mysql/bin/mysqld_safe --skip-grant-tables --syslog --skip-networking
mysql 15629 15407 0 13:41 pts/1 00:00:00 /application/mysql/bin/mysqld --basedir=/application/mysql --datadir=/data/mysql/data --plugin-dir=/application/mysql/lib/plugin --user=mysql --skip-grant-tables --skip-networking --log-error=/tmp/mysql.err --pid-file=instance-r5y0pf5d.pid --socket=/tmp/mysql.sock --port=3306 --log-syslog=1 --log-syslog-facility=daemon --log-syslog-tag=
root 15844 15201 0 13:51 pts/1 00:00:00 grep --color=auto mysql
[root@instance-r5y0pf5d ~]# pkill mysql
[root@instance-r5y0pf5d ~]# 2021-01-04T05:52:02.059953Z mysqld_safe mysqld from pid file /data/mysql/data/instance-r5y0pf5d.pid ended
[1]+ Done mysqld_safe --skip-grant-tables --syslog --skip-networking
[root@instance-r5y0pf5d ~]# ps -ef|grep mysql
root 15865 15201 0 13:52 pts/1 00:00:00 grep --color=auto mysql
[root@instance-r5y0pf5d ~]# systemctl start mysqld
[root@instance-r5y0pf5d ~]# ps -ef|grep mysql
mysql 15875 1 6 13:52 ? 00:00:00 /application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
root 15904 15201 0 13:52 pts/1 00:00:00 grep --color=auto mysql
[root@instance-r5y0pf5d ~]# mysql -uroot -p12345678
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.26 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>