linux主机系统版本:centos6.4
mysql版本:5.5
1)官网下载source package
https://dev.mysql.com/downloads/mysql/
2)解压缩source package
[root@kikkiking ~]# tar -xzvf mysql-5.5.58.tar.gz
3)新建mysql用户
[root@kikkiking ~]# useradd mysql -s /sbin/nologin
4)新建mysql的安装目录 /usr/local/mysql;data目录为/usr/local/data;socket目录为/usr/local/sock
[root@kikkiking ~]# mkdir /usr/local/mysql
[root@kikkiking ~]# mkdir /usr/local/sock
[root@kikkiking ~]# mkdir /usr/local/data [root@kikkiking ~]# chown mysql:mysql -R /usr/local/mysql
[root@kikkiking ~]# chown mysql:mysql -R /usr/local/data
[root@kikkiking ~]# chown mysql:mysql -R /usr/local/sock
5)yum安装编译所需依赖包
[root@kikkiking ~]# yum -y install cmake gcc ncurses-devel bison git gcc-c++
6)使用cmake进行编译
[root@kikkiking ~]# cd mysql-5.5.58
[root@kikkiking mysql-5.5.58]# rm -f CMakeCache.txt [root@kikkiking ~]#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_DATADIR=/usr/local/data \ -DMYSQL_UNIX_ADDR=/usr/local/sock/mysql.sock \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DEXTRA_CHARSET=gbk,gb2312,utf8,ascii \ -DENABLED_LOCAL_INFILE=on \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_FEDERATED_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITHOUT_EXAMPL_ESTORAGE_ENGINE=1 \ -DWITHOUT_PARTITION_ESTORAGE_ENGINE=1 \ -DWITH_ZLIB=bundled \ -DWITH_READLINE=1 \
-DWITH_ENBEDDED_SERVER=1 \ -DWITH_DEBUG=0
附:
-DDEFAULT_COLLATION=utf8_general_ci 设置数据校对规则
校对规则:参见https://www.cnblogs.com/zhqiang/p/6864133.html
编译参数:参见https://dev.mysql.com/doc/refman/5.5/en/source-configuration-options.html
7)
[root@kikkiking mysql-5.5.58]# make && make install
设置环境变量:
[root@kikkiking mysql-5.5.58]# echo "export PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile
[root@kikkiking mysql-5.5.58]# source /etc/profile
[root@kikkiking mysql-5.5.58]# echo $PATH
8)数据库初始化
[root@kikkiking mysql-5.5.58]# cd /usr/local/mysql/scripts/ [root@kikkiking scripts]# ./mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/data --user=mysql
9)设置mysqld启动服务
[root@kikkiking scripts]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld [root@kikkiking scripts]# chmod +x /etc/init.d/mysqld [root@kikkiking scripts]# chkconfig --add /etc/init.d/mysqld [root@kikkiking scripts]# chkconfig --level 35 mysqld on
10)编辑mysql配置文件/etc/my.cnf,更改data、socket目录
[root@kikkiking ~]# vim /etc/my.cnf

11)启动mysql服务
[root@kikkiking ~]# service mysqld start
12)设置mysql登录密码并删除匿名用户
[root@kikkiking ~]# mysqladmin -uroot password '123' [root@kikkiking ~]# mysql -uroot -p123 mysql> drop database test; mysql> drop user ''@'localhost'; mysql> drop user ''@'pb.com'; mysql> select user,host from mysql.user where user=''; Empty set (0.00 sec)
附:
若在更改密码时一直报错提示:
[root@kikkiking ~]# mysqladmin -uroot password '123'
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
可通过修改socket路径解决:
[root@kikkiking ~]# vim /etc/my.cnf
socket=/tmp/mysql.sock
重启服务:
[root@kikkiking ~]# service mysqld restart
13)忘记root密码的解决方式
win
C:\Users\kikkiking>net stop mysql C:\Users\kikkiking> mysqld -–skip-grant-tables - #此时这个终端会卡在这里不动,再开一个cmd终端
C:\Users\kikkiking> mysql #直接使用mysql命令进入数据库
mysql> update mysql.user set password=password('123') where user='root'; #设置密码,
mysql> flush privileges; #刷新权限表,然后退出
关闭另一个终端,启动mysql服务
C:\Users\kikkiking>net start mysql
使用新密码登录mysql
C:\Users\kikkiking>mysql -uroot -p123
linux
1)
[root@kikkiking ~]# service mysqld stop #关闭mysql服务 [root@kikkiking ~]# echo "skip-grant-tables" >> /etc/my.cnf #跳过权限表
[root@kikkiking ~]# mysql #直接登录数据库
mysql> update mysql.user set password=password('123') where user='root'; #修改root密码
mysql> flush privileges; #刷新权限表,然后退出数据库
不能直接使用set命令修改
mysql> set password for 'root'@'localhost'=password('123');
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
删除/etc/my.cnf中的skip-grant-tables
重启服务
[root@kikkiking 桌面]# service mysqld restart
使用新密码登录
[root@kikkiking 桌面]# mysql -uroot -p123
2)
[root@kikkiking ~]# service mysqld stop #停止mysqld服务
[root@kikkiking ~]# mysqld_safe --skip-grant-tables & #后台运行,跳过权限表
[root@kikkiking ~]# mysql #直接mysql命令登录数据库
mysql> update mysql.user set password=password('456') where user='root'; #修改密码
mysql> flush privileges; #刷新权限表,然后退出数据库
[root@kikkiking ~]# ps -ef | grep mysqld #查看刚才后台运行的mysqld服务
[root@kikkiking ~]# kill 13607 #杀掉
[root@kikkiking ~]# kill 13607 #杀掉
[root@kikkiking ~]# service mysqld start #启动服务
[root@kikkiking ~]# mysql -uroot -p456 #使用新密码登录数据库
[root@kikkiking ~]# mysql -uroot -p456 #使用新密码登录数据库