您的位置:首页 > 运维架构 > Linux

linux 下安装mysql相关笔记

2015-01-04 23:38 337 查看
安装mysql
yum -y install mysql-server
yum -y install mysql mysql-devel

都显示完成就可以了

查看mysql的版本号
rpm -qi mysql-server

数据库目录
/var/lib/mysql/
配置文件
/usr/share /mysql(mysql.server命令及配置文件)
相关命令
/usr/bin(mysqladmin mysqldump等命令)
启动脚本
/etc/rc.d/init.d/(启动脚本文件mysql的目录)

启动mysql:

service mysqld start

停止mysql:

service mysqld stop

修改密码及配置:

yum 安装的mysql默认有两个用户一个是空用户也就是匿名用户,一个是root但是密码也是为空的,这样可不行。

首先如果你在用匿名用户或者登陆后遇到这个错:Ignoring query to other database

说明你登陆的时候没有加-u这个参数

下面说说怎么配置吧

二、配置
[root@sample ~]# vi /etc/my.cnf  ← 编辑MySQL的配置文件

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1  ← 找到这一行,在这一行的下面添加新的规则,让MySQL的默认编码为UTF-8
default-character-set = gbk  ← 添加这一行
然后在配置文件的文尾填加如下语句:

[mysql]
default-character-set = gbk

三、启动MySQL服务

[root@sample ~]# chkconfig mysqld on  ← 设置MySQL服务随系统启动自启动

如果上述命令失败,显示 bash: chkconfig: command not found,见本文最下面解决方法

[root@sample ~]# chkconfig --list mysqld  ← 确认MySQL自启动
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off  ← 如果2--5为on的状态就OK

[root@sample ~]# /etc/rc.d/init.d/mysqld start  ← 启动MySQL服务

启动mysql [确定]

备 注:如果这一部执行失败有可能是/var/lib/mysql 没有数据库文件,会报【 Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist】,这是需要执行mysql_install_db命令就OK了。

四、MySQL的root用户设置密码
MySQL在刚刚被安装的时候,它的root用户是没有被设置密码的。首先来设置MySQL的root密码。

[root@sample ~]# mysql -u root  ← 用root用户登录MySQL服务器

:如果出现错误Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2),

:则查看mysql是否启动 /etc/rc.d/init.d/mysqld status, 没有启动则/etc/rc.d/init.d/mysqld start
mysql> select user,host,password from mysql.user;  ← 查看用户信息
mysql>set password for root@localhost=password('在这里填入root密码');  ← 设置root密码
mysql>set password for root@域名=password('在这里填入root密码');
mysql> delete from mysql.user where user='';  ← 删除匿名用户
mysql> exit  ← 退出MySQL服务器【测试设置的密码是否正确】

五、删除测试用数据库
mysql> show databases;  ← 查看系统已存在的数据库
mysql> drop database test;  ← 删除名为test的空数据库

六、对MySQL进行测试。包括建立新用户,以及用对关系性数据库进行数据库操作的指令来试着建立数据库及数据表。这里,新建用户以 sleinetpub为例。

[root@sample ~]# mysql -u root -p  ← 通过密码用root登录
Enter password:  ← 在这里输入密码

mysql> grant all privileges on test.* to sleinetpub@localhost identified by '在这里定义密码';  ← 建立对test数据库有完全操作权限的名为sleinetpub的用户
mysql> select user from mysql.user where user='sleinetpub';  ← 确认sleinetpub用户的存在与否
mysql> exit  ← 退出MySQL服务器
[root@sample ~]# mysql -u sleinetpub -p  ← 用新建立的sleinetpub用户登录MySQL服务器
Enter password:  ← 在这里输入密码
mysql> create database test;  ← 建立名为test的数据库
mysql> show databases;  ← 查看系统已存在的数据库
mysql> use test  ← 连接到数据库
mysql> create table test(num int, name varchar(50));  ← 在数据库中建立表
mysql> show tables;  ← 查看数据库中已存在的表
mysql> drop table test;  ← 删除表
mysql> show databases;  ← 查看已存在的数据库
Empty set (0.01 sec)  ← 确认test数据库已被删除(这里非root用户的关系,看不到名为mysql的数据库)
mysql> exit  ← 退出MySQL服务器

