centos7从安装mysq数据库到用别的主机访问mysql数据库详解,centos7mysq
1、卸载系统自带的 mariadb
由于centos7之后,yum源中就没有了mysql数据库。有的只是mariadb数据库,个人还是喜欢使用mysql数据库所以我卸载了并删除了mariadb数据库。
先查看mariadb的安装包,然后移除这个包
[root@VM_0_4_centos ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.32-2.el7.centos.x86_64
[root@VM_0_4_centos ~]# rpm -emariadb-libs-5.5.32-2.el7.centos.x86_64 --nodeps
2、下载mysql5.7.16 rpm安装包,官网最新的mysql安装包是mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar下载地址
[root@VM_0_4_centos ~]# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar
然后解压(我是把安装包移动到mysql文件夹下解压的)
[root@VM_0_4_centos mysql]# tar -xvf mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar
mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-5.7.16-1.el7.x86_64.rpm
mysql-community-common-5.7.16-1.el7.x86_64.rpm
mysql-community-devel-5.7.16-1.el7.x86_64.rpm
mysql-community-embedded-5.7.16-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.16-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.16-1.el7.x86_64.rpm
mysql-community-libs-5.7.16-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.16-1.el7.x86_64.rpm
mysql-community-minimal-debuginfo-5.7.16-1.el7.x86_64.rpm
mysql-community-server-5.7.16-1.el7.x86_64.rpm
mysql-community-server-minimal-5.7.16-1.el7.x86_64.rpm
mysql-community-test-5.7.16-1.el7.x86_64.rpm
3、安装
依次执行(几个包有依赖关系,所以执行有先后)下面命令安装
[root@VM_0_4_centos mysql]# rpm -ivh mysql-community-common-5.7.16-1.el7.x86_64.rpm
[root@VM_0_4_centos mysql]# rpm -ivh mysql-community-libs-5.7.16-1.el7.x86_64.rpm
[root@VM_0_4_centos mysql]# rpm -ivh mysql-community-client-5.7.16-1.el7.x86_64.rpm
[root@VM_0_4_centos mysql]# rpm -ivh mysql-community-server-5.7.16-1.el7.x86_64.rpm
在安装rpm -ivh mysql-community-server-5.7.16-1.el7.x86_64.rpm的时候会出现以下的错误:
libnuma.so.1()(64bit) is needed by mysql-community-server-5.7.16-1.el7.x86_6
是因为缺失依赖包需要执行:
[root@VM_0_4_centos mysql]# yum install numact
然后再执行
[root@VM_0_4_centos mysql]# rpm -ivh mysql-community-server-5.7.16-1.el7.x86_64.rpm
就可以安装成功了
4、数据库初始化
为了保证数据库目录为与文件的所有者为 mysql 登陆用户,如果你是以 root 身份运行 mysql 服务,需要执行下面的命令初始化
mysqld --initialize --user=mysql
如果是以 mysql 身份运行,则可以去掉 --user 选项。
另外 --initialize 选项默认以“安全”模式来初始化,则会为 root 用户生成一个密码并将该密码标记为过期,登陆后你需要设置一个新的密码,而使用 --initialize-insecure 命令则不使用安全模式,则不会为 root 用户生成一个密码。
这里演示使用的 --initialize 初始化的,会生成一个 root 账户密码,密码在log文件里,红色区域的就是自动生成的密码
[root@VM_0_4_centos mysql]# cat /var/log/mysqld.log
2016-12-07T04:41:58.028558Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-12-07T04:41:58.315188Z 0 [Warning] InnoDB: New log files created, LSN=45790
2016-12-07T04:41:58.355114Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-12-07T04:41:58.418448Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 7cc98831-bc37-11e6-a263-000c29f902dc.
2016-12-07T04:41:58.419935Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2016-12-07T04:41:58.420558Z 1 [Note] A temporary password is generated for root@localhost: )2rafs,kklj
现在启动mysql数据库systemctl start mysqld.service
[root@VM_0_4_centos mysql]# systemctl start mysqld.service
[root@VM_0_4_centos mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.16
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
5、修改密码
该密码被标记为过期了,如果想正常使用还需要修改密码
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
以前的 password()函数将会被抛弃,官方建议使用下面的命令来修改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
同时,如果你设置的密码过于简单也会报错。
6、授权让别的主机也可以访问这台主机的mysql服务器
在安装完毕之后,小编用别的主机的Navicat for MySQL访问这台主机的mysql的时候出现了这个问题:1130-host . is not allowed to connect to this MySql server,不允许账号远程登陆的错误小编把解决方法也贴在这供大家参考
1. 改表法。
可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改称"%"
mysql -u root -p
mysql>use mysql;
mysql>update user set host = '%' where user = 'root';
mysql>select host, user from user;
2. 授权法。
例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话。
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;
如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务器,并使用mypassword作为密码
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;
如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务器的dk数据库,并使用mypassword作为密码
GRANT ALL PRIVILEGES ON dk.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;
小编使用的是先将user的host改成%然后授权的方式,然后刷新了一下权限FLUSH PRIVILEGES;就可以了,仅供参考