Linux安装和配置MySQL5.7【修改密码、修改字符集等配置】
安装配置MySQL ,看这篇文章就够了。
本文包括mysql安装,默认密码获取与修改,修改MySQL默认编码字符集,防火墙关闭
CentOS6、CentOS7均可用
一、下载和安装mysql源
先下载 mysql源安装包
[[email protected] ~]# wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
如果显示
-bash: wget: command not found
我们先安装下wget
yum -y install wget
然后执行 wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
安装mysql源
yum -y localinstall mysql57-community-release-el7-11.noarch.rpm
二、在线安装Mysql
yum -y install mysql-community-server
下载的东西比较多 要稍微等会;
三、启动Mysql服务
systemctl start mysqld
四、设置开机启动
[[email protected] ~]# systemctl enable mysqld
[[email protected] ~]# systemctl daemon-reload
检查mysql服务状态 [active running是正常]
[[email protected] ~]# service mysqld status
五、修改root本地登录密码
第一次启动mysql,会在日志文件中生成root用户的一个随机密码,使用下面命令查看该密码
[[email protected] ~]# grep 'temporary password' /var/log/mysqld.log
[[email protected] ~]# mysql -u root -p
Enter password: (粘贴上面复制的密码)
输入临时密码 进入mysql命令行:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '[email protected]';
Query OK, 0 rows affected (0.00 sec)
修改密码为 [email protected] (备注 mysql5.7默认密码策略要求密码必须是大小写字母数字特殊字母的组合,至少8位)
//设置用户 root 可以在任意 IP 下被访问:
mysql>grant all privileges on *.* to [email protected]"%" identified by "新密码";
//设置用户 root 可以在本地被访问:
mysql>grant all privileges on *.* to [email protected]"localhost" identified by "新密码";
//刷新权限使之生效:
mysql> flush privileges;
到这里mysql就已经安装好了
启动 MySQL 服务:
service mysqld start
关闭 MySQL 服务:
service mysqld stop
重启 MySQL 服务:
service mysqld restart
查看 MySQL 的状态:
service mysqld status
六、修改MySQL字符集
查看 MySQL 的字符集:
mysql> show variables like '%character%';
退出MySQL命令行:
mysql> quit
Bye
设置 MySQL 的字符集为 UTF-8:
打开 /etc 目录下的 my.cnf 文件(此文件是 MySQL 的主配置文件):
[[email protected] ~]# vi /etc/my.cnf
在 [mysqld] 前添加如下代码:
[client]
default-character-set=utf8
在 [mysqld] 后添加如下代码:
character_set_server=utf8
如图:
修改后保存退出,重启MySQL
[[email protected] ~]# service mysqld restart
重新进入MySQL命令行
[[email protected] ~]# mysql -uroot -p
再次查看字符集:
mysql> show variables like '%character%';
七、关于开发环境关闭防火墙命令
CentOS7防火墙
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动