搭建 CentOS 6 服务器(8) - MySQL、PostgreSQL

(一)MySQL

(1)安装

Shell代码  收藏代码
  1. # yum -y install http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm  
  2. # yum info mysql-community-server  
  3. # yum -y install mysql-community-server  
  4. # vi /etc/my.cnf  
  5.     character-set-server=utf8  
  6. # /etc/init.d/mysqld restart  
  7. # mysqld --version  
  8.     mysqld  Ver 5.6.23 for Linux on x86_64 (MySQL Community Server (GPL))  



(2)基本设置

Shell代码  收藏代码
  1. # mysql_secure_installation  
  2.     Enter current password for root (enter for none): ← 回车  
  3.   
  4.     Set root password? [Y/n] ← 回车  
  5.     New password: ← 123456  
  6.     Re-enter new password: ← 123456  
  7.   
  8.     Remove anonymous users? [Y/n] ← 回车(删除匿名用户)  
  9.   
  10.     Disallow root login remotely? [Y/n] ← 回车(禁止远程root登录)  
  11.   
  12.     Remove test database and access to it? [Y/n] ← 回车(删除test库)  
  13.   
  14.     Reload privilege tables now? [Y/n] ← 回车  
  15.   
  16.     Thanks for using MySQL!  
  17. # mysql -u root -p  
  18.     Enter password:123456  
  19.   
  20.     mysql> create database mydb;  
  21.     mysql> grant all privileges on mydb.* to testuser@localhost identified by '123456';  
  22.     mysql> select user, host from mysql.user;  
  23.     mysql> quit  



(3)主从设置

①设置Master

Shell代码  收藏代码
  1. # vi /etc/my.cnf  
  2.     [mysqld]  
  3.     server-id=10  
  4.     log-bin=mysqld-bin  
  5.     binlog-ignore-db=test,performance_schema,information_schema  
  6. # service mysqld restart  
  7. # mysql -u root -p  
  8.     mysql> grant replication slave on *.* to 'repl_user'@'slave_host' identified by 'repl_pwd';  
  9.     mysql> flush privileges;  
  10.     mysql> flush tables with read lock;  
  11.     mysql> quit  
  12. # mysqldump -u root -p --all-databases --lock-all-tables > /root/dump.sql  
  13. # gzip dump.sql  
  14. # mysql -u root -p  
  15.     mysql> unlock tables;  
  16.     mysql> show master status\G ←确认File和Position,后边Slave会用到  
  17.     mysql> quit  



②设置Slave

Shell代码  收藏代码
  1. # vi /etc/my.cnf  
  2.     [mysqld]  
  3.     server-id=11  
  4.     relay-log=mysqld-relay-bin  
  5.     read_only=1  
  6. # service mysqld restart  
  7. # scp root@slave_host:/root/dump.sql.gz /root  
  8. # cd /root  
  9. # gzip -d dump.sql.gz  
  10. # mysql -uroot -p < /root/dump.sql  
  11. # mysql -u root -p  
  12.     mysql> reset slave;  
  13.     mysql> change master to master_host='master_host', master_user='repl_user', master_password='repl_pwd', master_log_file='mysqld-bin.xxxxxx', master_log_pos=xxxx; ←这里的值和上边'show master status'的结果相同  
  14.     mysql> start slave;  
  15.     mysql> show slave status\G  
  16.     mysql> quit  



③确认主从

Shell代码  收藏代码
  1. # mysql -u root -p  
  2.     mysql> show databases;  
  3.     mysql> create database test_db;  
  4.     mysql> quit  



Shell代码  收藏代码
  1. # mysql -u root -p  
  2.     mysql> show databases; ←test_db同时被做成  
  3.     mysql> quit  



(二)PostgreSQL

(1)下载安装

Shell代码  收藏代码
  1. # cd /usr/local/src  
  2. # wget http://sourceforge.net/projects/postgresql.mirror/files/PostgreSQL%209.3.4/postgresql-9.3.4-1-linux-x64.run/download  
  3. # chmod 755 postgresql-9.3.4-1-linux-x64.run  
  4. # ./postgresql-9.3.4-1-linux-x64.run  
  5.     Installation Directory [/opt/PostgreSQL/9.2]:/usr/local/pgsql  
  6.     Data Directory [/usr/local/pgsql/data]:   
  7.     Password :postgres  
  8.     Retype password :postgres  
  9.     Port [5432]:  
  10.     Please choose an option [1] : 430 ←选择对应的语言(430代表日语)  
  11.     Do you want to continue? [Y/n]: y  
  12.     Setup has finished installing PostgreSQL on your computer.  



只安装Client:

Shell代码  收藏代码
  1. # yum install readline-devel zlib-devel  
  2. # cd /usr/local/src/  
  3. # wget https://ftp.postgresql.org/pub/source/v9.3.4/postgresql-9.3.4.tar.gz  
  4. # tar xzvf postgresql-9.3.4.tar.gz  
  5. # cd postgresql-9.3.4  
  6. # ./configure --prefix /usr/local/pgsql  
  7. # make  
  8. # gmake -C src/bin install  
  9. # gmake -C src/include install  
  10. # gmake -C src/interfaces install  
  11. # gmake -C doc install  



(2)确认版本

Shell代码  收藏代码
  1. # id postgres  
  2. # su - postgres  
  3. # cd /usr/local/pgsql/bin  
  4. # ./psql --version  



(3)设置

