Compile and install mysqld service of LAMP

abstract

1. Transfer the software packages required to install mysql to the /opt directory

Insert picture description here

Second, the installation environment depends on the package

yum -y install \
gcc \
gcc-c++ \
ncurses \				#字符终端下图形互动功能的动态库
ncurses-devel \			#ncurses开发包
bison \					#语法分析器
cmake					#mysql需要用cmake编译安装
----------------------------------------------------------------------------------------------------------
yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake

Insert picture description here

Three, configure the software module

tar zxvf mysql-5.7.17.tar.gz
tar zxvf boost_1_59_0.tar.gz
cd /opt
mv boost_1_59_0 /usr/local/boost		#重命名

cd /opt/mysql-5.7.17/
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \		#指定mysql的安装路径
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ #指定mysql进程监听套接字文件(数据库连接文件)的存储路径
-DSYSCONFDIR=/etc \                             #指定配置文件的存储路径
-DSYSTEMD_PID_DIR=/usr/local/mysql \            #指定进程文件的存储路径
-DDEFAULT_CHARSET=utf8  \                       #指定默认使用的字符集编码,如 utf8
-DDEFAULT_COLLATION=utf8_general_ci \			#指定默认使用的字符集校对规则
-DWITH_EXTRA_CHARSETS=all \						#指定支持其他字符集编码
-DWITH_INNOBASE_STORAGE_ENGINE=1 \              #安装INNOBASE存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \               #安装ARCHIVE存储引擎 
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \             #安装BLACKHOLE存储引擎 
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \            #安装FEDERATED存储引擎 
-DMYSQL_DATADIR=/usr/local/mysql/data \         #指定数据库文件的存储路径
-DWITH_BOOST=/usr/local/boost \          #指定boost的路径,若使用mysql-boost集成包安装则-DWITH_BOOST=boost
-DWITH_SYSTEMD=1								#生成便于systemctl管理的文件

存储引擎选项:
MYISAM,MERGE,MEMORY,和CSV引擎是默认编译到服务器中,并不需要明确地安装。
静态编译一个存储引擎到服务器,使用-DWITH_engine_STORAGE_ENGINE= 1
可用的存储引擎值有:ARCHIVE, BLACKHOLE, EXAMPLE, FEDERATED, INNOBASE (InnoDB), PARTITION (partitioning support), 和PERFSCHEMA (Performance Schema)
----------------------------------------------------------------------------------------------------------
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=/usr/local/boost \
-DWITH_SYSTEMD=1

注意:如果在CMAKE的过程中有报错,当报错解决后,需要把源码目录中的CMakeCache.txt文件删除,然后再重新CMAKE,否则错误依旧
Insert picture description here


Insert picture description here


Insert picture description here

Four, compile and install

make -j 4 && make install
Insert picture description here


Insert picture description here

Five, create a Mysql user

useradd -M -s /sbin/nologin  mysql
Insert picture description here

Six, modify the mysql configuration file

vim /etc/my.cnf					#删除原配置项,再重新添加下面内容					                       	  
[client]                        #客户端设置
port = 3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
auto-rehash

[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES		
Insert picture description here


Insert picture description here

Seven, change the mysql installation directory and the owner group of the configuration file

chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf
Insert picture description here


Insert picture description here

Eight, set the path environment variable

echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile	
source /etc/profile
Insert picture description here

Nine, initialize the database

cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \				#生成初始化密码为空
--user=mysql \                      #指定管理用户
--basedir=/usr/local/mysql \        #指定数据库的安装目录
--datadir=/usr/local/mysql/data		#指定数据库文件的存储路径
----------------------------------------------------------------------------------------------------------
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

Insert picture description here

Ten, add mysqld system service

cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/		#用于systemctl服务管理
systemctl daemon-reload         #刷新识别     
systemctl start mysqld.service  #开启服务
systemctl enable mysqld         #开机自启动
netstat -anpt | grep 3306       #查看端口
Insert picture description here


Insert picture description here

Eleven, modify the mysql login password

mysqladmin -u root -p password "abc123" 	#给root账号设置密码为abc123,提示输入的是原始密码(为空)
Insert picture description here


Insert picture description here

12. Authorize remote login

mysql -u root -p
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
#授予root用户可以在所有终端远程登录,使用的密码是abc123,并对所有数据库和所有表有操作权限

show databases;			#查看当前已有的数据库
Insert picture description here