码迷,mamicode.com
首页 > 数据库 > 详细

MySQL主从

时间:2017-06-19 22:13:10      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:mysql主从

mysql主从--1

[root@localhost ~]# rm -rf /usr/local/mysql         (删除之前安装的MySQL)

[root@localhost ~]# cd /usr/local/src/

[root@localhost src]# wget http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.73-linux-i686-glibc23.tar.gz

[root@localhost src]# tar zxvf mysql-5.1.73-linux-i686-glibc23.tar.gz

[root@localhost src]# mv mysql-5.1.73-linux-i686-glibc23 /usr/local/mysql

[root@localhost src]# ls /usr/local/mysql/

bin      data  include         lib  mysql-test  scripts  sql-bench

COPYING  docs  INSTALL-BINARY  man  README      share    support-files

[root@localhost src]# grep ‘mysql‘ /etc/passwd  (检查有无MySQL用户)

[root@localhost src]# useradd -s /sbin/nologin mysql (创建MySQL用户)

[root@localhost src]# cd /usr/local/mysql/

[root@localhost mysql]# ls

bin      data  include         lib  mysql-test  scripts  sql-bench

COPYING  docs  INSTALL-BINARY  man  README      share    support-files

[root@localhost mysql]# cp support-files/my-small.cnf /etc/my.cnf     (拷贝配置文件)

cp:是否覆盖"/etc/my.cnf"? y   (把原来的覆盖掉)

[root@localhost mysql]# vim /etc/my.cnf  

[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld  (拷贝启动脚本)

[root@localhost mysql]#  vim /etc/init.d/mysqld

basedir=/usr/local/mysql

datadir=/data/mysql

技术分享

[root@localhost mysql]# rm -rf /data/mysql  (删除之前的/data/mysql )

[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql (重新生成/data/mysql )

Installing MySQL system tables...

170522 18:28:28 [Warning] ‘--skip-locking‘ is deprecated and will be removed in a future release. Please use ‘--skip-external-locking‘ instead.

OK

Filling help tables...

170522 18:28:28 [Warning] ‘--skip-locking‘ is deprecated and will be removed in a future release. Please use ‘--skip-external-locking‘ instead.

OK

To start mysqld at boot time you have to copy

support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !

To do so, start the server, then issue the following commands:

./bin/mysqladmin -u root password ‘new-password‘

./bin/mysqladmin -u root -h localhost password ‘new-password‘

Alternatively you can run:

./bin/mysql_secure_installation

which will also give you the option of removing the test

databases and anonymous user created by default.  This is

strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

cd ./mysql-test ; perl mysql-test-run.pl

Please report any problems with the ./bin/mysqlbug script!

[root@localhost mysql]# ls /data/mysql    (查看 /data/mysql  下是否有着两个目录)

mysql  test

[root@localhost mysql]# /etc/init.d/mysqld start         (启动MySQL)

Starting MySQL.                                           [确定]

[root@localhost mysql]# ps aux |grep mysql

root      2271  0.0  0.0   6680  1316 pts/0    S    18:52   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/localhost.pid

mysql     2383  0.0  0.7 112192 14328 pts/0    Sl   18:52   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql --log-error=/data/mysql/localhost.err --pid-file=/data/mysql/localhost.pid --socket=/tmp/mysql.sock --port=3306

root      2411  0.0  0.0   5980   744 pts/0    S+   19:01   0:00 grep mysql

[root@localhost mysql]# netstat -lnp |grep 3306

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      2383/mysqld

装第二个MySQL:

[root@localhost mysql]# cd ..

[root@localhost local]# ls

bin  etc  games  include  lib  libexec  mysql  pureftpd  sbin  share  src

[root@localhost local]# cp -r mysql mysql_slave   (拷贝程序目录)

[root@localhost local]# ls

bin  etc  games  include  lib  libexec  mysql  mysql_slave  pureftpd  sbin  share  src

[root@localhost local]# cd mysql_slave/

[root@localhost mysql_slave]# ls

bin      data  include         lib  mysql-test  scripts  sql-bench

COPYING  docs  INSTALL-BINARY  man  README      share    support-files

[root@localhost mysql_slave]# cp /etc/my.cnf .  (将配置文件放到当前目录下)

[root@localhost mysql_slave]# pwd

/usr/local/mysql_slave

[root@localhost mysql_slave]# vim my.cnf

修改并添加如下内容:

[mysqld]

port            = 3307

socket          = /tmp/mysql_slave.sock

datadir         =/data/mysql_slave

技术分享

[root@localhost mysql_slave]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql_slave    (初始化)

Installing MySQL system tables...

170522 22:38:48 [Warning] ‘--skip-locking‘ is deprecated and will be removed in a future release. Please use ‘--skip-external-locking‘ instead.

OK

Filling help tables...

170522 22:38:48 [Warning] ‘--skip-locking‘ is deprecated and will be removed in a future release. Please use ‘--skip-external-locking‘ instead.

OK

To start mysqld at boot time you have to copy

support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !

To do so, start the server, then issue the following commands:

./bin/mysqladmin -u root password ‘new-password‘

./bin/mysqladmin -u root -h localhost password ‘new-password‘

Alternatively you can run:

./bin/mysql_secure_installation

which will also give you the option of removing the test

databases and anonymous user created by default.  This is

strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

cd ./mysql-test ; perl mysql-test-run.pl

Please report any problems with the ./bin/mysqlbug script!

[root@localhost mysql_slave]# ls /data/mysql_slave/

mysql  test

[root@localhost mysql_slave]# cd /etc/init.d/

[root@localhost init.d]# cp mysqld mysqldslave

[root@localhost init.d]# vim !$      (或者 用vim mysqldslave

修改:

①:

basedir=/usr/local/mysql_slave

datadir=/data/mysql_slave

conf=$basedir/my.cnf         (添加)

②:

# Try to find basedir in /etc/my.cnf

 conf=$bindir/my.cnf

print_defaults=

if test -r $conf

then

[root@localhost init.d]# /etc/init.d/mysqldslave start

Starting MySQL.                                            [确定]

[root@localhost init.d]# netstat -lnp |grep mysql

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      2383/mysqld

tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      4511/mysqld

unix  2      [ ACC ]     STREAM     LISTENING     20473  2383/mysqld         /tmp/mysql.sock

unix  2      [ ACC ]     STREAM     LISTENING     28085  4511/mysqld         /tmp/mysql_slave.sock

[root@localhost init.d]# ps aux |grep mysql

root      2271  0.0  0.0   6680  1316 pts/0    S    May22   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/localhost.pid

mysql     2383  0.0  0.7 112192 14328 pts/0    Sl   May22   0:07 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql --log-error=/data/mysql/localhost.err --pid-file=/data/mysql/localhost.pid --socket=/tmp/mysql.sock --port=3306

root      4362  0.0  0.0   6680  1316 pts/0    S    May22   0:00 /bin/sh /usr/local/mysql_slave/bin/mysqld_safe --datadir=/data/mysql_slave --pid-file=/data/mysql_slave/localhost.pid

mysql     4511  0.0  0.6 122468 13424 pts/0    Sl   May22   0:00 /usr/local/mysql_slave/bin/mysqld --basedir=/usr/local/mysql_slave --datadir=/data/mysql_slave --user=mysql --log-error=/data/mysql_slave/localhost.err --pid-file=/data/mysql_slave/localhost.pid --socket=/tmp/mysql_slave.sock --port=3307

root      4530  0.0  0.0   5984   748 pts/0    S+   00:00   0:00 grep mysql

两个MySQL均一起动,如果想再添加一个MySQL服务,就可以参照之前的配置,配置第三个服务或者更多。


MySQL主从--2

在主上创建一个测试的数据库。

登录MySQL:

# mysql    (输入MySQL命令会直接启动MySQL进入到3306端口下),因为有/usr/bin/mysql文件所以可以直接登录。

[root@qiangge init.d]# which mysql

/usr/bin/mysql

[root@qiangge init.d]# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.1.73 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> quit

Bye

如果没有/usr/bin/mysql文件,可以用绝对路径/usr/local/mysql/bin/mysql。

[root@qiangge init.d]# /usr/local/mysql/bin/mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.1.73 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> quit

Bye

登录两台mysql:(可以通过端口也可以通过sock)

[root@qiangge init.d]# mysql -S /tmp/mysql.sock     (这是登录3306端口的MySQL)

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.1.73 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> quit

Bye

[root@qiangge init.d]# mysql -S /tmp/mysql_slave.sock  (这是登录3307端口的MySQL)

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.1.73 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> quit

Bye

先登录主:

[root@qiangge init.d]# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 6

Server version: 5.1.73 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> create database db1;    (创建库db1)

Query OK, 1 row affected (0.10 sec)

mysql> quit

Bye

[root@qiangge init.d]# mysqldump -S /tmp/mysql.sock mysql >123.sql

(拷贝数据到MySQL库)

-- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.

[root@qiangge init.d]# cat 123.sql   (查看有没有拷贝成功文件内容)

[root@qiangge init.d]# mysql -S /tmp/mysql.sock db1 < 123.sql (拷贝回去)

再次登录MySQL:

[root@qiangge init.d]# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 10

Server version: 5.1.73 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> use db1;

Database changed

mysql> show tables;   (查看表)

+---------------------------+

| Tables_in_db1             |

+---------------------------+

| columns_priv              |

| db                        |

| event                     |

| func                      |

| general_log               |

| help_category             |

| help_keyword              |

| help_relation             |

| help_topic                |

| host                      |

| ndb_binlog_index          |

| plugin                    |

| proc                      |

| procs_priv                |

| servers                   |

| slow_log                  |

| tables_priv               |

| time_zone                 |

| time_zone_leap_second     |

| time_zone_name            |

| time_zone_transition      |

| time_zone_transition_type |

| user                      |

+---------------------------+

23 rows in set (0.01 sec)

mysql>

另外打开一个终端,登录从:

[root@qiangge ~]# mysql -S /tmp/mysql_slave.sock

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.1.73 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>

退出主,编辑配置文件:

[root@qiangge init.d]# vim /etc/my.cnf

#skip-networking

server-id       = 1     (可以将数字1改成其他的,但必须保证住上和从上不一样)

# Uncomment the following if you want to log updates

#log-bin=mysql-bin   (打开前面的井号,可以自定义名字)

改为log-bin=qiangzi

binlog-do-db=db1  (添加,意思是这个数据库只针对db1做主从)这是白名单,后面还可以添加多个库。但是库太多的时候就可以创建黑名单性质:binlog-ignore-db=mysql(只需写上不通同步的库就行了)。看需要

保存退出

[root@qiangge init.d]# /etc/init.d/mysqld restart    (重启MySQL服务)

Shutting down MySQL.....                                   [确定]

Starting MySQL..                                                [确定]

[root@qiangge init.d]# ls /data/mysql/       (会生成以qiangge作为前缀的几个文件)

db1      ib_logfile0  mysql        qiangge.pid     qiangzi.index

ibdata1  ib_logfile1  qiangge.err  qiangzi.000001  test

接下来需要做一些授权操作:

[root@qiangge init.d]# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.1.73-log 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> grant replication slave on *.* to ‘repl‘@‘127.0.0.1‘ identified by ‘123456‘;

(创建用户,只给赋予replication权限,用户名为“repl”,因为是本机所以用回环地址127.0.0.1;密码是123456)

Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;   (刷新权限)

Query OK, 0 rows affected (0.06 sec)

mysql> flush tables with read lock;  (将数据库中的表都加一个读锁)

Query OK, 0 rows affected (0.00 sec)

mysql> show master status;

+-------------------+-----------+-------------------+------------------------+

| File                   | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+-------------------+-----------+-------------------+------------------------+

| qiangzi.000001 |       331 | db1                  |                               |

+--------------------+----------+-------------------+------------------------+

1 row in set (0.00 sec)

mysql>

退出MySQL从,更改配置文件:

[root@qiangge ~]# vim /usr/local/mysql_slave/my.cnf  (编辑配置文件)

#skip-networking

server-id       = 111     (保证从上的这个数字和主上的不一样)

# Uncomment the following if you want to log updates

#log-bin=mysql-bin

保存退出

[root@qiangge ~]# /etc/init.d/mysqldslave restart

Shutting down MySQL......                                  [确定]

Starting MySQL..                                                 [确定]

[root@qiangge ~]# mysql -S /tmp/mysql_slave.sock -e "create database db1"

(创建一个库)

[root@qiangge ~]# mysql -S /tmp/mysql_slave.sock db1< 123.sql(要实现同步,必须保证这两机器的库是一样的)

[root@qiangge ~]# mysql -S /tmp/mysql_slave.sock  (登录到从上)

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 5

Server version: 5.1.73 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> slave stop ;  (先停掉)

Query OK, 0 rows affected, 1 warning (0.06 sec)

mysql> change master to master_host=‘127.0.0.1‘, master_port=3306, master_user=‘repl‘, master_password=‘123456‘, master_log_file=‘qiangzi.000001‘, master_log_pos=331; 以上内容在同一行中,并且非常重要;master_host默认是3306,也可以不写,连接需要哪个端口就写哪个端口;密码是之前配置是的密码;master_log_file和master_log_pos就用到主上表里面的内容)

Query OK, 0 rows affected (0.15 sec)

mysql> slave start;   (启动从)

Query OK, 0 rows affected (0.07 sec)

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 127.0.0.1

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: qiangzi.000001

Read_Master_Log_Pos: 331

Relay_Log_File: qiangge-relay-bin.000002

Relay_Log_Pos: 249

Relay_Master_Log_File: qiangzi.000001

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

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: 331

Relay_Log_Space: 406

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:

1 row in set (0.00 sec)

ERROR:

No query specified

mysql>

看到有两个yes,说明就OK了。


MySQL主从--3 (测试MySQL主从)

登录主:

mysql> flush tables with read lock; (将数据库中的表都加一个读锁)

Query OK, 0 rows affected (0.00 sec)

mysql> show master status;

+-------------------+-----------+-------------------+------------------------+

| File                   | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+-------------------+-----------+-------------------+------------------------+

| qiangzi.000001 |       331 | db1                  |                               |

+--------------------+----------+-------------------+------------------------+

1 row in set (0.00 sec)

mysql> unlock tables;    (先需要给MySQL解锁)

Query OK, 0 rows affected (0.00 sec)

mysql> use db1;

Database changed

mysql> show tables;

+---------------------------+

| Tables_in_db1             |

+---------------------------+

| columns_priv              |

| db                        |

| event                     |

| func                      |

| general_log               |

| help_category             |

| help_keyword              |

| help_relation             |

| help_topic                |

| host                      |

| ndb_binlog_index          |

| plugin                    |

| proc                      |

| procs_priv                |

| servers                   |

| slow_log                  |

| tables_priv               |

| time_zone                 |

| time_zone_leap_second     |

| time_zone_name            |

| time_zone_transition      |

| time_zone_transition_type |

| user                      |

+---------------------------+

23 rows in set (0.00 sec)

mysql>drop table help_category;  (删除help_category表)

Query OK, 0 rows affected (0.02 sec)

mysql>

登录从:

mysql> use db1;

Database changed

mysql> show tables;

+---------------------------+

| Tables_in_db1             |

+---------------------------+

| columns_priv              |

| db                        |

| event                     |

| func                      |

| general_log               |

| help_keyword              |

| help_relation             |

| help_topic                |

| host                      |

| ndb_binlog_index          |

| plugin                    |

| proc                      |

| procs_priv                |

| servers                   |

| slow_log                  |

| tables_priv               |

| time_zone                 |

| time_zone_leap_second     |

| time_zone_name            |

| time_zone_transition      |

| time_zone_transition_type |

| user                      |

+---------------------------+

22 rows in set (0.00 sec)

mysql>

看到这个表里面并没有help_category表了,因为之前在主上删除了,MySQL主从只能在主上操作,不能再从上操作一些写入,因为在从上操作MySQL的话,会使其发生紊乱。

登录主:

mysql>  use db1;

Database changed

mysql> show tables;

+---------------------------+

| Tables_in_db1             |

+---------------------------+

| columns_priv              |

| db                        |

| event                     |

| func                      |

| general_log               |

| help_keyword              |

| help_relation             |

| help_topic                |

| host                      |

| ndb_binlog_index          |

| plugin                    |

| proc                     |

| procs_priv                |

| servers                   |

| slow_log                  |

| tables_priv               |

| time_zone                 |

| time_zone_leap_second     |

| time_zone_name            |

| time_zone_transition      |

| time_zone_transition_type |

| user                      |

+---------------------------+

22 rows in set (0.00 sec)

mysql> drop table proc;   (删除proc表)

Query OK, 0 rows affected (0.04 sec)

mysql>

登录从:

mysql> use db1;

Database changed

mysql> show tables;

+---------------------------+

| Tables_in_db1             |

+---------------------------+

| columns_priv              |

| db                        |

| event                     |

| func                      |

| general_log               |

| help_keyword              |

| help_relation             |

| help_topic                |

| host                      |

| ndb_binlog_index          |

| plugin                    |

| procs_priv                |

| servers                   |

| slow_log                  |

| tables_priv               |

| time_zone                 |

| time_zone_leap_second     |

| time_zone_name            |

| time_zone_transition      |

| time_zone_transition_type |

| user                      |

+---------------------------+

21 rows in set (0.00 sec)

mysql>

(登录从发现proc表已被删除)

登录主:(创建一个表,创建表的时候我们要定义列什么,不能直接创建)

mysql> show create table user\G:

*************************** 1. row ***************************

Table: user

Create Table: CREATE TABLE `user` (

`Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,

`User` char(16) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,

`Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT ‘‘,

`Select_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Insert_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Update_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Delete_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Create_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Drop_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Reload_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Shutdown_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Process_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`File_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Grant_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`References_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Index_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Alter_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Show_db_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Super_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Create_tmp_table_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Lock_tables_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Execute_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Repl_slave_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Repl_client_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Create_view_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Show_view_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Create_routine_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Alter_routine_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Create_user_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Event_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Trigger_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`ssl_type` enum(‘‘,‘ANY‘,‘X509‘,‘SPECIFIED‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘‘,

`ssl_cipher` blob NOT NULL,

`x509_issuer` blob NOT NULL,

`x509_subject` blob NOT NULL,

`max_questions` int(11) unsigned NOT NULL DEFAULT ‘0‘,

`max_updates` int(11) unsigned NOT NULL DEFAULT ‘0‘,

`max_connections` int(11) unsigned NOT NULL DEFAULT ‘0‘,

`max_user_connections` int(11) unsigned NOT NULL DEFAULT ‘0‘,

PRIMARY KEY (`Host`,`User`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT=‘Users and global privileges‘

1 row in set (0.04 sec)

-> ;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘:‘ at line 1

下面的命令CREATE TABLE `qiangzilinux`后段直接拷贝上面表中列出的内容,创建qiangziLinux表。

mysql> CREATE TABLE `qiangzilinux` (  

   ->   `Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,

   ->   `User` char(16) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,

   ->   `Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT ‘‘,

   ->   `Select_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Insert_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Update_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Delete_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Create_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Drop_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Reload_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Shutdown_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,    ->   `Process_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `File_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Grant_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `References_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Index_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Alter_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Show_db_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Super_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Create_tmp_table_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Lock_tables_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Execute_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Repl_slave_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Repl_client_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Create_view_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Show_view_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Create_routine_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Alter_routine_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Create_user_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Event_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `Trigger_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

   ->   `ssl_type` enum(‘‘,‘ANY‘,‘X509‘,‘SPECIFIED‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘‘,

   ->   `ssl_cipher` blob NOT NULL,

   ->   `x509_issuer` blob NOT NULL,

   ->   `x509_subject` blob NOT NULL,

   ->   `max_questions` int(11) unsigned NOT NULL DEFAULT ‘0‘,

   ->   `max_updates` int(11) unsigned NOT NULL DEFAULT ‘0‘,

   ->   `max_connections` int(11) unsigned NOT NULL DEFAULT ‘0‘,

   ->   `max_user_connections` int(11) unsigned NOT NULL DEFAULT ‘0‘,

   ->   PRIMARY KEY (`Host`,`User`)

   -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT=‘Users and global privileges‘;

Query OK, 0 rows affected (0.08 sec)

mysql> show tables;

+---------------------------+

| Tables_in_db1             |

+---------------------------+

| columns_priv              |

| db                        |

| event                     |

| func                      |

| general_log               |

| help_keyword              |

| help_relation             |

| help_topic                |

| host                      |

| ndb_binlog_index          |

| plugin                    |

| procs_priv                |

| qiangzilinux             |

| servers                   |

| slow_log                  |

| tables_priv               |

| time_zone                 |

| time_zone_leap_second     |

| time_zone_name            |

| time_zone_transition      |

| time_zone_transition_type |

| user                      |

+---------------------------+

22 rows in set (0.00 sec)

mysql>

创建qiangzilinux表成功。

登录从:

mysql> show tables;  

+---------------------------+

| Tables_in_db1             |

+---------------------------+

| columns_priv              |

| db                        |

| event                     |

| func                      |

| general_log               |

| help_keyword              |

| help_relation             |

| help_topic                |

| host                      |

| ndb_binlog_index          |

| plugin                    |

| procs_priv                |

| qiangzilinux              |

| servers                   |

| slow_log                  |

| tables_priv               |

| time_zone                 |

| time_zone_leap_second     |

| time_zone_name            |

| time_zone_transition      |

| time_zone_transition_type |

| user                      |

+---------------------------+

22 rows in set (0.01 sec)

mysql>

MySQL主从机制非常脆弱,很容易中断,为了避免这种情况,建议做个监控,核心的就是监控 Slave_IO_Running: Yes ;Slave_SQL_Running: Yes

是否都是yes.载查看下面有没有error。另外从上千万不能写数据,如果写了服务就会紊乱。

扩展练习:

1. mysql replication模式主要应用在什么场景呢?

应用在mysql数据库的实时备份;读写分离;mysql的HA集群等

2. 在一台服务器上同时配置两个mysql服务的时候,如果已经配置完一个mysql服务,配置另一个的时候如何更改监听端口?

vim /etc/my.cnf  #更改[mysqld]下的 port = 3306 

3. 想要让mysql开机启动,需要把启动命令放到哪个文件下?

/etc/rc.local

4. 在master的配置文件中,都修改了哪几个配置选项?

server-id=1

log-bin=mysql-bin

5.  如何给mysql设置root密码?

mysqladmin -uroot password ‘123456‘

6. 配置replication模式时,master上需要给slave授予什么样的权限?

grant replication slave on *.* to ‘repl‘@‘ip‘ identified by ‘pass‘; 

7. mysql有一个选项,可以不进入mysql的命令控制台就可以使用sql语句操作,是哪一个选项?

-e

8. 如何在slave上查看主从是否正常?

show slave status\G;

9. replication模式下,如果想重启master, slave必须先操作什么?

slave 上必须先slave stop;


本文出自 “12350027” 博客,谢绝转载!

MySQL主从

标签:mysql主从

原文地址:http://12360027.blog.51cto.com/12350027/1939838

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!