先检查系统是否装有mysql
如果返回空值,说明没有安装MySQL;
[root@localhost ~]# rpm -qa | grep mysql
这里执行安装命令是无效的,因为centos-7默认是Mariadb,所以执行以下命令只是更新Mariadb数据库; [root@localhost ~]# rpm -qa | grep mysql下载mysql的repo源
[root@localhost ~]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm安装mysql-community-release-el7-5.noarch.rpm包
[root@localhost ~]# sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm安装MySQL
[root@localhost ~]# sudo yum install mysql-server根据步骤安装就可以了,不过安装完成后,没有密码,需要重置密码。安装后再次查看mysql
接下来登录MySQL
[root@localhost ~]# mysql -u root登录时有可能报出如下图的错误,原因是/var/lib/mysql的访问权限问题。下面的命令把/var/lib/mysql的拥有者改为当前用户:
[root@localhost ~]# chown root /var/lib/mysql
然后重启MySQL服务;
[root@localhost ~]# service mysqld start如需要重置登录密码,请自行百度。
必要时加入以下命令行,为root添加远程连接的能力。链接密码为 “pengqi”(不包括双引号)
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "pengqi";最后再打开3306端口,实现远程连接服务,命令如下
[root@localhost ~]#/sbin/iptables -I INPUT -p tcp --dport 3306-j ACCEPT
划重点:如果中途报错:can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
1.删除原来安装过的mysql残留的数据(这一步非常重要,问题就出在这)
rm -rf /var/lib/mysql
2.重启mysqld服务
systemctl restart mysqld
3.再去找临时密码
grep 'temporary password' /var/log/mysqld.log
登录成功后修改密码:
set password=password("yourpassword");
设置安全选项:
mysql_secure_installation
启动mysql
systemctl start mariadb.service 或者 systemctl start mysqld.service
结束
systemctl stop mariadb.service 或者 systemctl stop mysqld.service
重启
systemctl restart mariadb.service 或者 systemctl restart mysqld.service
开机自启
systemctl enable mariadb.service 或者 systemctl enable mysqld.service