很多同学都装过mysql数据库,但是windows系统和linux系统有点不一样,我就自己使用yum源在云服务器上安装mysql做一个分享,希望能帮助到其他小伙伴。
一、安装器前的准备工作
我的云主机使用的系统是CentOS7,但是不巧的是yum源中好像没有mysql,所以查阅了一下资料,可以通过现在mysql的repo源来解决问题。也就是下面两条语句:
1[root@VM_0_9_centos /]#wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm(下载repo源)
2[root@VM_0_9_centos/]#sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm(安装mysql-community-release-el7-5.noarch.rpm包)
上述两步可能需要等一会才能完成(中途会让你输入y/n,输入y即可),当上述两步完成后在我们的 /etc 目录下会多出两个以 .repo 结尾的文件,目录如下:
二、安装
使用命令:
1、[root@VM_0_9_centos /]# sudo yum install mysql-server
等待安装完之后先登录一下:
2、[root@VM_0_9_centos /]# mysql -u root
这时一般会报错:ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2)
执行赋权命令:
3、[root@localhost ~]# sudo chown -R openscanner:openscanner /var/lib/mysql(把/var/lib/mysql的拥有者改为当前用户)
接下来重启服务:
4、[root@VM_0_9_centos /]# service mysqld restart
一般这个时候可以修改密码了:
5、[root@VM_0_9_centos /]# mysql -u root
mysql > use mysql;
mysql > update user set password=password("新密码") where user="root";
mysql > exit;
6、重置完密码以后一般是可以链接的,但也有可能会报错:
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
这个时候我们就需要去搞一下my.cnf文件了,不知道这个文件在哪里?没关系,使用[root@localhost ~]#whereis my;
[root@VM_0_9_centos /]# whereis my
my: /etc/my.cnf
编辑这个文件:
7、[root@VM_0_9_centos /]# vim /etc/my.cnf, 输入i进入编辑模式在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程,按Esc键退出编辑模式,使用wq保存。
接下来重启服务:
[root@VM_0_9_centos /]# service mysqld restart
[root@VM_0_9_centos /]# cd usr/lib64/mysql
mysql> use mysql;
mysql> update user set password=password("新密码") where user="root";
mysql> flush privileges;
mysql> quit
8、重新编辑my.cnf文件[root@VM_0_9_centos /]# vim /etc/my.cnf, 输入i进入编辑模式删掉添加“skip-grant-tables”按Esc键退出编辑模式,使用wq保存。
重启服务:
[root@VM_0_9_centos /]#service mysqld restart,到这里在命令行里链接已经没有任何问题了,但是想要其他主机连接还的进行一次修改。
[root@VM_0_9_centos /]#mysql -h localhost -uroot -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4 to server version: 4.0.20a-debug
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
mysql> use mysql; (此DB存放MySQL的各种配置信息)
Database changed
mysql> select host,user from user; (查看用户的权限情况)
mysql> select host, user, password from user;
我们可以看到机器全部是用localhost方式来连接的
mysql> Grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
(%表示是所有的外部机器,如果指定某一台机,就将%改为相应的机器名;‘root’则是指要使用的用户名,)
mysql> flush privileges; (运行此句才生效,或者重启MySQL)
至此大功告成,可以远程连接,可以本地链接。
更多内容请移步微信公众号和微信小程序“每天学java”,更多内容等你来get。