MySQL (Mariadb) installation and use under Linux

Due to the needs of work, various kinds of information are often counted, so there is a need to develop and deploy an enterprise information management system. Considering that it is only the needs of this department and has human resources to support the development, it is developed by ourselves. The hardware uses the old hardware that the employees have eliminated, the software plans to use the LAMP combination, and other parts that need to be developed are also completed by ourselves. Simply put, it doesn't cost a cent to get the job done.

The operating system adopts CentOS-7.4, and MySQL adopts the matching mariadb-5.5.56. In fact, I am a novice myself, and I have never done this work, so I also work overtime during the Chinese New Year, and strive to be able to deploy and use it on the machine of the unit immediately after the year. Now, write down this process in case you need it.

The installation of CentOS 7 will not be described in detail, just choose the minimum installation, and the rest of the software can be installed after the operating system is installed.

The reason why MySQL chooses mariadb instead of native MySQL is because within our business scope, mariadb can fully meet the needs, and it comes with CentOS 7, which should solve many compatibility problems. We know from Baidu Encyclopedia that the MariaDB database management system is a branch of MySQL, mainly maintained by the open source community and licensed under the GPL license. MariaDB is designed to be fully compatible with MySQL, including API and command line, making it an easy replacement for MySQL. For the above reasons, we chose mariadb.

  1. install mariadb

      # yum install -y mariadb mariadb-server
      # systemctl list-unit-files | grep mariadb
      mariadb.service                             disabled
      # systemctl enable mariadb.service
      # systemctl start  mariadb.service
    

    Root privileges are required to install, and mariadb.service must be enabled and started after installation.

  2. change the password

      $ mysql
      MariaDB [(none)]> set password = password('coder');         
      ERROR 1133 (42000): Can't find any matching row in the user table  
      MariaDB [(none)]> flush privileges;  
      ERROR 1227 (42000): Access denied; you need (at least one of) the RELOAD  
      privilege(s) for this operation
      #  mysql
      MariaDB [(none)]> flush privileges;     
      Query OK, 0 rows affected (0.00 sec)    
      MariaDB [(none)]> set password = password('coder');   
      Query OK, 0 rows affected (0.00 sec)
      $ mysql
      MariaDB [(none)]> set password = password('coder');         
      ERROR 1133 (42000): Can't find any matching row in the user table  
      $ mysql -u root -p
      Enter password:
      MariaDB [(none)]> 
    

    After installing mariadb, exit the root authority, start mysql at this time, it can start normally, but the password cannot be set. Check the mysql error code description, found that the mysql error 1133 is that the database user name does not exist; check the online solution, many prompts to execute the FLUSH PRIVILEGES; command, but still can not be successfully executed. However, the FLUSH PRIVILEGES; and set password commands can be executed correctly from the root user
    . Later, the table of the system database is checked. In the user permission table, there is only the root user, and there are no ordinary users in the Linux system. Therefore, ordinary users cannot execute the commands after connecting to the database. Affecting commands.

    If you need to connect to the database from an ordinary user of the Linux system in daily applications, you need to use the mysql -u root -p command and enter the set database root user password, not the Linux system root user password, and the -p option must be used, otherwise will prompt:

    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

    At this point, it is still not possible to enter the database.

  3. Database Security Settings
    MariaDB recommends that all production MariaDB servers run this script.

      # mysql_secure_installation
    

    (1). Set the password, since it has been set before, there is no need to change it here;
    (2). Delete the anonymous account of the database, it is precisely because of this anonymous account that we can log in with an empty identity (only enter mysql), but Most other operations cannot be completed. The anonymous account should be deleted here;
    (3). The root user is not allowed to log in from the network, the root user is only allowed to log in from 'localhost', and Yes should be selected here;
    (4). Delete the test database, which is only used for testing , should be deleted before transferring to the production environment, select delete here;
    (5). Reload the permission table to make the changes take effect, select yes here;

  4. Database character set setting
    Since Chinese information needs to be input and output in the database, it is necessary to do the setting to support Chinese.

    MariaDB [(none)]> show variables like 'char%';
    

    By default, most of the various character sets output by this command are latin1, which does not support Chinese. At this time, if you create a database, table, and write Chinese information. The insert operation can be successfully executed, but when querying, the Chinese information will be displayed in the form of "???", which is completely unreadable.

    Change method:

    In file /etc/my.cnf.d/client.cnf, after the '[client]' zone, add the next line as follows:
    [client]
    default-character-set=utf8
    in file /etc/my.cnf.d/server.cnf, after the '[server]' zone, add the next line as follows:
    [server]
    character-set-server=utf8
    Restart the mariadb.service
    systemctl restart mariadb.service

    After completing these preparations, you can use Chinese in the mariadb database. If a remote connection is used, the code used by the remote connection tool must be consistent with the database.

  5. Create database table

      MariaDB [(none)]>  create database worker_info;  
      MariaDB [(none)]>  use worker_info;  
      MariaDB [(none)]>  create table wid_name ( wid  int unsigned not null,  name  varchar(10) not null, 
     primary key (wid) );  
    
  6. Execute the insert command to insert data

      MariaDB [(worker_info)]>  insert into wid_name (wid, name ) values ( 2018101001, '今天' );
    
    

The installation of the database basically ends here.

Guess you like

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