Install and configure mysql5.6 under Centos7 system -- personally operated

Centos7 replaced the default database mysql with Mariadb, which is not good news for those of us who still want to use mysql. However, there are countless tutorials online about installing mysql database in Linux, but most of the tutorials are full of loopholes. Today, in order to help those who need it, I wrote a tutorial on installing and configuring mysql5.6 on Centos7. Well, without further ado, let's go to the tutorial:

In the next mysql installation process, please make sure that your current directory is correct! 

eg: [root @localhost ~]# means the current directory is ~

[root @localhost mysql]# indicates that the current directory is mysql

1. Install MySQL

1. Download the installation package mysql-5.6.26-linux-glibc2.5-x86_64.tar (you can go to the official website to download it yourself - the address will change at any time, adjust according to the actual situation: https://downloads.mysql.com/archives/ community/ )

2. Uninstall the Mariadb that comes with the system


    Open Terminal:

        [root @localhost  ~]# rpm -qa|grep mariadb // Query the installed mariadb  

        [root @localhost  ~]# rpm -e --nodeps file name // Uninstall mariadb, the file name is the file queried by the above command  

3. Delete my.cnf in the etc directory

     [root@localhost ~]# rm /etc/my.cnf  

4. Execute the following command to create a mysql user group ( the name is customized, mysql has a security policy, it is not recommended to use root to start, so you need to create a user specifically to start mysql )

     [root@localhost ~]# groupadd mysql

5. Execute the following command to create a user named mysql and join the mysql user group

     [root@localhost ~]# useradd -g mysql mysql 

6. Put the downloaded compressed package in the /usr/local/ directory (the file to be moved by mv /usr/local/)

7. Unzip the installation package

    [root@localhost ~]# tar -xvf mysql-5.6.34-linux-glibc2.5-x86_64.tar  

     Note: If the compressed package is: mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz, the decompression command is: tar -zxvf mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz  

8. Rename the unzipped folder to mysql

     [root@localhost local]# mv decompressed folder name mysql  

9. Create a new configuration file my.cnf under etc, and add the following code to the file:

    Of course, there is also a simple way: copy a copy of the my.cnf file directly to /etc, and then modify it.

    eg: copy a copy of my-default.cnf file under /usr/local/mysql/support-files/ to /etc

    The command is: [root@localhost support-files]# cp my-default.cnf /etc/my.cnf  

Then, configure the my.cnf file in the /etc directory

    [root@localhost support-files]# vim /etc/my.cnf  

    Note: The file encoding here must be ANSI, if you do not use Notepad to change the file encoding

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.


[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

## 设置mysql客户端默认字符集  
  
socket=/var/lib/mysql/mysql.sock
skip-name-resolve  
#设置3306端口  
port = 3306   
socket=/var/lib/mysql/mysql.sock  
## 设置mysql的安装目录  
basedir=/usr/local/mysql  
## 设置mysql数据库的数据的存放目录  
datadir=/usr/local/mysql/data  
## 允许最大连接数  
max_connections=200  
## 服务端使用的字符集默认为8比特编码的latin1字符集  
character-set-server=utf8  
## 创建新表时将使用的默认存储引擎  
default-storage-engine=INNODB  
max_allowed_packet=16M 
log-error = /var/log/mysql/error.log

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

10. Enter the directory where the mysql software is installed

    [root@localhost ~]# cd /usr/local/mysql  

    [root@localhost mysql]# chown -R mysql:mysql ./ // Modify the current directory to have the mysql user

  [root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ //Install the database (this step is very important , otherwise the following error may be reported ( error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist )

     Note: If you execute the last command above, the following problems occur:

    FATAL ERROR: please install the following Perl modules before executing  ./scripts/mysql_install_db:Data::Dumper  

    Workaround: Install the autoconf library  

    Command: yum -y install autoconf //Data:Dumper module will be installed when this package is installed

    After the installation is complete, re-execute the last command above  

11. Continue

     [root@localhost mysql]# chown -R mysql:mysql data //Modify the owner of the current data directory to the mysql user 

    To this database installation is complete!

Second, configure MySQL

1. Grant my.cnf maximum permissions

     [root@localhost ~]# chown 777 /etc/my.cnf  

Set the boot auto-start service control script:

2. Copy the startup script to the resource directory

     [root@localhost mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld 

3. Increase the execution authority of the mysqld service control script

     [root@localhost mysql]# chmod +x /etc/rc.d/init.d/mysqld  

4. Add the mysqld service to the system service

     [root@localhost mysql]# chkconfig --add mysqld

5. Check whether the mysqld service has taken effect

     [root@localhost mysql]# chkconfig --list mysqld  

     The command outputs something like the following:

     mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off 

     Indicates that the mysqld service has taken effect, and will start automatically with the system startup at run levels 2, 3, 4, and 5. You can use the service command to control the start and stop of mysql in the future.

     The commands are: service mysqld start and service mysqld stop  

6. Start mysqld

     [root@localhost mysql]# service mysqld start  

    The error in the picture below may appear when starting up here. If there is a command to execute the step of the picture again

   

7. Add the mysql bin directory to the PATH environment variable and edit the ~/.bash_profile file

     [root@localhost mysql]# vim ~/.bash_profile  

     Add the following information at the end of the file:

     export PATH=$PATH:/usr/local/mysql/bin  

then press ESC

Continue to shift key plus colon to type out => :

Next, enter wq and press Enter

Execute the following command to make the modification take effect immediately:

[root@localhost mysql]# source ~/.bash_profile  

8. Log in to mysql as root account, the default is no password

     [root@localhost mysql]# mysql -uroot -p 

Just press Enter when you want to enter the password.

    Note: Can't connect to local MySQL server through socket '/tmp/mysql.sock' error may appear here, solution:

    ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock //Make a soft connection

9. Set the root account password to root (you can also change it to the password you want)

    mysql>use mysql;  

    mysql>update user set password=password('root') where user='root' and host='localhost';  

    mysql>flush privileges;  

10. Set the remote host login, pay attention to change your username and your password below to the user and password you need to set       

    mysql>use mysql;  

    mysql>GRANT ALL PRIVILEGES ON *.* TO 'your username'@'%' IDENTIFIED BY 'your password' WITH  GRANT OPTION; 

    mysql>flush privileges;  

     Well, at this point, the installation of mysql5.6 on Centos 7 is complete. Of course, the installation of mysql on centos 6 can also be done in the same way. Next, go and write sql command with mysql installed on Centos 7!

Guess you like

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