话不多说,直接上步骤。
1、检查
查看是否安装mysql
rpm -qa|grep -i mysql
2、卸载
rpm -e xxx(普通删除)
rpm -e --nodeps(强力删除)
3、解压
tar -xvf mysql-5.7.19-1.el6.x86_64.rpm-bundle.tar
下载到的安装包解压后会包括以下rpm包:
mysql-community-server-5.7.19-1.el6.x86_64.rpm:服务端安装包
mysql-community-client-5.7.19-1.el6.x86_64.rpm:客户端安装包
mysql-community-devel-5.7.19-1.el6.x86_64.rpm:包含开发用的库头文件安装包
mysql-community-test-5.7.19-1.el6.x86_64.rpm:一些测试的安装包
mysql-community-embedded-5.7.19-1.el6.x86_64.rpm:嵌入式mysql的安装包
mysql-community-libs-5.7.19-1.el6.x86_64.rpm:mysql共享库安装包
mysql-community-common-5.7.19-1.el6.x86_64.rpm:MySQL的配置安装包
mysql-community-libs-compat-5.7.19-1.el6.x86_64.rpm:mysql兼容性库安装包
mysql-community-embedded-devel-5.7.19-1.el6.x86_64.rpm:嵌入式开发库的安装包
4、顺序安装
上述的rpm包按需安装即可。一般mysql-community-client-5.7.24-1.el6.x86_64.rpm、
mysql-community-common-5.7.24-1.el6.x86_64.rpm、mysql-community-libs-5.7.24-1.el6.x86_64.rpm、mysql-community-libs-compat-5.7.24-1.el6.x86_64.rpm、mysql-community-server-5.7.24-1.el6.x86_64.rpm即可满足基本需求。
由于rpm的依赖关系,需要按顺序安装,此处列出安装顺序:
rpm -ivh mysql-community-common-5.7.19-1.el6.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.19-1.el6.x86_64.rpm
rpm -ivh mysql-community-client-5.7.19-1.el6.x86_64.rpm
rpm -ivh mysql-community-devel-5.7.19-1.el6.x86_64.rpm
rpm -ivh mysql-community-embedded-5.7.19-1.el6.x86_64.rpm
rpm -ivh mysql-community-embedded-devel-5.7.19-1.el6.x86_64.rpm
rpm -ivh mysql-community-libs-compat-5.7.19-1.el6.x86_64.rpm
rpm -ivh mysql-community-server-5.7.19-1.el6.x86_64.rpm
5、修改配置文件
vi /etc/my.cnf
[mysqld]
port=3306 #设置端口
character-set-server=utf8 #设置编码格式
max_connections=1000 #设置最大连接数为1000
max_allowed_packet=67108864 #单次执行sql语句的最大的byte
datadir=/var/lib/mysql #(可能已有)datadir为mysql的 data 包存放路径
lower_case_table_names=1 # 设置表名大小写不敏感
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES #开启严格模式
6、启动数据库
service mysqld start #开启
systemctl enable mysqld #开机启动
service mysqld stop #停止
service mysqld restart #重启
7、初始化root密码(mysql5.7以后自动生成随机密码)
grep "password" /var/log/mysqld.log #查看临时密码
1 [Note] A temporary password is generated for root@localhost: 2h>o#oCyMszj
使用上面的临时密码登录MySQL(第三行最后冒号后面的:2h>o#oCyMszj)。
mysql -uroot -p # 回车键入临时密码
2h>o#oCyMszj
mysql> SHOW VARIABLES LIKE 'validate_password%'; # 查看密码规则
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password'; #修改为新密码
mysql> SET PASSWORD = PASSWORD('123456');#修改root密码为123456,如果提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements,则说明密码设置太简单,如果想设置123456这样的简单密码,可在SQL中执行:
#mysql> set global validate_password_policy=0;
#mysql> set global validate_password_length=1;
#这样再次执行SET PASSWORD = PASSWORD('123456')就可成功。
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.07 sec)
mysql> flush privileges; # 刷新权限
Query OK, 0 rows affected (0.00 sec)
mysql> quit # 退出数据库
service mysqld restart #重启数据库