javaEE : install mysql from source
Installation Environment
System platform: CentOS-7-x86_64
Database version: mysql-5.6.14
Steps to install mysql from source:
1. Uninstall mysql
Before installing mysql, make sure that there are no mysql related files in the linux system;
If not, then please ignore this step of the uninstallation process.
If so, uninstall mysql.
1. Check if there is myql service
rpm -qa | grep mysql
: check if there is mysql
2. If so, uninstall mysql
rpm -e mysql_libs
==" rpm -e mysql具体包名
: Normal delete mode
or
rpm -e --nodeps mysql_libs
==" rpm -e --nodeps mysql具体包名
: Force delete mode, if you use the above command to delete, it prompts other dependent files, you can use this command to delete it forcefully.
2. Install mysql
1. Install the packages required to compile the code
yum -y install make gcc-c++ cmake bison-devel ncurses-devel
. Copy and paste this instruction directly to execute.
2. Download MySQL and put it in the /usr/local directory
Omit this step
3. Unzip the mysql source package and enter the mysql directory
tar xvf mysql-5.6.14.tar.gz
: Unzip the mysql source package
cd /usr/local/mysql-5.6.14
: Go to the mysql directory
4. Compile and install
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=
/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=
1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=
1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=
/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=
1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=
all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
Copy the above command and execute it /usr/local/mysql-5.6.14
(into the mysql directory).
After the execution is complete, execute the following command:
make && make install
make && make install
: Compile and install; && means to execute the preceding instruction, and then execute the following installation instruction.
This step takes about half an hour. After the installation is complete, we need to configure our mysql.
Three, configure mysql
Before setting permissions, we must first determine whether there are mysql users and user groups in the linux system
cat /etc/passwd
: View user list
cat /etc/group
: View the list of user groups
If not, create mysql user and mysql user group
groupadd mysql
: create mysql user group
useradd -g mysql mysql
: Create a mysql user and set this user to the mysql group.
The following is the official entry: set permissions
1. Set permissions
Before setting mysql permissions, let's query the information of this file
[root@localhost local]# ls -l | grep mysql
drwxr-xr-x. 13 root root 213 May 5 00:15 mysql
chown -R mysql:mysql /usr/local/mysql
: /user/local/mysql
permission to modify
After modifying the permissions, re-check the mysql information
[root@localhost local]# ls -l | grep mysql
drwxr-xr-x. 13 mysql mysql 213 May 5 00:17 mysql
Modify permissions successfully! !
2. Initialize the configuration
Enter the installation path (and execute the following command) , execute the initialization configuration script, and create the database and tables that come with the system.
cd /usr/local/mysql
: enter the installation pathscripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
: This is an instruction
Note: Execute the above command to report an error
WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server
Reason for the error: After the minimal installation of the CentOS 7 operating system is completed, there will be a my.cnf in the /etc directory. This file needs to be renamed to another name, such as: /etc/my.cnf.bak, otherwise, This file will interfere with the correct configuration of MySQL installed from source code, resulting in failure to start. Modify the name to prevent interference:
Solution:mv /etc/my.cnf /etc/my.cnf.bak
Note: When starting the MySQL service, my.cnf will be searched in a certain order, first in the /etc directory, if not found, it will search for "$basedir/my.cnf", in this case it is /usr/local/ mysql/my.cnf, this is the default location of the configuration file of the new version of MySQL!
3. Start mysql
Add a service, copy the service script to the init.d directory, and set it to start.
Note: It is executed under /usr/local/mysql
shell
[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysql
[root@localhost mysql]# chkconfig mysql on
[root@localhost mysql]# service mysql start
Starting MySQL.. SUCCESS!
The above command is used to start mysql;
The first line: enter the /usr/local/mysql directory;
The second line: copy the service script to the init.d directory;
The third line: set the boot to start;
Fourth line: start Mysql
Fourth, use mysql
Execute the following command to change the password:
[root@localhost mysql]# cd /usr/local/mysql/bin
[root@localhost bin]# ./mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.14 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set password =password('root');
Query OK, 0 rows affected (0.01 sec)
mysql> quit //退出指令
Bye
[root@localhost bin]# ./mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@localhost bin]# ./mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.14 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases; // 查看mysql中数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.03 sec)
mysql>
The first line: cd /usr/local/mysql/bin
, enter the directory
second line:./mysql -uroot
Line 11: set password =password('root');
: Modify the database password
Line thirteen: quit
; exit
Line 17: ./mysql -u root -p
; Re-login to mysql and enter the password
Line 31: show databases;
View the database in mysql
Then on the basis of the above, create a user table and insert two pieces of data
mysql> create database myDB; //创建数据库:myDB
Query OK, 1 row affected (0.00 sec)
mysql> use myDB; //使用数据库:myDB
Database changed
mysql> show databases; //查询列出所有数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| myDB |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> create table user(id int, name varchar(32)); //创建表:user
Query OK, 0 rows affected (0.01 sec)
mysql> insert into user values(100,'tom'); //插入数据:user
Query OK, 1 row affected (0.00 sec)
mysql> insert into user values(1,'tom');
Query OK, 1 row affected (0.00 sec)
mysql> insert into user values(2,'王春兰');
Query OK, 1 row affected (0.00 sec)
mysql> select * from user; //查询user表
+------+-----------+
| id | name |
+------+-----------+
| 100 | tom |
| 1 | tom |
| 2 | 王春兰 |
+------+-----------+
3 rows in set (0.00 sec)
mysql>
Five, configure all variables
!!!important
Configure the /usr/local/mysql/bin
directory to the file of the global variable /etc/profile
;
[root@localhost mysql]# pwd
/usr/local/mysql
[root@localhost mysql]# vim /etc/profile
//... ...省略部分内容
unset i
unset -f pathmunge
JAVA_HOME=/usr/local/java
// 注意将:/usr/local/mysql/bin配置到这来,前面用:隔开
PATH=$PATH:$JAVA_HOME/bin:/usr/local/mysql/bin
CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME PATH CLASSPATH
Finally restart the service or log out and log in again
systemctl daemon-reload
: service reload
logout
:quit
At this time, we can log in to mysql from any location.
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.14 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>