因为要通过yum安装,前提是centos能上网。
下载
到mysql官网下载页面,https://www.mysql.com/downloads/
点击“MySQL Community (GPL) Downloads »”
然后选择yum这个
选择对应版本,我是centos,所以选择redhat,点击“Download".
点击“No thanks, just start my download.”
然后就下载好rpm,大概25K。下载好上传上centos上。
mysql80-community-release-el7-3.noarch.rpm
或者在centos上通过wget下载
wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
安装
1.直接通过yum安装
yum install -y mysql80-community-release-el7-3.noarch.rpm
这个安装很快,安装好后再安装server:
yum -y install mysql-community-server
server安装会比较慢(看网速)。
server安装好后会看到 mariadb被替换,mariadb是mysql一个分支,是centos安装自带的。
2.启动MySQL
systemctl start mysqld
3.查看root默认密码
cat /var/log/mysqld.log
红框中的这是root默认密码
4登录mysql,用root登录,输入上面的密码,
mysql -hlocalhost -uroot -p
如果报错ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
vim /etc/my.cnf,在[mysqld]下追加skip-grant-tables
重启:systemctl restart mysqld,然后用mysql命令直接登录
mysql> use mysql;
mysql> update user set authentication_string=‘’ where user=‘root’;
然后退出mysql,将skip-grant-tables注释掉,再重启mysql
登录mysql -uroot --connect-expired-password
修改默认密码:alter user user() identified by ‘123456!aA’;
4.修改root默认密码
mysql> alter user root@localhost identified by '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
设为123456会报错,因为有强制密码规则,先设置一个复杂的密码,数字,特殊字符,大小写字母都要有。
mysql> alter user root@localhost identified by '123456!aA';
Query OK, 0 rows affected (0.02 sec)
然后查看密码规则
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 8 |
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | MEDIUM |
| validate_password.special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.02 sec)
修改密码规则
mysql> set global validate_password.policy=LOW;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password.length=1;
Query OK, 0 rows affected (0.00 sec)
然后再把密码设置为123456
mysql> alter user root@localhost identified by '123456';
Query OK, 0 rows affected (0.02 sec)
远程登录
从另一台机器上用root用户登录mysql,会报错,因为mysql默认所有用户不能从其他机器远程登录
[root@horse mysql]# mysql -h192.168.137.2 -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1130 (HY000): Host 'horse' is not allowed to connect to this MySQL server
下面是配置远程登录方法:
1.在安装mysql机器上用root用户登录,
[root@horse mysql]# mysql -uroot -p123456
mysql> select user,host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
mysql>
select user,host from mysql.user;查看所有用户的远程登录参数,这里看到root是localhost,意思是root只能通过本机登录。
下面是修改,把host改为“%”,“%”意思是任意ip都可以登录。注意修改完后要执行flush。
mysql> update mysql.user set host="%" where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
如果要配置只允许某些ip登录到mysql,在mysql.user表中插入用户root和对应的ip就行了,insert into mysql.user(user,host) values(“root”,“ip地址”),也就是表中root会有多条记录
其他用户也是这样配置,否则新建一个用户之后,不能在其他机器上用这个用户登录。