1. 下载
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
2. 解压
rpm -ivh mysql-community-release-el7-5.noarch.rpm
解压完,在路径/etc/yum.repos.d下有两个mysql的yum repo源
mysql-community.repo 和 mysql-community-source.repo
3. 安装mysql
yum install mysql-server
4. 重置密码
mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
解决办法:
修改mysql的拥有者为当前用户 chown -R root:root /var/lib/mysql
root为当前用户
重启服务 service mysqld restart
5. 继续重置密码:
mysql -u root
UPDATE user SET Password=PASSWORD('newpassword') where USER='root';(旧版数据库)
或 UPDATE user SET authentication_string=PASSWORD('newpassword') where USER='root';(新版数据库)
FLUSH PRIVILEGES;
6. 设置远程连接数据库
mysql> use mysql;
Database changed
mysql> select host,user,password from user;
+-------------------------+------+-------------------------------------------+
| host | user | password |
+-------------------------+------+-------------------------------------------+
| localhost | root | *0F11165043F0A6B93SSEESDFSFDsDFsD7DEA610|
| iz2ze6g18mpr8vc88n2rhkz | root | *0F11165043F0A6B93SSEESDFSFDsDFsD7DEA610 |
| 127.0.0.1 | root | *0F11165043F0A6B93SSEESDFSFDsDFsD7DEA610 |
| ::1 | root | *0F11165043F0A6B93SSEESDFSFDsDFsD7DEA610 |
| localhost | | |
| iz2ze6g18mpr8vc88n2rhkz | | |
+-------------------------+------+-------------------------------------------+
6 rows in set (0.00 sec)
mysql> Grant all privileges on *.* to 'root'@'%' identified by '01234' with grant option;
Query OK, 0 rows affected (0.00 sec)
((%表示是所有的外部机器,如果指定某一台机,就将%改为相应的机器名;‘root’则是指要使用的用户名,))
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select host,user,password from user;
+-------------------------+------+-------------------------------------------+
| host | user | password |
+-------------------------+------+-------------------------------------------+
| localhost | root | *0F11165043F0A6B93SSEESDFSFDsDFsD7DEA610 |
| iz2ze6g18mpr8vc88n2rhkz | root | *0F11165043F0A6B93SSEESDFSFDsDFsD7DEA610 |
| 127.0.0.1 | root | *0F11165043F0A6B93SSEESDFSFDsDFsD7DEA610 |
| ::1 | root | *0F11165043F0A6B93SSEESDFSFDsDFsD7DEA610|
| localhost | | |
| iz2ze6g18mpr8vc88n2rhkz | | |
| % | root | *0F11165043F0A6B93SSEESDFSFDsDFsD7DEA610 |
+-------------------------+------+-------------------------------------------+
7 rows in set (0.00 sec)
7. 防火墙设置
远程访问 MySQL, 需开放默认端口号 3306.
[root@iz2ze6g18mpr8vc88n2rhkz /]# systemctl start firewalld 开启防火墙
//Centos7 开放端口
//命令含义:
//--zone #作用域
//--add-port=80/tcp #添加端口,格式为:端口/通讯协议
//--permanent #永久生效,没有此参数重启后失效
[root@iz2ze6g18mpr8vc88n2rhkz /]# firewall-cmd --permanent --zone=public --add-port=3306/tcp
success
[root@iz2ze6g18mpr8vc88n2rhkz /]# firewall-cmd --permanent --zone=public --add-port=3306/udp
success
//重启防火墙
[root@iz2ze6g18mpr8vc88n2rhkz /]# firewall-cmd --reload
success
8. 开机自起
查看 MySQL 服务是否开机启动
[root@localhost ~]# systemctl is-enabled mysql.service;echo $?
enabled
0
如果是 enabled 则说明是开机自动,如果不是,执行
chkconfig --levels 235 mysqld on
9. 设置字符集
修改 /etc/my.cnf 文件,添加字符集的设置
[mysqld]
character_set_server = utf8
[mysql]
default-character-set = utf8
重启 service mysql start
查看
mysql> SHOW VARIABLES LIKE 'character%'
-> ;
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)