Mysql is a commonly used database and is often used in daily development. If you want to do your job well, you must first sharpen your tools. This article specifically explains how to install Mysql under centos (similar to other Linux distributions). The first materials to prepare: Mysql, I use 64-bit here, portal:
http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.15-linux-glibc2.5- x86_64.tar.gz.
1. What are the installation methods?
A. Download the binary installation package of Mysql and configure your personal configuration file for use.
B. Use Linux package management tools for quick installation, such as Ubuntu’s apt-get, Centos’ yum, and rpm.
Refer to blog post 1: http://www.tuicool.com/articles/2EfM7f .
Reference blog post 2: http://www.bitsCN.com/article/94284.htm
Reference blog post 3: http://www.bitsCN.com/article/94287.htm
C. Source code compilation and installation, Reference blog post: http://www.bitsCN.com/article/94291.htm
2. The advantages and disadvantages of the above installation method.
A. It is simple and convenient. You can DIY the configuration file yourself. This method is highly recommended.
B. Simple, convenient and fast, but often not free enough, and may not be able to use the latest Mysql version, so it is suitable for novices.
C. The source code installation is more complicated. If you are interested, you can try it. If you want to use it quickly, it is recommended to install it in the first two ways
3. Install binary Mysql
1. Download the installation package to the root directory.
Command: cd ~/
wget http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz
2. Create a new folder mysql in the opt directory. Command: mkdir /opt/mysql
3. Unzip the downloaded installation package to /opt/mysql.
Command: cd /opt/mysql
,,,,,,,,,,,,,,,,,,,,,, ln -s /opt/mysql/mysql-5.7.15-linux-glibc2.5-x86_64/ /usr/local/mysql
[Note] Create a soft link to facilitate your package management. : Test whether it is successful: 是否 command:LS -LH /USR /LOCAL /
5. Add mysql user and MySQL user group command:GROUPADD mysql
useradd -g mysql -S / sbin/nologin -d /usr/local/mysql -M mysql
6. Configure global variablesCommand:
vim /etc/profileAdd the following code to the last line of the profile file:
PATH=$PATH :/usr/local/mysql/binAs shown in the picture:
Then save and exit, use the command: source /etc/profile
7. Configuration fileMysql needs to be configured. Generally, if you do not have your own defined configuration file, Mysql will first go to the etc directory to find the my.cnf file as the configuration file. If not, it will continue to the etc directory under the /usr/local/mysql path to find it. Here I am using my own configuration file.
Use the mv command or cp command to get this file to the root directory. 8. Create a folder to store Mysql data, logs, and sockets
Command:
mkdir -p /data/mysql/mysql3306/{data,logs,tmp}After the creation is completed, we put the steps in step 6 Copy the configuration file to the directory mysql3306 in the path /data/mysql. If you need a configuration file, please leave your email in the comments below and I will send it to you.
As shown in the picture:
Command:
cp ~/mysql3306.cnf /data/mysql/mysql3306/
[Note] This command is used to create directories in batches, where the data folder is located with the root file Clip a layer. You can customize this path. Since I have already created 3306, I will take screenshot 3308 as an example. :) Okay, if you made it this far. So congratulations, you are more than halfway done. All that's left is to initialize, start and log in to Mysql.9. Initialize Mysql:
Command: mysqld --initialize datadir=/data/mysql/mysql3306/data/
[Note] This command process is successful if there is no error. If there is an error, you can go to /data/mysql/mysql3306/data and open error.log to check the cause of the error. If a warning occurs, you can ignore it.
As shown in the picture:
[Note] After this command is successfully executed, it will give a temporary login password for the root account at the end (red arrow in the picture). The default password for root login in the previous Mysql version was an empty password, but this has been changed. So you need to record this temporary password and save it for later use when logging in in step 12. 10. Authorize the relevant directory for the mysql user in step 4 Command: 11. Start Mysql: Command: mysqld --defauslts-file=/data/mysql/mysql3306/mysql3 306 .cnf & As shown in the picture: [Note] The '&' after the command allows Mysql to run in the background. If no error occurs, it can start successfully. As shown in the picture: 12. Log in to Mysql: Command: mysql --socket=/tmp/mysql330 6.sock -uroot -p [Note] Enter the password recorded in step 9. As shown in the picture After successfully logging in, you will find that you cannot use "show databases;" to view the database. mysqladmin --socket=/tmp/mysql3306.sock -uroot -p shutdown Possible FAQ: Q: I believe you have learned how to install Mysql. So what then? A: Haha, you can check out "Must Know Mysql" to play and consolidate your Mysql knowledge and skills. Q: Mysql startup and login commands are more complicated and cumbersome than yum installation. What should I do? Summary:chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /opt/mysql
chown -R mysql:mysql /data/mysql
[Check whether it started successfully]
Command: ps axu | grep mysqld
We need to make some modifications here, log in to Mysql and type the command.
Step 1: To facilitate subsequent login, we change the root account password to 123456.
Command: alter user root@localhost identified by '123456';
Step 2: Release permissions
Command: flush privileges; databases; "You will find that you can already view the database.
13. Shut down mysqld: