1. 下载mysql
版本: mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
2. 解压到指定目录下,并更改文件名为mysql
# tar -zxvf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
# cd /usr/local
# mv mysql-5.7.29-linux-glibc2.12-x86_64 mysql
3. 创建mysql用户组和用户
# groupadd mysql
# useradd -r -g mysql mysql
4. 创建数据目录并赋予权限
# cd /usr/local/mysql/
# mkdir -p ./data
# chown mysql:mysql -R ./data
5. 创建配置文件my.cnf
# vi /usr/local/mysql/my.cnf
内容如下:
[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
log-error=/usr/local/mysql/data/mysql.err
pid-file=/usr/local/mysql/data/mysql.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true
6. 进入mysql的bin目录,安装包并初始化
# cd /usr/local/mysql/bin/
# yum install libaio-devel.x86_64
# ./mysqld --defaults-file=/usr/local/mysql/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql --initialize
7. 查看密码
# cat /usr/local/mysql/data/mysql.err
[Note] A temporary password is generated for root@localhost: Uo.KEY/!n2.u
查看可知密码为: Uo.KEY/!n2.u
8. 将mysql.server放置到/etc/init.d/mysql中
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
9. 创建文件/var/log/mariadb/mariadb.log,并授权
# mkdir /var/log/mariadb
# touch /var/log/mariadb/mariadb.log
# chown -R mysql:mysql /var/log/mariadb/
10. 启动
# service mysql start
11. 修改密码
登录密码查看: 7. 查看密码
# cd /usr/local/mysql/bin
# ./mysql -u root -p
输入初始密码登录成功后:
mysql> SET PASSWORD = PASSWORD('123456');
mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
mysql> FLUSH PRIVILEGES;
mysql> use mysql
mysql> update user set host = '%' where user = 'root';
mysql> FLUSH PRIVILEGES;
卸载Mysql
1.首先查看mysql的安装情况
rpm -qa|grep -i mysql
显示之前安装了:
MySQL-client-5.5.25a-1.rhel5
MySQL-server-5.5.25a-1.rhel5
2.停止mysql服务,并删除包
service mysql stop
rpm -ev MySQL-client-5.5.25a-1.rhel5
rpm -ev MySQL-server-5.5.25a-1.rhel5
如果提示依赖包错误,则使用以下命令尝试
rpm -ev MySQL-client-5.5.25a-1.rhel5 --nodeps
如果提示错误:error: %preun(xxxxxx) scriptlet failed, exit status 1
则用以下命令尝试:
rpm -e --noscripts MySQL-client-5.5.25a-1.rhel5
3.查找之前老版本mysql的目录、并且删除老版本mysql的文件和库
whereis mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/lib64/mysql
删除对应的mysql目录
rm -rf /var/lib/mysql
rm -rf /var/lib/mysql
rm -rf /usr/lib64/mysql
注意:卸载后/etc/my.cnf不会删除,需要进行手工删除
rm -rf /etc/my.cnf
//可能有 my.cnf.rpmsave 文件
rm -rf my.cnf.rpmsave
4.再次查找机器是否安装mysql
rpm -qa|grep -i mysql
无结果,说明已经卸载彻底
附加:
解决:
检查服务器的防火墙 或 阿里云的 安全组规则 是否配置