mysql - install mysql in a linux environment

1. Preparations

Prepare a virtual machine         that has not installed mysql and mariadb , or create a brand new virtual machine. Note : This installation of mysql is carried out in centos7.

        Prepare the mysql installation package for the linux environment, which can be downloaded from the official website. You can download version 5.7 of mysql from this page ( MySQL :: Download MySQL Community Server (Archived Versions) ).


 2. Use the command line to operate

        Not much to say, let's start the operation directly

        1. Transfer the downloaded installation package to the Linux system.

        2. Install the required tools and resolve software dependencies

yum install cmake ncurses-devel gcc gcc-c++ vim lsof bzip2 openssl-devel ncurses-compat-libs net-tools -y

        3. Decompress the binary installation package of mysql

tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz

        4. Move the decompressed mysql file to /usr/local and rename it to mysql

mv mysql-5.7.37-linux-glibc2.12-x86_64 /usr/local/mysql

        5. Create a new group mysql

groupadd mysql

        6. Create a new user mysql , the shell of this user is /bin/false , belongs to the group mysql, -r means this is a system user (system)

useradd -r -g mysql -s /bin/false mysql

        7. Turn off the firewall service

service firewalld stop 

        8. Set the firewall not to start when booting

systemctl disable firewalld

        9. Temporarily shut down selinux

setenforce 0

        10. Permanently shut down selinux

sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config

        11. Create a new directory for storing data, that is, the mysql warehouse , -p means recursively create the directory

mkdir /data/mysql -p

        12. The permission to modify the /data/mysql directory belongs to the mysql user and the mysql group, so that the mysql process started by mysql can read and write this directory

chown mysql:mysql /data/mysql/

        13. Set this directory to only allow the mysql user to read and write, the mysql group to read and write, and other users do not have any permissions

chmod 750 /data/mysql/

        14. Enter the /usr/local/mysql/bin/ directory

cd /usr/local/mysql/bin/

        15. Initialize mysql. In this step, pay attention to saving the generated password , that is, the last line can be copied and saved to avoid loss.

Similar to: 2023-07-06T02:47:58.457334Z 1 [Note] A temporary password is generated for root@localhost: HEJ)g&2pzus?

./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql

        16. Set mysql to support ssl login

./mysql_ssl_rsa_setup --datadir=/data/mysql/

        17. Modify the PATH variable and add the mysql bin directory. Temporarily modify the value of the PATH variable

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

        18. Permanently modify the value of the PATH variable, and restart the linux system to take effect

echo 'PATH=/usr/local/mysql/bin:$PATH' >>/root/.bashrc

        19. Copy the mysql.server file in support-files to the /etc/init.d/ directory called mysqld

cp ../support-files/mysql.server /etc/init.d/mysqld

        20. Modify the value of the datadir directory in the /etc/init.d/mysqld script file

sed -i '70c datadir=/data/mysql' /etc/init.d/mysqld

        21. Generate the configuration file of /etc/my.cnf , just copy all of them directly

cat >/etc/my.cnf <<EOF

[mysqld_safe]

[client]

socket=/data/mysql/mysql.sock
 

[mysqld]

socket=/data/mysql/mysql.sock

port = 3306

open_files_limit = 8192

innodb_buffer_pool_size = 512M

character-set-server=utf8

[mysql]

auto-rehash

prompt=\\u@\\d \\R:\\m mysql>

EOF

        22. Modify the number of kernel open files

decrease in 1000000

        23. Set the configuration to take effect when starting up

echo "ulimit -n 1000000" >>/etc/rc.local

chmod +x /etc/rc.d/rc.local

24. Add         mysqld to the service management list in the linux system

chkconfig --add mysqld

        25. Set the mysqld service to start at boot

chkconfig mysqld on

        26. Start the mysqld process , and configure it if it starts successfully

service mysqld start

        27. Use a randomly generated password to log in to mysql. The password I generated randomly is already on it, namely HEJ)g&2pzus?

mysql -uroot -p'HEJ)g&2pzus?'

        28. Modify the password of mysql, remember to write a semicolon when writing a statement in mysql . If the password does not require high security, set it to be easy to remember. I set it to study hard and make progress every day (hhxxttxsoy)

set password='hhxxttxsoy';

        29. Check whether the configuration is successful . If the database in mysql appears, it means the configuration is successful.

show databases;


3. Use script operation

        According to the above command line operation, we can also write a script, which can achieve the effect of one-click installation .

        Write a script according to the above command line, the steps that need to be modified are:

        1. In step 15 , remember to redirect the output to the specified file in order to find the randomly generated password, ie

./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql &>passwd.txt

        2. Add a step after step 16 , that is, add this step after setting mysql to support ssl login to extract the generated password

tem_passwd=$(cat passwd.txt |grep "temporary"|awk '{print $NF}')

#$NF means the last field

# abc=$(command) Execute the command first, and then assign the result to abc

        3. Change steps 24 and 25 to the following

/sbin/chkconfig --add mysqld

/sbin/chkconfig mysqld on

        4. Since the script automatically modifies the temporary password, all modifications that need to be modified are combined with steps 27 and 28

mysql -uroot -p$tem_passwd --connect-expired-password -e "set password='hhxxttxsoy';"

        Finally, by modifying and integrating the commands, write a script and execute it. The premise is that there must be a mysql compressed package that is consistent with the mysql version number in the script. Make sure that the compressed package and the script are in the same directory.


        The above is the basic content about installing mysql under the linux system. If there is any error, please correct me .

Guess you like

Origin blog.csdn.net/m0_53891399/article/details/131586423