MYsql安装
- 安装mysql前需要查询系统中含有的有关mysql的软件
rpm -qa | grep -i mysql //grep -i是不分大小写字符查询,只要含有mysql就显示
-
屏幕显示:
下载mysql地址:http://dev.mysql.com/downloads/mysql/
用下面这两个包
需要下载2个内容,一个是MySQL-5.6.21-1.el6.i686.rpm-bundle.tar,这个是几个程序的合集包,另一个是MySQL-shared-compat-5.6.21-1.el6.i686.rpm,这个是软件包包括MySQL 3.23和MySQL 4.0的共享库。如果你安装了应用程序动态连接MySQL 3.23,但是你想要升级到ySQL 4.0而不想打破库的从属关系,则安装该软件包而不要安装MySQL-shared。从MySQL 4.0.13起包含该安装软件包。
- 将2个文件上传到redhat中,解压MySQL-5.6.21-1.el6.i686.rpm-bundle.tar。
tar xvf MySQL-5.6.21-1.el6.i686.rpm-bundle.tar
- 安装MySQL-shared-compat替换mysql-libs,如果不替换,在删除mysql-libs,会提示postfix依赖于mysql-libs:
rpm -i MySQL-shared-compat-5.6.21-1.el6.i686.rpm
rpm -qa | grep -i mysql #如果有mysql-libs直接删
#mysql-libs-5.1.71-1.el6.i686
#MySQL-shared-compat-5.6.21-1.el6.i686
yum remove mysql-libs
- 测试MySQL-server安装,提示需要安装perl:
rpm -ivh --test MySQL-server-5.6.21-1.el6.i686.rpm
yum install perl #如果你没有perl的话需要安装这个
- 安装MySQL-server,MySQL-client:
rpm -ivh MySQL-server-5.6.21-1.el6.i686.rpm
Preparing... ########################################### [100%]
1:MySQL-server ########################################### [100%]
………………
………………
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.
You must change that password on your first connect,
no other statement but 'SET PASSWORD' will be accepted.
See the manual for the semantics of the 'password expired' flag.
Also, the account for the anonymous user has been removed.
In addition, you can run:
/usr/bin/mysql_secure_installation
………………
………………
# rpm -ivh MySQL-client-5.6.21-1.el6.i686.rpm
Preparing... ########################################### [100%]
1:MySQL-client ########################################### [100%]
下面这一段如果上面没有弹这一堆说密码的内容,就说明他默认给了你空密码直接登录就行,如果出现了的话,大意是全新安装设置的root密码在/root/.mysql_secret中,这是一个随机密码,你需要运行/usr/bin/mysql_secure_installation,删除anonymous用户。当然不建议用root用户来运行,rpm包已经建了一个mysql用户,可以使用这个用户:
more .mysql_secret
# The random password set for the root user at Tue Nov 18 22:57:46 2014 (local t
ime): NljqL63OYlGo5cqy <– 得到root访问mysql的密码:NljqL63OYlGo5cqy
service mysql start
#Starting MySQL... SUCCESS!
/usr/bin/mysql_secure_installation --user=mysql
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): <–使用刚才得到的root的密码 NljqL63OYlGo5cqy
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] y <– 是否更换root用户密码,输入y并回车,强烈建议更换
New password: <– 设置root用户的密码
Re-enter new password: <– 再输入一次你设置的密码
Password updated successfully!
Reloading privilege tables..
... Success!
##如果没有上面那段提示一堆密码内容的话,直接登录进去/usr/bin/mysql_secure_installation --user=mysql
就能修改密码,改成你想要的,然后下面继续
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y <– 是否删除匿名用户,生产环境建议删除,所以输入y并回车
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y <–是否禁止root远程登录,根据自己的需求选择Y/n并回车,建议禁止
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y <– 是否删除test数据库,输入y并回车
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y 是否重新加载权限表,输入y并回车
... Success!
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up...
- 至此,MySQL已经安装完成,最后看一下是否已将MySQL加到开机服务里:
# chkconfig
auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iscsi 0:off 1:off 2:off 3:on 4:on 5:on 6:off
iscsid 0:off 1:off 2:off 3:on 4:on 5:on 6:off
lvm2-monitor 0:off 1:on 2:on 3:on 4:on 5:on 6:off
mdmonitor 0:off 1:off 2:on 3:on 4:on 5:on 6:off
multipathd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off <-看到这个OK了
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
netfs 0:off 1:off 2:off 3:on 4:on 5:on 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off
rdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:off
restorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
saslauthd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
udev-post 0:off 1:on 2:on 3:on 4:on 5:on 6:off
数据库安装好以后修改远程登录状态
mysql>use mysql;
mysql> select host from user where user = 'root';
+-----------+
| host |
+-----------+
| 127.0.0.1 |
| localhost |
+-----------+
2 rows in set (0.00 sec)
- 出现情况如上图
Root用户host值是上图两个,都是本地,要修改为%
mysql>select host from user where user = 'root';
+-----------+
| host |
+-----------+
| % |
| 127.0.0.1 |
+-----------+
2 rows in set (0.00 sec)
mysql>flush privileges; ##让改动生效
mysql> quit
data目录迁移
默认mysql是安装在/var/lib/mysql目录下,但是一般只有15G空间,令人至疯,所以装完必须迁移
1、home目录下建立data目录
cd /homemkdir data
2、把MySQL服务进程停掉:
mysqladmin -u root -p shutdown
3、把/var/lib/mysql整个目录移到/home/data
mv /var/lib/mysql /home/data/
这样就把MySQL的数据文件移动到了/home/data/mysql下
4、找到my.cnf配置文件
如果/etc/目录下没有my.cnf配置文件,请到/usr/share/mysql/下找到*.cnf文件,拷贝其中一个到/etc/并改名为my.cnf)中。命令如下:
[root@test1 mysql]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
5、编辑MySQL的配置文件/etc/my.cnf
为保证MySQL能够正常工作,需要指明mysql.sock文件的产生位置。修改socket=/var/lib/mysql/mysql.sock一行中等号右边的值为:/home/mysql/mysql.sock 。操作如下:
vi my.cnf
(用vi工具编辑my.cnf文件,找到下列数据修改之)# The MySQL server[mysqld] port = 3306#socket = /var/lib/mysql/mysql.sock(原内容,为了更稳妥用“#”注释此行)socket = /home/data/mysql/mysql.sock (加上此行)
6、修改MySQL启动脚本/etc/init.d/mysql
最后,需要修改MySQL启动脚本/etc/init.d/mysqld,把其中datadir=/var/lib/mysql一行中,等号右边的路径改成你现在的实际存放路径:home/data/mysql。
[root@test1 etc]# vi /etc/init.d/mysql#datadir=/var/lib/mysql(注释此行)datadir=/home/data/mysql (加上此行)
如果是CentOS还要改 /usr/bin/mysqld_safe 相关文件位置;
最后 做一个mysql.sock 链接:
in -s /home/data/mysql/mysql.sock /var/lib/mysql/mysql.sock
7、重新启动MySQL服务
/etc/init.d/mysql start或者service mysql restart
8、修改整个目录权限
chown -R mysql:mysql /home/data/mysql/
chmod -R 777 /home/data/mysql/*
无密码登录问题
- 如果环境出现登录时候报错
“ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)”。
是密码不对
1.首先修改/etc/my.cnf中的mysqld,添加skip-grant-tables,保存退出
2,执行 service mysql restart,重启之后,再尝试登陆mysql,遇到需要输入密码,直接回车
3,执行 use mysql
4,执行 update user set password=PASSWORD("你的新密码") where user="root"; 这是mysql的执行语句,所以记得末尾加上;号
5,执行 exit ,退出mysql
6,执行 vim /etc/mysql/my.cnf,打开文件后, 把刚才添加的 skip-grant-tables 删掉,保存退出
7,执行 service mysql restart。