CentOS7的yum源中默认是没有mysql的,为了解决这个问题,先下载mysql的repo源。
1. 下载mysql的repo源
#wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
2. 安装mysql-community-release-el7-5.noarch.rpm包
#rpm -ivh mysql-community-release-el7-5.noarch.rpm
安装这个包后,会获得两个mysql的yum repo源 /etc/yum.repos.d/:
/etc/yum.repos.d/mysql-community.repo,/etc/yum.repos.d/mysql-community-source.repo。
3. 安装mysql
#yum install -y mysql-server
根据步骤安装就可以了,不过安装完成后,没有密码,需要重置密码。
4. 重置密码
重置密码前,首先要登录
#mysql -u root
登录时有可能报这样的错:ERROR 2002 (HY000): Can't connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2),
原因是/var/lib/mysql的访问权限问题,下面的命令把/var/lib/mysql的拥有者改为当前用户:
eg:
#chown -R root:root /var/lib/mysql
然后,重启服务:
#service mysqld restart
接下来登录重置密码:
#mysql -u root
mysql > use mysql;
mysql > update user set password=password('123456') where user='root';
mysql > exit;
5. 开放3306端口
#/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
#service iptables save #将更改进行保存
#service iptables restart #重启防火墙以便改动生效
这样从其它客户机也可以连接上mysql服务了。
6.But something error
如果连接mysql的时候发生类似这个错误:
ERROR 1130: Host '192.168.1.2' is not allowed to connect to this MySQL server
1. 改表法
可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改称"%"
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;
如果你想允许用户myuser从ip为192.168.1.2的主机连接到mysql服务器,并使用mypassword作为密码
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.2' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
mysql>flush privileges; 这句一定要加上!!!
1. 下载mysql的repo源
#wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
2. 安装mysql-community-release-el7-5.noarch.rpm包
#rpm -ivh mysql-community-release-el7-5.noarch.rpm
安装这个包后,会获得两个mysql的yum repo源 /etc/yum.repos.d/:
/etc/yum.repos.d/mysql-community.repo,/etc/yum.repos.d/mysql-community-source.repo。
3. 安装mysql
#yum install -y mysql-server
根据步骤安装就可以了,不过安装完成后,没有密码,需要重置密码。
4. 重置密码
重置密码前,首先要登录
#mysql -u root
登录时有可能报这样的错:ERROR 2002 (HY000): Can't connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2),
原因是/var/lib/mysql的访问权限问题,下面的命令把/var/lib/mysql的拥有者改为当前用户:
eg:
#chown -R root:root /var/lib/mysql
然后,重启服务:
#service mysqld restart
接下来登录重置密码:
#mysql -u root
mysql > use mysql;
mysql > update user set password=password('123456') where user='root';
mysql > exit;
5. 开放3306端口
#/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
#service iptables save #将更改进行保存
#service iptables restart #重启防火墙以便改动生效
这样从其它客户机也可以连接上mysql服务了。
6.But something error
如果连接mysql的时候发生类似这个错误:
ERROR 1130: Host '192.168.1.2' is not allowed to connect to this MySQL server
1. 改表法
可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改称"%"
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;
如果你想允许用户myuser从ip为192.168.1.2的主机连接到mysql服务器,并使用mypassword作为密码
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.2' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
mysql>flush privileges; 这句一定要加上!!!