Ubuntu 24.04安装MySQL5.7重置密码

高版本MySQL占用资源有点多,安装5.7也够用。

wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
tar zxvf mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.44-linux-glibc2.12-x86_64 /usr/local/mysql
useradd -r -s /sbin/nologin mysql
cd /usr/local/mysql/
mkdir mysql-files
chown mysql:mysql mysql-files
chmod 750 mysql-files/
# ln做软链接时,Debian12上可能有所区别,方法就是ldd bin/mysql看其他库文件路径在哪,链接到哪就行。
ln -s /usr/lib/x86_64-linux-gnu/libaio.so.1t64.0.2 /usr/lib/x86_64-linux-gnu/libaio.so.1
ln -s /usr/lib/x86_64-linux-gnu/libncurses.so.6.4 /usr/lib/x86_64-linux-gnu/libncurses.so.5
ln -s /lib/x86_64-linux-gnu/libtinfo.so.6.4 /lib/x86_64-linux-gnu/libtinfo.so.5
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql         #记住临时密码
cp support-files/mysql.server /etc/init.d/mysql
#先执行init.d下mysql是因为这样后面systemctl才可以使用。
/etc/init.d/mysql start
/etc/init.d/mysql stop
systemctl status mysql
systemctl start mysql
/usr/lib/systemd/systemd-sysv-install enable mysql
bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
#设置一些安全选项
bin/mysql_secure_installation
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> ~/.bashrc
source ~/.bashrc
vi /usr/local/mysql/my.cnf
#写入,设置监听地址、端口及编码
[mysqld]
tls_version=TLSv1.2
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
#skip-grant-tables
bind-address=127.0.0.1
port=3306
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
skip-character-set-client-handshake
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
#重启mysql
systemctl restart mysql

bind-address根据需要修改,如果忘记密码,把skip-grant-tables注释取消,重启systemctl restart mysql ,然后就可以无密码登录,登录后修改密码

mysql
UPDATE mysql.user SET authentication_string=password("123456") WHERE User='root';
Flush Privileges;

记得把my.cnf里skip-grant-tables重新注释掉,重启mysql。

如果是已知密码,可以使用mysqladmin修改

mysqladmin -u root -p password

 

 

阅读剩余
THE END