Shell代码  收藏代码
  1. # cd /usr/local/pgsql/data  
  2. # cp postgresql.conf postgresql.conf.default  
  3. # vi postgresql.conf  
  4.     max_connections=100  
  5.     shared_buffers=128MB  
  6.     work_mem=16MB  
  7. # cp pg_hba.conf pg_hba.conf.default  
  8. # vi pg_hba.conf  
  9.     host    all         all         192.168.21.0/24        trust  
  10. # /etc/init.d/postgresql-9.3 restart  



数据库操作的历史记录文件

Shell代码  收藏代码
  1. #  touch /usr/local/pgsql/.psql_history  
  2. #  chown postgres:postgres /usr/local/pgsql/.psql_history  



通过pgAdmin测试是否连接成功。

(4)启动设置

Shell代码  收藏代码
  1. # chkconfig postgresql-9.3 on  
  2. # chkconfig --list postgresql-9.3  



(5)创建登录用户

Shell代码  收藏代码
  1. # su - postgres  
  2. # cd /usr/local/pgsql/bin  
  3. # ./createuser -s -d -r -l -P pguser  
  4.     Enter password for new role: 123456  
  5.     Enter it again: 123456  
  6.     Password :postgres  
  7.     ./psql  
  8.     Password :postgres  
  9.     postgres=# \du  
  10.                                  List of roles  
  11.      Role name |                   Attributes                   | Member of  
  12.     -----------+------------------------------------------------+-----------  
  13.      pguser    | Superuser, Create role, Create DB              | {}  
  14.      postgres  | Superuser, Create role, Create DB, Replication | {}  
  15.     postgres=# \q  



(6)创建数据库

Shell代码  收藏代码
  1. # ./createdb -E UTF8 -O pguser -U pguser pgdb  
  2.     Password :postgres  
  3. # ./psql -l  
  4.     Password :postgres  
  5. # ./psql -U pguser -d pgdb  
  6.     postgres=# \l  
  7.      List of databases  
  8.        Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges  
  9.     -----------+----------+----------+------------+------------+----  
  10.      pgdb    | pguser       | UTF8     | ja_JP.utf8 | ja_JP.utf8 |  
  11.     postgres=# CREATE TABLE test_users(id integer NOT NULL, name character varying(10) NOT NULL, CONSTRAINT test_users_pkc PRIMARY KEY (id));  
  12.     postgres=# INSERT INTO test_users(id,name) VALUES (1, 'rensanning');  
  13.     postgres=# SELECT * FROM test_users;  
  14.      id | name  
  15.     ----+------  
  16.       1 | rensanning  
  17.     (1 row)  
  18.     postgres=# \q  



(7)主从设置

①设置Master

Shell代码  收藏代码
  1. # vi /usr/local/pgsql/data/postgresql.conf  
  2.     wal_level = hot_standby  
  3.     synchronous_commit = on  
  4.     max_wal_senders = 2  
  5.     synchronous_standby_names = 'slave1'  
  6.     wal_keep_segments = 100  
  7. # vi /usr/local/pgsql/data/pg_hba.conf  
  8.     local   replication     postgres                                trust  
  9.     host    replication     postgres        127.0.0.1/32            trust  
  10. # /etc/init.d/postgresql-9.3 restart  



②设置Slave

Shell代码  收藏代码
  1. # /etc/init.d/postgresql-9.3 stop  
  2. # /usr/local/pgsql/bin/pg_basebackup -U postgres -h master_host -p 5432 -D /usr/local/pg_slave --xlog --progress --verbose  
  3. # cp /usr/local/pgsql/data/postmaster.opts /usr/local/pg_slave/  
  4. # mv /usr/local/pgsql/data/ /usr/local/pgsql/data_bak/  
  5. # cp /usr/local/pg_slave/ /usr/local/pgsql/data/  
  6. # chown -R postgres:postgres /usr/local/pgsql/data/  
  7. # vi /usr/local/pgsql/data/postgresql.conf  
  8.     hot_standby = on  
  9. # cp /usr/local/pgsql/share/postgresql/recovery.conf.sample /usr/local/pgsql/data/recovery.conf  
  10. # vi /usr/local/pgsql/data/recovery.conf  
  11.     standby_mode = on  
  12.     primary_conninfo = 'host=master_host port=5432 application_name=slave1'  
  13. # /etc/init.d/postgresql-9.3 restart  



③确认主从

Shell代码  收藏代码
  1. # su - postgres  
  2. # cd /usr/local/pgsql/bin  
  3. # ./psql -U pguser -d pgdb -c "INSERT INTO test_users VALUES (2,'slave')"  
  4.     OK  
  5. # ./psql -U pguser -d pgdb -c "select * from test_users"  
  6.      id | name    
  7.     ----+-------  
  8.       1 | rensanning  
  9.       2 | slave  
  10.     (2 rows)  
  11. # ./psql -U pguser -d pgdb -c "SELECT application_name,state,sync_priority,sync_state FROM pg_stat_replication;"  
  12.      application_name |   state   | sync_priority | sync_state   
  13.     ------------------+-----------+---------------+------------  
  14.      slave1           | streaming |             1 | sync  
  15.      slave1           | streaming |             1 | potential  



Shell代码  收藏代码
  1. # su - postgres  
  2. # cd /usr/local/pgsql/bin  
  3. # ./psql -U pguser -d pgdb -c "INSERT INTO test_users VALUES (3,'slave')"  
  4.     ERROR:  cannot execute INSERT in a read-only transaction  
  5. # ./psql -U pguser -d pgdb -c "select * from test_users"  
  6.      id | name    
  7.     ----+-------  
  8.       1 | rensanning  
  9.       2 | slave  
  10.     (2 rows)  



posted @ 2015-07-07 15:31  Ben@Li  阅读(256)  评论(0编辑  收藏  举报