Use yum to install mysql database under centos7

Sharing the process of downloading the mysql database and the method of remote connection, integrating some online resources and some problems encountered by myself, some commonly used commands will not be introduced one by one. Don't say much, let's start now

 

1. Download the mysql repo source

$ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

2. Install the mysql-community-release-el7-5.noarch.rpm package

$ sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

After installing this package, you will get two mysql yum repo sources: /etc/yum.repos.d/mysql-community.repo, /etc/yum.repos.d/mysql-community-source.repo.

3. Install mysql

$ sudo yum install mysql-server

You can install it according to the steps, but after the installation is complete, there is no password, you need to reset the password.

4. Reset password

Before resetting your password, you must first log in

$ mysql -u root

This error may be reported when logging in: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2), the reason is /var/lib/mysql Access rights issues. The following command changes the owner of /var/lib/mysql to the current user:

$ sudo chown -R root:root /var/lib/mysql

Then, restart the service:

$ service mysqld restart

Next, log in to reset your password:

$ mysql -u root
mysql > use mysql;
mysql > update user set password=password('123456') where user='root';
mysql > exit;

5. Open port 3306

    Method 1, iptables (used before CentOS 7.x version, not recommended)

    Open the iptables configuration file:

$vi /etc/sysconfig/iptables

 

 

    Add the following two lines above -A INPUT -j REJECT --reject-with icmp-host-prohibited
    -A RH-Firewall-1-INPUT -m statestate NEW -m tcp -p tcp –dport 3306 -j ACCEPT
    -A RH-Firewall-1-INPUT -m statestate NEW -m udp -p udp –dport 3306 -j ACCEPT
If the iptables configuration file does not exist, execute the yum install iptables-servicesinstallation
     Restart iptables:
$ service iptables restart
    Method 2, firewall-cmd (recommended)
$ firewall-cmd --permanent --zone=public --add-port=3306/tcp
$ firewall-cmd --permanent --zone=public --add-port=3306/udp

permanent means permanently valid, that is, restarting will not fail
   Restart the firewall:
$ firewall-cmd --reload

6. Create a common user and authorize

Example (login with root user):

mysql > use mysql;
Create a user and set a password: username is the username, localhost is the ip address (localhost is fine), and password is the password
mysql >CREATE USER username@localhost IDENTIFIED BY 'password';
mysql > GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password'
              WITH GRANT OPTION;
'%' means that all ip addresses can be connected
Refresh permissions
mysql > flushn privileges;

 

Now you can connect to the mysql server from the client, if the connection reports such an error: ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.x.xxx' (113) . Because we are centos7, please confirm whether the firewall is turned on. Centos7 is firewall by default.

We can stop it and disable it, then start our familiar iptables and that's it!

Note: The mysql client needs to be installed with the mysql client.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326282928&siteId=291194637