写在前面:
在使用源码来安装mysql之前,我建议使用Ubuntu 源来安装mysql。
安装方法: sudo apt-get install mysql_server_5.6
输入mysql root用户的密码即可
当然,这里你选择安装什么mysql需要根据实际来,如果你还没有安装mysql,执行mysql命令,看提示。
使用源码安装:
安装的mysql数据库是:mysql-5.6.4-m7,这个版本的数据库要用cmake工具安装。
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的automake
cmake下载地址:http://www.cmake.org/cmake/resources/software.html
1、安装cmake(最好使用root用户)
root@ubuntu:~$ cd cmake-3.2.1/
root@ubuntu:~/cmake-3.2.1$ ./bootstrap
root@ubuntu:~/cmake-3.2.1$ make
root@ubuntu:~/cmake-3.2.1$ make install
如果要更新cmake 只需进入相关目录,执行 cmake
(ps:我不得不提一下,cmake的版本不是越高越好的,因为有些函数可能在低版本有效,在高版本的时候就已经失效了)
注意:在安装cmake时执行 ./bootstrap如果没有安装g++会出现以下错误
CMake 3.2.1, Copyright 2000-2015 Kitware, Inc.
C compiler on this system is: cc
---------------------------------------------
Error when bootstrapping CMake:
Cannot find appropriate C++ compiler on this system.
Please specify one using environment variable CXX.
See cmake_bootstrap.log for compilers attempted.
---------------------------------------------
Log of errors: /opt/mysql/cmake-3.2.1/Bootstrap.cmk/cmake_bootstrap.log
解决办法:先安装gcc-c++
安装GCC
2、安装mysql(最好依旧使用root用户)
root@ubuntu:~$ cd mysql-5.6.4-m7/
root@ubuntu:~$ cmake .
root@ubuntu:~$ make
root@ubuntu:~$ make install
(将mysql编译安装了一遍,由日志可知安装文件是在 /usr/local/mysql 下)
执行cmake . 可能 会 出现下面问题:
CMake Error at cmake/readline.cmake:83 (MESSAGE):
Curses library not found. Please install appropriate package,
remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first):
cmake/readline.cmake:136 (FIND_CURSES)
cmake/readline.cmake:226 (MYSQL_USE_BUNDLED_LIBEDIT)
CMakeLists.txt:262 (MYSQL_CHECK_READLINE)
-- Configuring incomplete, errors occurred!
See also "/opt/mysql/mysql-5.6.4-m7/CMakeFiles/CMakeOutput.log".
See also "/opt/mysql/mysql-5.6.4-m7/CMakeFiles/CMakeError.log".
解决办法:按照上面内容提示的方法进行修改
若是Debian/Ubuntu安装libncurses5-dev 包
若是redhat 安装ncurses-devel这个包即可。
P s:我安装mysql的时候就使用的mysql用户,删了文件重新执行的时候发现cmake变成一个文件夹了,于是……我就删了,重新解压了一遍mysql
3、安装默认的数据库(使用root吧)
root@ubuntu:/usr/local/mysql# cd bin
root@ubuntu:/usr/local/mysql/bin# ls
innochecksum myisampack mysqladmin mysql_config mysqldump mysqlimport mysqlslap mysql_zap
msql2mysql my_print_defaults mysqlbinlog mysql_convert_table_format mysqldumpslow mysql_plugin mysqltest perror
myisamchk mysql mysqlbug mysqld mysql_find_rows mysql_secure_installation mysql_tzinfo_to_sql replace
myisam_ftdump mysqlaccess mysqlcheck mysqld_multi mysql_fix_extensions mysql_setpermission mysql_upgrade resolveip
myisamlog mysqlaccess.conf mysql_client_test mysqld_safe mysqlhotcopy mysqlshow mysql_waitpid resolve_stack_dump
root@ubuntu:/usr/local/mysql# cd ../scripts/
root@ubuntu:/usr/local/mysql/scripts# ls
mysql_install_db
root@ubuntu:/usr/local/mysql/scripts# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
安装完后有这么一段提示,可以仔细看看:
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h ubuntu password 'new-password'
Alternatively you can run:
/usr/local/mysql/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/local/mysql/scripts/mysqlbug script!
(PS:由于之前执行了cmake 之后,使用的是mysql用户,然后make执行成功,然后一直找不到对应的文件)
可能有的错误:
FATAL ERROR: Could not find ./bin/my_print_defaults
If you compiled from source, you need to run 'make install' to
copy the software into the correct location ready for operation.
If you are using a binary release, you must either be at the top
level of the extracted archive, or pass the --basedir option
pointing to that location.
原因:没有指定base_dir 的目录,找不到对应文件
4、复制mysql配置文件和服务文件到系统配置路径下面
[root@localhost support-files]# cp my-medium.cnf /etc/my.cnf
[root@localhost support-files]# cp mysql.server /etc/init.d/mysqld
5、启动服务
6、增加mysql的root用户密码
[root@localhost init.d]# /usr/local/mysql//bin/mysqladmin -u root -h localhost.localdomain password 'mysql'
7、查看启动,看默认的安装路径
root 5038 1 0 21:54 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/localhost.localdomain.pid (说明是root用户启动的进程)
mysql 5293 5038 1 21:54 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/localhost.localdomain.err --pid-file=/usr/local/mysql/data/localhost.localdomain.pid --socket=/tmp/mysql.sock --port=3306 (包含路径、端口等)
root 5324 4756 0 21:54 pts/1 00:00:00 grep mysql
8、设置软连接,添加mysql(基本命令), mysqldump(备份命令), mysqladmin(管理命令)到/usr/bins使其能在shell中直接运行
[root@localhost ~]# /usr/local/mysql/bin/mysqldump /usr/bin
[root@localhost ~]# /usr/local/mysql/bin/mysqladmin /usr/bin
还可以添加更多的命令
9、连接到数据库
mysql> GRANT ALL PRIVILEGES on *.* to 'root'@'%' identified by '123456';
出现error: 'Access denied for user 'root'@'localhost' (using password: NO)'解决方法:
[root@localhost ~]# cd /usr/local/mysql/bin/mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
[root@localhost ~]#mysql -u root
mysql> use mysql
mysql> update user set password=PASSWORD(123456) where user='root' and host='root' or host='localhost';
mysql> flush privileges;
mysql> quit;
[root@localhost ~]# service mysqld start
[root@localhost ~]# mysql -u root -p
Enter password:
mysql>
对 root 用新帐号登陆重新授权:
10、 远程连接 Ubuntu上的mysql
修改 my.cnf (使用apt-get 安装的my.cnf在/etc/mysql 目录下)
将 bind-address= 127.0.0.1 改成 bind-address=0.0.0.0
对root用新帐号登陆重新授权: