(
mysql> update user set host = '%' where user ='root';
ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
然后查看了下数据库的host信息如下:
MySQL> select host from user where user = 'root';
+-----------------------+
| host |
+-----------------------+
| % |
| 127.0.0.1 |
| localhost.localdomain |
+-----------------------+
3 rows in set (0.00 sec)
host已经有了%这个值,所以直接运行命令: MySQL>flush privileges;
然后连接成功)。
)
mysql>flush privileges;
mysql>select 'host' from user where user='root';
第一句是以权限用户root登录
第二句:选择mysql库
第三句:查看mysql库中的user表的host值(即可进行连接访问的主机/IP名称)
第四句:修改host值(以通配符%的内容增加主机/IP地址),当然也可以直接增加IP地址
第五句:刷新MySQL的系统权限相关表
第六句:再重新查看user表时,有修改。。
重起mysql服务即可完成。
改密:
mysql> use mysql;
mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';
mysql> FLUSH PRIVILEGES;
新增用户,
select user,host from user;
使用create user新增用户
1.mysql>CREATE USER 'user1'@'localhost' IDENTIFIED BY 'pass1';
说明:username – 你将创建的用户名, host – 指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost, 如 果想让该用户可以从任意远程主机登陆,可以使用通配符%. password – 该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登 陆服务器.
例子:
2.mysql>GRANT SELECT,INSERT,UPDATE,DELETE ON *.* TO 'user1'@'localhost';
OR
简单的方法:
mysql>GRANT ALL ON *.* TO 'user1'@'localhost';
或者
grant all on *.* to 'root'@'%' identified by '密码' 这样会自动生成用户名,并且给予指定的密码
If you think the method above is so difficult that you can't accept, you can use the method below:
mysql>GRANT ALL ON *.* TO 'user2'@'localhost' IDENTIFIED BY 'pass1';
And there is another direct method that you can have a try!
mysql>INSERT INTO user (Host,User,Password,Select_priv,Insert_priv)
VALUES('localhost','user4',PASSWORD('pass3'),'Y','Y');
mysql对用户登录ip段授权的方法
GRANT ALL PRIVILEGES ON *.* TO abc@'192.168.2.%' 即可。
GRANT ALL PRIVILEGES ON *.* TO abc@'192.168.%' 即可。
或者
GRANT ALL PRIVILEGES ON *.* TO abc@'192.168.%' 即可。'192.168.0.0/255.255.0.0'
在user表范围字段可以如下被指定:
一个Host值可以是主机名或一个IP数字,或'localhost'指出本地主机。
你可以在Host字段里使用通配符字符“%”和“_”。
一个Host值'%'匹配任何主机名,一个空白Host值等价于'%'。注意这些值匹配能创建一个连接到你的服务器的任何主机!
通配符字符在User字段中不允许,但是你能指定空白的值,它匹配任何名字。如果user表匹配到来的连接的条目有一个空白的用户名,用户被认为是匿名用户(没有名字的用户),而非客户实际指定的名字。这意味着一个空白的用户名被用于在连接期间的进一步的存取检查(即,在阶段2期间)。
Password字段可以是空白的。这不意味着匹配任何口令,它意味着用户必须不指定一个口令进行连接。
非空白Password值代表加密的口令。 MySQL不以任何人可以看的纯文本格式存储口令,相反,正在试图联接的一个用户提供的口令被加密(使用PASSWORD()函数),并且与存储了user表中的已经加密的版本比较。如果他们匹配,口令是正确的。
下面的例子显示出各种user表中Host和User条目的值的组合如何应用于到来的连接:
Host 值
User 值
被条目匹配的连接
'thomas.loc.gov'
'fred'
fred, 从thomas.loc.gov 连接
'thomas.loc.gov'
''
任何用户, 从thomas.loc.gov连接
'%'
'fred'
fred, 从任何主机连接
'%'
''
任何用户, 从任何主机连接
'%.loc.gov'
'fred'
fred, 从在loc.gov域的任何主机连接
'x.y.%'
'fred'
fred, 从x.y.net、x.y.com,x.y.edu等联接。(这或许无用)
'144.155.166.177'
'fred'
fred, 从有144.155.166.177 IP 地址的主机连接
'144.155.166.%'
'fred'
fred, 从144.155.166 C类子网的任何主机连接
既然你能在Host字段使用IP通配符值(例如,'144.155.166.%'匹配在一个子网上的每台主机),有可能某人可能企图探究这种能力,通过命名一台主机为144.155.166.somewhere.com。为了阻止这样的企图,MySQL不允许匹配以数字和一个点起始的主机名,这样,如果你用一个命名为类似1.2.foo.com的主机,它的名字决不会匹配授权表中Host列。只有一个IP数字能匹配IP通配符值。
一个到来的连接可以被在user表中的超过一个条目匹配。例如,一个由fred从thomas.loc.gov的连接匹配多个条目如上所述。如果超过一个匹配,服务器怎么选择使用哪个条目呢?服务器在启动时读入user表后通过排序来解决这个问题,然后当一个用户试图连接时,以排序的顺序浏览条目,第一个匹配的条目被使用。
另外的方法:
http://www.cnblogs.com/fly1988happy/archive/2011/12/15/2288554.html
centos mysql iptables配置
在CentOS系统中防火墙默认是阻止3306端口的,我们要是想访问mysql数据库,我们需要这个端口,命令如下:
1 /sbin/iptables -I INPUT -p tcp --dport 3036 -j ACCEPT
我们需要保存我们的操作,命令如下:
1 /etc/rc.d/init.d/iptables save
此时我们可以查看端口的状态,命令如下:
1 /etc/init.d/iptables status
当然如果你打开其他端口也一样,只需要把这个端口号换乘你需要的端口号即可。
#关闭防火墙
/etc/init.d/iptables stop
service iptables stop # 停止服务
#查看防火墙信息
/etc/init.d/iptables status
#开放端口:8080
/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
#重启防火墙以便改动生效:(或者直接重启系统)
/etc/init.d/iptables restart
#将更改进行保存
/etc/rc.d/init.d/iptables save
另外直接在/etc/sysconfig/iptables中增加一行:
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 8080 -j ACCEPT
#永久关闭防火墙
chkconfig –level 35 iptables off
mysql修改端口
参考:
http://www.cnblogs.com/siqi/p/3567551.html 很好的一个总结。
http://my.oschina.net/u/1179414/blog/202377
mysql配置多实例:
#创建一个新的空数据库
[root@test-206 ~]# mkdir /var/lib/mysql_3307
[root@test-206 ~]# mysql_install_db --datadir=/var/lib/mysql_3307 --user=mysql
3、给数据文件赋予mysql用户与用户组
[root@test-206 ~]# chown -R mysql.mysql /var/lib/mysql_3307
4、创建multi的配置cnf文件,用于启动这个mysql实例(如3307)载入执行
文件中写入你想要的配置,如下为典型配置
[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = root #用于登陆和关闭此服务
password = 123456 #同上
[mysqld3307]
socket = /tmp/mysql_3307.sock
port = 3307
pid-file = /var/lib/mysql_3307/3307.pid
datadir = /var/lib/mysql_3307/
log = /var/lib/mysql_3307/3307.log
character-set-server = utf8
user = mysql
5、启动你的多实例
[root@test-206 ~]# mysqld_multi --defaults-extra-file=/usr/local/my_multi.cnf start 3307
6、检查是否启动成功
[root@test-206 ~]# netstat -ntlp
tcp 0 0 :::3306 :::* LISTEN 3919/mysqld
tcp 0 0 :::3307 :::* LISTEN 15027/mysqld
如果没有发现你要的端口号mysql实例,可以检查下/var/lib/mysql_3307/3307.log文件,排除问题。
my.cnf里面的client和mysql都有port,有有什么不同
我启动出现这个问题:
160419 11:06:56 mysqld_safe The file /usr/local/mysql/bin/mysqld
does not exist or is not executable. Please cd to the mysql installation
directory and restart this script from there as follows:
./bin/mysqld_safe&
加个软连接:
问题存在原因:
1、mysqld_safe只认识/usr/local/mysql/bin/mysqld(也许是配置问题)。
[root@localhost bin]# ./mysqld_multi --help
mysqld_multi version 2.16 by Jani Tolonen
Description:
mysqld_multi can be used to start, or stop any number of separate
mysqld processes running in different TCP/IP ports and UNIX sockets.
mysqld_multi can read group [mysqld_multi] from my.cnf file. You may
want to put options mysqld=... and mysqladmin=... there. Since
version 2.10 these options can also be given under groups [mysqld#],
which gives more control over different versions. One can have the
default mysqld and mysqladmin under group [mysqld_multi], but this is
not mandatory. Please note that if mysqld or mysqladmin is missing
from both [mysqld_multi] and [mysqld#], a group that is tried to be
used, mysqld_multi will abort with an error.
mysqld_multi will search for groups named [mysqld#] from my.cnf (or
the given --defaults-extra-file=...), where '#' can be any positive
integer starting from 1. These groups should be the same as the regular
[mysqld] group, but with those port, socket and any other options
that are to be used with each separate mysqld process. The number
in the group name has another function; it can be used for starting,
stopping, or reporting any specific mysqld server.
Usage: mysqld_multi [OPTIONS] {start|stop|report} [GNR,GNR,GNR...]
or mysqld_multi [OPTIONS] {start|stop|report} [GNR-GNR,GNR,GNR-GNR,...]
The GNR means the group number. You can start, stop or report any GNR,
or several of them at the same time. (See --example) The GNRs list can
be comma separated or a dash combined. The latter means that all the
GNRs between GNR1-GNR2 will be affected. Without GNR argument all the
groups found will either be started, stopped, or reported. Note that
syntax for specifying GNRs must appear without spaces.
Options:
These options must be given before any others:
--no-defaults Do not read any defaults file
--defaults-file=... Read only this configuration file, do not read the
standard system-wide and user-specific files
--defaults-extra-file=... Read this configuration file in addition to the
standard system-wide and user-specific files
Using:
--example Give an example of a config file with extra information.
--help Print this help and exit.
--log=... Log file. Full path to and the name for the log file. NOTE:
If the file exists, everything will be appended.
--
启动报错:
Can't find messagefile '/usr/share/mysql/errmsg.sys'
解决方法是:
root@debian:/usr/local# vi mysql3306/my.cnf
加入language配置项,由于我是使用二进制分发包进行安装的,而默认的情况下,mysql会到/usr/local/mysql/share/english下查找该文件,
[mysqld]
pid-file = /home/www/mysql/m3306/mysql3306.pid
log-error = /home/www/mysql/m3306/mysql3306.err
language = /usr/local/mysql3306/share/english #此处为language配置项
成功启动后:
mysql -P3307 -uroot --socket=/tmp/mysql2.sock
要指定socket文件。
查看mysql状态:
./mysqladmin --socket=/tmp/mysql2.sock ping
mysqld is alive
---------------------
mysql更换data
有时候我们要把复制过来的data数据弄到mysql里。
与使用root用户启动mysqld相比,更好的方法是使用一个普通的、没有高级权限的用户帐户允许mysqld,例如创建一个名为mysql的用户帐户来专门管理MySQL。使用其帐启
动MySQL的方法是在mysqld命令后面加上一个用户选项,这个用户属于mysqld用户组并且位于my.cnf配置文件中。例如在创建mysql帐户后,可以将下面的内容添加到my.cnf文
件中:
[mysqld]
user=mysql
This causes the server to start as the designated user whether you
start it manually or by using *Note `mysqld_safe': mysqld-safe. or
*Note `mysql.server': mysql-server. For more details, see *Note
changing-mysql-user::.
mysql 5.6编译安装:
5.6不依赖boost
5.7有2个版本,依赖boost和不依赖boost.
这样就可以了, 但是为什么和我之前安装的5.1 不一样呢,原来从MySQL5.6.8开始,MySQL RPM安装包用了更安全的安装方式,再不是以前的密码为空了,mysql会给root账号随机分配一个密码,安装MySQL后,root的这个随机密码会写在文件:.mysql_secret中,但这个密码不能做任何事情,只能登录。
mysql里面的mysql.user表为空?
mysql> select * from user;
Empty set (0.00 sec)
mysql> use mysql;
Database changed
mysql> grant all on *.* to 'root'@'%' identified by '123456';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> CREATE USER 'user1'@'localhost' IDENTIFIED BY '123456';