Introduction to MySQL database and common functions (1)

1. Quotes

With the advancement of science and technology, the development of the current society can be said to be changing with each passing day. The problem is the rapid growth of the amount of data information. The MySQL database was born in this environment. MySQL is a popular open source relational database management system that is widely used in various application and website development. Its powerful features and flexibility, as well as large community support, make it the database solution of choice for many developers and organizations.

2. Installation of MySQL database

Before learning the MySQL database, we must install the MySQL database. There are three main MySQL installation methods: source code installation, rpm installation and binary installation. Next, let’s introduce the process of rpm installation and source code compilation and installation.

1. rpm installation (based on Tsinghua University source installation)

First cd to the /etc/yum.repos.d/ directory

cd /etc/yum.repos.d/

 Create and open the yum.repo file with vim and paste the following content into the file

[mysql]
name=mysql
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-x86_64/
gpgcheck=0
enabled=1
gpgkey=https://mirrors.ustc.edu.cn/mysql-repo/RPM-GPG-KEY-mysql

Just use the yun command to download it directly.

yum -y install mysql-community-server

After the download is completed, you can initialize the MySQL database (initialize during the process of starting the MySQL database)

systemctl start mysqld 
systemctl disable mysqld

 After the initialization is completed, we can go to the /var/lib/mysql/* directory to check whether the file has been initialized.

After completing the above operations, we can find the initial password in the /var/log/mysql.log file

grep password /var/log/mysql.log

Since logging in to the MySQL database using the initial password cannot perform read and write operations, we need to change the password before logging in.

mysqladmin -uroot -p'初始密码' password '修改密码'

Because the MySQL database has complexity requirements for passwords, we can set weak passwords for it in the experimental environment. However, it is not recommended to turn it on in the production environment. The following is the operation to turn off password restrictions:

vim to the /etc/my.conf file and add the line "validate_password=off" to the last line.

At this point our rpm installation of the database is complete.

2. Source code compilation and installation

Before installing the source code, we need to clean up the installation environment of the machine.

yum erase mariadb mariadb-server mariadb-libs mariadb-devel -y
userdel -r mysql
rm -rf /etc/my*
rm -rf /var/lib/mysql

After cleaning up the environment, we can proceed with the formal installation.

Before installation, we need to create a user to manage and log in to mysql

useradd -r mysql -M -s /sbin/nologin

 After the account is successfully created, we can install the compilation tools and dependencies required for the MySQL database.

yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make cmake

After installing the dependencies, we need to create the mysql directory

mkdir -p /usr/local/{data,mysql,log}

Unzip the mysql installation package

tar -vih  mysql-boost-5.7.37.tar.gz -C /usr/local

Then cd to the /usr/local/mysql-boots-5.7.37/ directory to compile

cmake . \
-DWITH_BOOST=boost/boost_1_59_0/ \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DINSTALL_MANDIR=/usr/share/man \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1

 MySQL database compilation and installation can take many specified parameters. I will list them in detail below.

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \   安装目录
-DSYSCONFDIR=/etc \   配置文件存放 (默认可以不安装配置文件)
-DMYSQL_DATADIR=/usr/local/mysql/data \   数据目录   错误日志文件也会在这个目录
-DINSTALL_MANDIR=/usr/share/man \     帮助文档 
-DMYSQL_TCP_PORT=3306 \     默认端口
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \  sock文件位置,用来做网络通信的,客户端连接服务器的时候用
-DDEFAULT_CHARSET=utf8 \    默认字符集。字符集的支持,可以调
-DEXTRA_CHARSETS=all \   扩展的字符集支持所有的
-DDEFAULT_COLLATION=utf8_general_ci \  支持的
-DWITH_READLINE=1 \    上下翻历史命令
-DWITH_SSL=system \    使用私钥和证书登陆(公钥) 可以加密。 适用与长连接。坏处:速度慢
-DWITH_EMBEDDED_SERVER=1 \   支持嵌入式数据库
-DENABLED_LOCAL_INFILE=1 \    从本地倒入数据,不是备份和恢复。
-DWITH_INNOBASE_STORAGE_ENGINE=1  默认的存储引擎,支持外键

Use make -j4 && make install to compile and install

make -j4 && make install

 If an installation error occurs and you want to reinstall: No need to re-decompress, just delete the cache file CMakeCache.txt in the installation directory.

After the above operation is successful, our compilation and installation is completed

3. Conclusion

In this blog, we introduce the MySQL installation process and some common configurations and operations. MySQL is a powerful relational database management system that is widely used in various application and website development.

As an open source database management system, MySQL has extensive community support and rich documentation resources. If you encounter problems while using MySQL, you can solve them by consulting official documentation, participating in community discussions, or seeking professional technical support.

In summary, MySQL is a powerful and easy-to-use database management system that provides developers and data professionals with rich functionality and flexibility. I hope this blog can help you get started with MySQL quickly and play a role in your projects.

If you have other questions about MySQL or need more in-depth study, it is recommended that you continue to explore the various functions and advanced usage of MySQL. I wish you success in using MySQL!

Guess you like

Origin blog.csdn.net/XX_HK/article/details/134258953