七、删除测试用过的遗留用户
[root@sample ~]# mysql -u root -p  ← 通过密码用root登录
Enter password:  ← 在这里输入密码
mysql> revoke all privileges on *.* from sleinetpub@localhost;  ← 取消sleinetpub用户对数据库的操作权限
mysql> delete from mysql.user where user='sleinetpub' and host='localhost';  ← 删除sleinetpub用户
mysql> select user from mysql.user where user='sleinetpub';  ← 查找用户sleinetpub,确认已删除与否
Empty set (0.01 sec)  ← 确认sleinetpub用户已不存在
mysql> flush privileges;  ← 刷新,使以上操作生效
mysql> exit

八、最后,重新启动一次HTTP服务。

[root@undefined /]# /etc/rc.d/init.d/mysqld stop  ← 停止HTTP服务
停止mysql [确定]

[root@undefined /]# /etc/rc.d/init.d/mysqld start ← 启动HTTP服务
启动mysql [确定]

各种保存解决方案

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

Red Hat Enterprise Linux 5服务器上mysql启动报错:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
原因1-启动命令错误:
我开始的时候直接输入命令:mysql start
正确的启动命令是:
/etc/rc.d/init.d/mysql start

原因2-配置文件错误:
检查etc下面的my.cnf如下内容:
[client]
#password = your_password
port = 3306
socket = /usr/mysql-data/mysql.sock
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
port = 3306
socket = /usr/mysql-data/mysql.sock

原因3-启动文件错误:
需要修改MySQL启动脚本/etc/rc.d/init.d/mysql,
其中datadir= ? 一行检查下!

原因4-前提是你在使用php连接时候报错!
在/etc/php.ini修改mysql.default_socket的值设置为:
mysql.default_socket=/var/lib/mysql/mysql.sock
回到终点设置个连接:ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
(在/etc/php.ini中mysql.default_socket这个文件中,关于mysql.default_socket的值的说明是这样的,
;Default socket name for local MySQL connects. If empty, uses the built-in MySQL defaults.
这个值一开始是空的,也就是说,如果我们不主动去修改的话,php将会使用内建在mysql中的默认值)

另一篇文章:

Mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost'问题的解决

这种问题需要强行重新修改密码,方法如下:

/etc/init.d/mysql stop (service mysqld stop )
/usr/bin/mysqld_safe --skip-grant-tables
另外开个SSH连接
[root@localhost ~]# mysql
mysql>use mysql
mysql>update user set password=password("123456") where user="root";
mysql>flush privileges;
mysql>exit

pkill -KILL -t pts/0 可将pts为0的**用户(之前运行mysqld_safe的用户窗口)强制踢出
正常启动 MySQL:/etc/init.d/mysql start (service mysqld start)

注意:另外还遇到需要service mysql star才能启动service mysql stop才能停止。
还有直接使用mysql不能找到命令,错误为“bash: mysql: command not found”可以直接**mysql的安装目录中的bin文件夹跟绝对路径运行命令,还有的需要加./mysql 才能执行。

本文来自:http://sundful.javaeye.com/blog/704337

另一篇关于清除密码、重置用户的文章:

Quote:
First things first. Log in as root and stop the mysql daemon.

sudo /etc/init.d/mysql stop


Now lets start up the mysql daemon and skip the grant tables which store the passwords.

sudo mysqld_safe --skip-grant-tables&

(press Ctrl+C now to disown the process and start typing commands again)

You should see mysqld start up successfully. If not, well you have bigger issues. Now you should be able to connect to mysql without a password.

sudo mysql --user=root mysql

update user set Password=PASSWORD('new-password');
flush privileges;
exit;


Now kill your running mysqld then restart it normally.

sudo killall mysqld_safe&
(press Ctrl+C now to disown the process and start typing commands again)
/etc/init.d/mysql start

You should be good to go. Try not to forget your password again.
http://www.howtoforge.com/reset-forgotten-mysql-root-password

另外关于denied的总结:

MySQL Authentication Denial

3/29/2005, 12:05 am

It seems to me that a lot of people have auth denial when trying to make connections to MySQL. They seem to ignore the text of the error message. ‘Access Denied’ means access denied, nothing else.

Remember three things have to match. The host as MySQL sees it, the username, and the password. When MySQL returns access denied it’s not broken. One or more of those three things does not match. I don’t really need to reiterate what’s in the manual. Chang the lock or change the key to make it fit.

其中连接到mysql的文档内容为:
http://dev.mysql.com/doc/refman/5.5/en/access-denied.html

主从配置相关:

GRANT ALL ON *.* TO slave_1@'192.168.253.210' IDENTIFIED BY 'slave_1';

CHANGE MASTER TO MASTER_HOST='192.168.253.206',MASTER_USER='slave_1',MASTER_PASSWORD='slave_1',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=788;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: