`

linux mysql 常见错误以及主从复制

 
阅读更多

linux mysql 常见错误以及主从复制

1.

如果你的linux 桌面版中已安装了mysql 5.1等低版的mysql ,那么在安装时会报一个 conflict 冲突的提示,

那么,就用yum remove mysql 把它干掉,再安装,yum 能解决包的依赖关系 。

2. 安装:MySQL-client-5.6.13-1.linux_glibc2.5.i386.rpm, MySQL-server-5.6.13-1.linux_glibc2.5.i386.rpm

rpm -ivh MySQL-client-5.6.13-1.linux_glibc2.5.i386.rpm  

rpm -ivh MySQL-server-5.6.13-1.linux_glibc2.5.i386.rpm

说明: mysql 在 linux 中安装完后,安装完的文件不在同一个目录下,分散在几个目录下,

1、数据库目录
/var/lib/mysql/

2、配置文件
/usr/share/mysql(mysql.server命令及配置文件)

3、相关命令
/usr/bin(mysqladmin mysqldump等命令)

4、启动脚本
/etc/rc.d/init.d/(启动脚本文件mysql的目录)

 

3. 安装后,启动  mysql 服务,用 service mysql start   , 默认的root 用户的密码不知道是什么,以前mysql版本的默认密码是空,直接用 mysql 就可以进入,这个5.6 版本的不行,需要修改密码,

4,先用

 1.用:
[root@centos64 mysql]# /usr/bin/mysqld_safe --skip-grant-tables&   以安全方式启动 不用密码就可以login

2.用:

2.  再启一个终端:

[root@centos64 proc]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.13 MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> UPDATE user SET password=password('admin') WHERE user='root';
ERROR 1046 (3D000): No database selected
mysql> show databases ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.06 sec)

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> UPDATE user SET password=password('admin') WHERE user='root';
Query OK, 4 rows affected (0.04 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> exit
Bye

OK,这样密码就修改了

5. 再以正常的方式启动myslq 服务就可以进入了,

说明:这个5.6 版本的mysql 安装后,没有 /etc/my.cnf 文件,

在:/usr/share/mysql/my-default.cnf 有这个文件, 把它复制一份到/etc 下

cp  /usr/share/mysql/my-default.cnf    /etc

再改个名

mv /etc/my-default.cnf /etc/my.cnf    就行了

6.在启动mysql 是如果报:mysqld_safe mysqld from pid file /var/lib/mysql/localhost.localdomain.pid ended 找不到 ........pid 文件,或没有更新,就到:

[root@localhost mysql]# ll /var/lib/mysql/
-rw-rw---- 1 mysql mysql       56 08-22 14:55 auto.cnf
-rw-rw---- 1 mysql mysql 12582912 08-23 15:17 ibdata1
-rw-rw---- 1 mysql mysql 50331648 08-23 15:17 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 08-22 14:53 ib_logfile1
-rw-r----- 1 mysql root    120007 08-23 15:17 localhost.localdomain.err
-rw-rw---- 1 mysql mysql      177 08-23 15:09 localhost-relay-bin.000001
-rw-rw---- 1 mysql mysql      547 08-23 15:17 localhost-relay-bin.000002
-rw-rw---- 1 mysql mysql       58 08-23 15:09
localhost-relay-bin.index
drwx------ 2 mysql mysql     4096 08-23 13:15 master01
-rw-rw---- 1 mysql mysql      130 08-23 15:17 master.info
drwx--x--x 2 mysql mysql     4096 08-22 14:53 mysql
-rw-rw---- 1 mysql mysql      786 08-23 11:48 mysql-bin.000001
-rw-rw---- 1 mysql mysql      481 08-23 13:37 mysql-bin.000002
-rw-rw---- 1 mysql mysql      143 08-23 13:44 mysql-bin.000003
-rw-rw---- 1 mysql mysql      143 08-23 14:37 mysql-bin.000004
-rw-rw---- 1 mysql mysql      143 08-23 15:17 mysql-bin.000005
-rw-rw---- 1 mysql mysql       19 08-23 15:06 mysql-bin.index
drwx------ 2 mysql mysql     4096 08-22 14:53 performance_schema
-rw-rw---- 1 mysql mysql       64 08-23 15:17 relay-log.info
-rw-r--r-- 1 root  root       136 08-22 14:53 RPM_UPGRADE_HISTORY
-rw-r--r-- 1 mysql mysql      136 08-22 14:53 RPM_UPGRADE_MARKER-LAST
drwxr-xr-x 2 mysql mysql     4096 08-23 13:17 test
到此目录下把 mysql-bin.index  文件删除,或查看 localhost.localdomain.err 文件
如:

 

 

 

130823 15:17:11 mysqld_safe mysqld from pid file /var/lib/mysql/localhost.localdomain.pid ended
130823 15:17:38 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
2013-08-23 15:17:44 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2013-08-23 15:17:44 10049 [Note] Plugin 'FEDERATED' is disabled.
2013-08-23 15:17:45 10049 [Note] InnoDB: The InnoDB memory heap is disabled
2013-08-23 15:17:45 10049 [Note] InnoDB: Mutexes and rw_locks use InnoDB's own implementation
2013-08-23 15:17:45 10049 [Note] InnoDB: Compressed tables use zlib 1.2.3
2013-08-23 15:17:45 10049 [Note] InnoDB: Using Linux native AIO
2013-08-23 15:17:45 10049 [Note] InnoDB: Not using CPU crc32 instructions
2013-08-23 15:17:45 10049 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2013-08-23 15:17:45 10049 [Note] InnoDB: Completed initialization of buffer pool
2013-08-23 15:17:45 10049 [Note] InnoDB: Highest supported file format is Barracuda.
2013-08-23 15:17:47 10049 [Note] InnoDB: 128 rollback segment(s) are active.
2013-08-23 15:17:47 10049 [Note] InnoDB: Waiting for purge to start
2013-08-23 15:17:47 10049 [Note] InnoDB: 5.6.13 started; log sequence number 1643040
2013-08-23 15:17:47 10049 [ERROR] /usr/sbin/mysqld: unknown variable 
'Connect_Retry=60'
2013-08-23 15:17:47 10049 [ERROR] Aborting

2013-08-23 15:17:47 10049 [Note] Binlog end
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'partition'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'BLACKHOLE'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'ARCHIVE'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_FT_DELETED'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_METRICS'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_CMPMEM'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_CMP_RESET'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_CMP'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_LOCKS'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'INNODB_TRX'
2013-08-23 15:17:47 10049 [Note] Shutting down plugin 'InnoDB'
2013-08-23 15:17:47 10049 [Note] InnoDB: FTS optimize thread exiting.
2013-08-23 15:17:47 10049 [Note] InnoDB: Starting shutdown...
2013-08-23 15:17:49 10049 [Note] InnoDB: Shutdown completed; log sequence number 1643050
2013-08-23 15:17:49 10049 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
2013-08-23 15:17:49 10049 [Note] Shutting down plugin 'MRG_MYISAM'
2013-08-23 15:17:49 10049 [Note] Shutting down plugin 'CSV'
2013-08-23 15:17:49 10049 [Note] Shutting down plugin 'MyISAM'
2013-08-23 15:17:49 10049 [Note] Shutting down plugin 'MEMORY'
2013-08-23 15:17:49 10049 [Note] Shutting down plugin 'sha256_password'
2013-08-23 15:17:49 10049 [Note] Shutting down plugin 'mysql_old_password'
2013-08-23 15:17:49 10049 [Note] Shutting down plugin 'mysql_native_password'
2013-08-23 15:17:49 10049 [Note] Shutting down plugin 'binlog'
2013-08-23 15:17:49 10049 [Note] /usr/sbin/mysqld: Shutdown complete

130823 15:17:50 mysqld_safe mysqld from pid file /var/lib/mysql/localhost.localdomain.pid ended

 就去查看这里的  .err文件 里面有错误提示。

 

二:主从同步,网上有很多,

最少两台以上的机器上,安装mysql ,

如 192.168.18.130 作为 master ,

192.168.18.131 作为 slave ,

192.168.18.132 作为 slave ,

那么,在 master 上,先建两上账号, 如slave1, slave2 , 这两个账号各自负责同步131 ,132 上的数据,

主要是 配置 /etc/my.cnf 这个文件,

如果你照网上配完后,再启动mysql 服务有报错,就到 /var/lib/mysql/下去查看 .....err 的文件,里面有记录,你的 /etc/my.cnf 文件中哪个配字段配的有问题,   这个是因为mysql 的版本不同导致。你就再打开/etc/my.cnf 先注释掉那一行就行了。

1. 在master 130 上 /etc/my.cnf 上加上:

log_bin=mysql-bin   --- 这个是打开日志文件,在主从复制时必须打开。 启用二进制日志;
server_id=131         ---  这个ID 随意加一个数值就行,如果一台机器装了多个mysql 不要重复,一般以本机的IPV4 的最后一位一样就行了,

replicate-do-db=test     -- 这个是要同步的数据库名
slave-skip-errors=1062   -- 这个是主从复制出错后,跳过错误,继续执行复制;在主从的my.cnf中都加入,
-- 这个 1062 是在出错后在 slave 中会显示出来的,表示主键重复,也就是在主库中新加一条ID重复的数据,从库中会报错,如果不加此行,那么从库出错后,接下来就不能再同步主库中的数据了,加上这一行表示同步数据时ID重复时,从库接下来还可以同步主库中的数据。

log-slave-updates //把更新的记录写到二进制文件中;  -- 暂没试

binlog-ignore-db=mysql   //不同步的二进制数据库名; -- 暂没试

2. 进入 master 中的 mysql ,
mysql> insert into mysql.user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject) values("localhost","slave1",password("slave1"),"","","");
Query OK, 1 row affected (0.10 sec) 

mysql> insert into mysql.user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject) values("localhost","slave2",password("slave2"),"","","");

这一行和网上说的不一样,因为版本的原因。

说明,这两上账号只用于复制数据所有,没有必要给它们授所有的权限,但要授一个权限就行了,如下:


mysql> grant replication slave on *.* to 'slave1'@'192.168.18.131' identified by 'slave1' ;
mysql> grant replication slave on *.* to 'slave2'@'192.168.18.132' identified by 'slave2' ;

这样主库就配好了,

如果还给了别的权限如:

mysql> grant all privileges on *.* to 'slave1'@'localhost' identified by 'slave1' ;  -- 授于所有的权限

mysql> FLUSH PRIVILEGES   -- 一定要用此刷新一下

进入,用

mysql> show master status ;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |      361 | test         |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)  
这里几个参数就是要上面 /etc/my.cnf 中配置的,

file 表示 日志文件,在  /var /lib /myql / 目录下,

position 表示日志文件中的位置,就是靠这个pos 去同步数据的,

Binlog_Do_DB 表示 要同步的数据库名。

 

3. 再配置从库,131 ,132 配置相同,

vi /etc/my.cnf :

加入:

log_bin=mysql-bin
server_id=131

#master-host=192.168.18.131    -- 这些是从网上找的,加上去有报错,
#master-user='master'         ------  mysql 服务起不来就先去掉了。
#master-password='master'
#master-port=3306
#master-connect-retry=60
#Connect_Retry=60
replicate-do-db=test
slave-skip-errors=1062
那就用root 进入131 的mysql :

mysql> change master to
master_host=’192.168.10.130’,
master_user=’slave1’,
master_password=slave1,
master_log_file=’mysql-bin.000003’,  --- 这个就是主库上的那个FILE的名称
master_log_pos=361;   --- 这个是主库上的Position  的值

然后再:

mysql> start slave ;

mysql> show slave status\G ;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.18.130
                  Master_User: master
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 361
               Relay_Log_File: localhost-relay-bin.000005
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 361
              Relay_Log_Space: 623
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 130
                  Master_UUID: e1116fa3-0b0d-11e3-8af1-000c296696a9
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)

ERROR:
No query specified

查看:

 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes             这两个参数为YES 才能正常工作,这两个参数表示两个进程,

主库中的有一个进程,在主从复制时主要有三个进程在工作,
说明:如果主从在复制时从库中查看show slave status\G ;有ERROR报错进,先用stop slave ;

解决,1,进入从库中的mysql   用此再进行一次,但用这个的话中间会有一部分没有同步过来,

mysql> change master to
master_host=’192.168.10.130’,
master_user=’slave1’,
master_password=slave1,  (或只加以下两行,上三行不用再加了)
master_log_file=’mysql-bin.000003’,  --- 这个就是主库上的那个FILE的名称
master_log_pos=361;   --- 这个是主库上的Position  的值

再把主库上的数据文件打个tar 包,copy 到出错的从库上,在主库中 /var/lib/mysql/test  打个tar

但到从库 override 相同的文件,再start slave 即可。

如果出错了,把出错代码记下来,加到my.cnf 中的 slave-skip-errors=1062 后面用 ,1089(比如)

slave-skip-errors=1062,1967,1345 .....

等等 ,下次就不会有错了,

2. 网上有的说用:试了下也是可以了,但中间也有一部分数据没同步过来,建议还是加上slave-skip-errors参数。

进入从库,stop slave ,先停掉。

再用:

mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = n;  n 跳过几步 取1,2,4,5,,这个表示跳到那个出错的 Position 位置点,再

start slave 也是可以的,

再查看  show  slave  status\G  ;

看下这两个参数:

   Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 361
如果和主库中不一致的话,还得用:

mysql> change master to
master_log_file=’mysql-bin.000003’,  --- 这个就是主库上的那个FILE的名称
master_log_pos=361;     去更改一个即可。

 ---------------------------  又找了一个不用上面这么麻烦,

在从库中的 /etc/my.cnf 中加入

slave-skip-errors=all   就可以过滤掉所有的同步错误。建议加上此行参数

可以解决主库在同步数据到从库中从库出错时不再同步数据的情况,非常好,

 

以上设完后就可以同步了,

 参考:http://www.blogjava.net/persister/archive/2009/09/09/294507.html

http://www.iteye.com/topic/1113437 

http://defungo.iteye.com/blog/1836532 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics