安装mysql前的准备:
一、安装依赖的库:
yum install gcc-c++ ncurses-devel perl-Data-Dumper python-devel openssl openssl-devel
二、 安装cmake(因为mysql5.7的编译由cmake来实现)
安装cmake:
https://cmake.org/files/v2.8/cmake-2.8.8.tar.gz
cd cmake-2.8.8/
预编译和安装:
./bootstrap
Make && make install
三、 安装boost:
下载源码包:
wget http://jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.zip
解压
unzip boost_1_59_0.zip
mv boost_1_59_0.zip /usr/local/boots
cd /usr/local/boost
编译安装:
./bootstrap.sh
./b2
sudo ./b2 install
配置环境变量:
vim /etc/profile
BOOST_ROOT=/usr/local/boost
BOOST_LIB=/usr/local/boost/stage/lib
BOOST_INCLUDE=/usr/local/include/boost
export BOOST _ROOT BOOST _INCLUDE BOOST _LIB
source /etc/profile
四、编译安装mysql:
下载源码包:
wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.15.tar.gz
tar zxvf mysql-5.7.15.tar.gz
cd mysql-5.7.15/
编译和安装(下面编译参数请手动一行一行复制)
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_DATADIR=/mydata/data
-DSYSCONFDIR=/etc
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_READLINE=1
-DWITH_SSL=system
-DWITH_ZLIB=system
-DWITH_LIBWRAP=0
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost
备注:mysql5.7的编译需指定boost,即:-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost
2、添加mysql用户和组:
groupadd -r mysql
useradd -g mysql -r -d /mydata/data mysql
初始化数据库
bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/data # Before MySQL 5.6
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/data # MySQL 5.7 and up
cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
3、修改配置文件:
vim /etc/my.cnf
[client]
port=3306
socket=/tmp/mysql.sock
default-character-set=utf8
[mysqld]
basedir =/usr/local/mysql
datadir =/mydata/data
port =3306
server_id =1
socket =/tmp/mysql.sock
pid-file=/mydata/data/mysql.pid
bind-address=localhost
#skip-grant-tables
4、启动mysql服务
service mysqld start
5、为了直接使用,加到环境变量里
vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
:wq 保存退出
执行这个脚本:
. /etc/profile.d/mysql.sh
[root@localhost ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
6、此时需要修改root用户密码,在初始化数据库是会随机生成一个密码
方法如下:
1)、vim /etc/my.cnf #编辑文件,找到[mysqld],在下面添加一行skip-grant-tables
[mysqld]
skip-grant-tables
:wq! #保存退出
service mysqld restart #重启MySQL服务
2)、进入MySQL控制台
mysql -uroot -p #直接按回车,这时不需要输入root密码。
3)、修改root密码
update mysql.user set password=password('123456') where User="root" and Host="localhost";
flush privileges; #刷新系统授权表
grant all on *.* to 'root'@'localhost' identified by '123456' with grant option;
4)、取消/etc/my.cnf中的skip-grant-tables
vi /etc/my.cnf 编辑文件,找到[mysqld],删除skip-grant-tables这一行
:wq! #保存退出
5)、重启mysql
service mysqld restart #重启mysql,这个时候mysql的root密码已经修改为123456
6)、进入mysql控制台
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 22 Server version: 5.7.15 Source distribution
Copyright (c) 2000, 2016, 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>
至此mysql5,7安装好了
7、启用远程连接:
mysql数据库远程访问设置方法
1、修改localhost
更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改成"%"
mysql>use mysql;
mysql>update user set host = '%' where user = 'root';
mysql>select host, user from user;
mysql>FLUSH PRIVILEGES;
我自己使用 sqldeveloper远程连接mysql
需要下载mysql-connector-java-5.0.4-bin.jar包,并将其放到sqldeveloper下的jlib目录下
打开sqldeveloper,菜单栏-工具-首选项-数据库-第三方JDBC驱动程序,点击添加条目,在sqldeveloperjlib目录下,
选择mysql-connector-java-5.0.4-bin.jar,点击确定,如下图所示:可以连接mysql(关闭linux防火墙哦,不然连不上)