Ubuntu16.04 compile and install the mysql-boost-5.7.21 and compile it into php extension test and use,
My previous articles have transformed four types of tools (Verification Code, Image Upload, image processing, and paging) in the Custom MVC Framework. Next, we need to transform the model class, the model class must be connected to the database. Because my Ubuntu Linux is a bare-metal php (currently only one gd extension is compiled), you need to compile and install mysql and compile it into an extension. Here, I chose the source code package with boost in version 5.7. After one night, I took some notes while performing the exercises... Too many configurations.
I. databases to be prepared
1. cmake Compiler
sudo apt-get install cmake
2. bison (C/C ++ syntax analyzer in Linux)
sudo apt-get install bison
3. ncurses library. If you have studied programming game development in Linux, you may have heard of or used this library.
sudo apt-get install libncurses5-dev
4. gcc compiler, which comes with ubuntu16.04
5. Boost 1.59.0. After downloading this library, compile and install it.
wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
tar -zxvf boost_1_59_0.tar.gz -C /usr/local/
> Go to the decompressed source code directory.
cd /usr/localcd boost_1_59_0/
If you do not switch to the boost_000059_0 directory, remember to modify the user, group, and permissions. [sudo chmod 755 boost_000059_0 /]
> Then run the bootstrap. sh script and set relevant parameters.
./bootstrap.sh --with-libraries=all --with-toolset=gcc
-- With-libraries specifies which boost libraries to compile. If all is used, all are compiled.
-- With-toolset specifies the compiler used for compilation.
> Compile and install boost
. /B2 // compile boost sudo. /b2 install // install the generated Library to the/usr/local/lib directory. The default header file is under the/usr/local/include/boost directory.
> Check whether the boost library is successfully installed.
ghostwu@ghostwu:~/php/test$ cat boost_test.c #include <string> #include <iostream> #include <boost/version.hpp> #include <boost/timer.hpp> using namespace std; int main() { boost::timer t; cout << "max timespan: " << t.elapsed_max() / 3600 << "h" << endl; cout << "min timespan: " << t.elapsed_min() << "s" << endl; cout << "now time elapsed: " << t.elapsed() << "s" << endl; cout << "boost version" << BOOST_VERSION <<endl; cout << "boost lib version" << BOOST_LIB_VERSION <<endl; return 0; }
Compile and execute:
ghostwu@ghostwu:~/php/test$ g++ boost_test.c -o boost_testghostwu@ghostwu:~/php/test$ ./boost_test max timespan: 2.56205e+09hmin timespan: 1e-06snow time elapsed: 4.7e-05sboost version105900boost lib version1_59
Show boost version 1.59
2. Prepare for mysql installation.
1. Add a Mysql user
groupadd mysqluseradd -r -g mysql mysql
2. Create the Directory and data file directory of the Mysql installation program and the user and group
sudo mkdir /usr/local/mysql57sudo mkdir ~/mysql57_datachown -R mysql.mysql ~/mysql57_data/sudo chown -R mysql.mysql /usr/local/mysql57/
3. Configure mysql
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql57 \-DMYSQL_DATADIR=/home/ghostwu/mysql57_data \-DWITH_BOOST=/usr/local/boost_1_59_0 \-DSYSCONFDIR=/etc \-DEXTRA_CHARSETS=all
Use cmake to configure mysql pre-compilation parameters:
-DCMAKE_INSTALL_PREFIX: installation path
-DMYSQL_DATADIR: data storage directory
-DWITH_BOOST: boost source code path
-DSYSCONFDIR: my. cnf configuration file directory
-DEFAULT_CHARSET: Default character encoding of the database
-DDEFAULT_COLLATION: Default sorting rule
-DENABLED_LOCAL_INFILE: allows you to import data from this file.
-DEXTRA_CHARSETS: Install all character sets
For more pre-compiled configuration parameters, see mysql official documentation Description: http://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html#cmake-general-options
> Make & sudo make install
> Initialize the database
sudo ./bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql57 --datadir=/home/ghostwu/mysql57_data
Temporary password:
cat ~/.mysql_secret
Copy Service Startup files
sudo cp support-files/mysql.server /etc/init.d/mysqld
Create my. cnf in The/etc directory
[client]port=3306socket=/var/run/mysqld/mysqld.sock[mysqld]port=3306socket=/var/run/mysqld/mysqld.sockpid-file=/home/ghostwu/mysql57_data/mysql.pidbasedir=/usr/local/mysql57datadir=/home/ghostwu/mysql57_data
Sudo mkdir-p/var/run/mysqld
Sudo chown mysql: mysql/var/run/mysqld
Start the service
/etc/init.d/mysqld start|stop|restart
3. What should I do if the temporary password is too complex to log on? Change Password again
> Disable the mysql service first.
/etc/init.d/mysqld stop
> Switch the path to/usr/local/mysql57/bin and run
mysqld_safe --skip-grant-tables &
> Mysql-p connection, with a blank password
Then update the password:
>update mysql.user set authentication_string=password('abc123') where user='root' and Host = 'localhost';
There is no Password field in the user table of the new mysql database. Instead, the encrypted user Password is stored in the authentication_string field.
>flush privileges;
> Quit
Then restart the server and use the abc123 password to log on.
4. Compile mysql into php Extension
When mysql extension is not available, an error is reported when php calls mysql_connect and mysql extension cannot be found using the-m parameter.
ghostwu@ghostwu:~/php/senior_php$ /usr/local/php54/bin/php -f db.php PHP Fatal error: Call to undefined function mysql_connect() in /home/ghostwu/php/senior_php/db.php on line 2ghostwu@ghostwu:~/php/senior_php$ /usr/local/php54/bin/php -m | grep mysqlghostwu@ghostwu:~/php/senior_php$ cat db.php<?php $link = mysql_connect( "localhost", "root", "abc123" ); var_dump( $link );?>ghostwu@ghostwu:~/php/senior_php$
Extension installation steps:
1 /usr/local/php54/bin/phpize2 ./configure --with-php-config=/usr/local/php54/bin/php-config --with-mysql=/usr/local/mysql573 make4 sudo make install
Then enable mysql. so extension in/usr/local/php54/lib/php. ini: extension = mysql. so
Execute the php (db. php) file again. If the following error occurs:
PHP Warning: mysql_connect(): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /home/ghostwu/php/senior_php/db.php on line 2
Please tell the socket file path of php mysql in the configuration below the php. ini file
ghostwu@ghostwu:~/php/senior_php$ cat /usr/local/php54/lib/php.ini | grep mysql.default_socketpdo_mysql.default_socket=mysql.default_socket = /var/run/mysqld/mysqld.sock
Finally, the database connection is successful:
ghostwu@ghostwu:~/php/senior_php$ /usr/local/php54/bin/php -f db.php resource(4) of type (mysql link)
Various reference articles:
Http://www.jb51.net/article/101767.htm
Https://www.cnblogs.com/Lam7/p/6090989.html
Http://blog.csdn.net/upHailin/article/details/69264389? LocationNum = 11 & fps = 1
Http://blog.csdn.net/u011573853/article/details/52682256
Https://www.cnblogs.com/forest-wow/p/6642025.html
Summary:
An Important Skill: Reading logs, reading logs, and mastering debugging methods. My mysql cannot be started several times, but all rely on error logs to locate the problem !!!